Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Lecture No.
05
___________________________________________________________________
Data Structures
Lecture No. 05
Reading Material
Data Structures and Algorithm Analysis in C++
Chapter. 3
3.1, 3.2.5, 3.3.1, 3.3.2
(array implementation)
Summary
1)
2)
3)
4)
In the previous lecture, we demonstrated the use of the circular list for the resolution
of the Josephus problem. After writing a program with the help of this data structure, a
leader among ten persons was selected. You must have noted many things while trying
to solve the problem. These things will help us to understand the usage of data
structures in C++, thus making the programming easy. The code of the program is
given below.
#include "CList.cpp"
void main(int argc, char *argv[])
{
CList list;
int i, N=10, M=3;
for(i=1; i <= N; i++ ) list.add(i);
list.start();
while( list.length() > 1 ) {
for(i=1; i <= M; i++ ) list.next();
cout << "remove: " << list.get() << endl;
list.remove();
}
cout << "leader is: " << list.get() << endl;
}
In the program, we include the file of the class CList and create its object i.e. list. Then
we solve the problem by using the add, start, length, next, remove and get methods of
the class CList.
In the program, we have included already-defined data structure CList. After defining
its different methods, we have an interface of Clist. There is no need to be worry about
the nature of the list i.e. whether it is linked list, doubly linked list or an array. For us, it
Page 1 of 10
Page 2 of 10
Page 3 of 10
Stacks
Lets talk about another important data structure. You must have a fair idea of stacks.
Some examples of stacks in real life are stack of books, stack of plates etc. We can add
new items at the top of the stack or remove them from the top. We can only access the
elements of the stack at the top. Following is the definition of stacks.
Stack is a collection of elements arranged in a linear order
Lets see an example to understand this. Suppose we have some video cassettes. We
took one cassette and put it on the table. We get another cassette and put it on the top
of first cassette. Now there are two cassettes on the table- one at the top of other.
Now we take the third cassette and stack it on the two. Take the fourth cassette and
stack it on the three cassettes.
Now if we want to take the cassette, we can get the fourth cassette which is at the top
and remove it from the stack. Now we can remove the third cassette from the stack
and so on. Suppose that we have fifty cassettes stacked on each other and want to
access the first cassette that is at the bottom of the stack. What will happen? All the
cassettes will fell down. It will not happen exactly the same in the computer. There may
be some problem. It does not mean that our data structure is incorrect. As we see in
the above example that the top most cassette will be removed first and the new
cassette will be stacked at the top. The same example can be repeated with the books.
In the daily life, we deal with the stacked goods very carefully.
Page 4 of 10
Description
Insert x as the top element of the stack
Remove the top element of the stack and return it.
Return the top element without removing it from the stack.
The push(x) method will take an element and insert it at the top of the stack. This
element will become top element. The pop() method will remove the top element of the
stock and return it to the calling program. The top() method returns the top-most stack
element but does not remove it from the stack. The interface method names that we
choose has special objective. In case of list, we have used add, remove, get, set as the
suitable names. However, for stack, we are using push, pop and top. We can depict the
activity from the method name like push means that we are placing an element on the
top of the stack and pushing the other elements down.
The example of a hotels kitchen may help understand the concept of stacks in a
comprehensive manner. In the kitchen, the plates are stacked in a cylinder having a
spring on the bottom. When a waiter picks a plate, the spring moves up the other
plates. This is a stack of plates. You will feel that you are pushing the plates in the
cylinder and when you take a plate from the cylinder it pops the other plates. The top
method is used to get the top- most element without removing it.
When you create classes, interfaces and methods, choose such names which depicts
what these method are doing. These names should be suitable for that class or factory.
Lets discuss the working of stack with the help of a diagram.
Page 5 of 10
top
top
5
7
5
7
5
push(2)
push(5)
push(7)
push(1)
top
top
top
top
21
7
5
7
5
pop()
top
push(21)
7
5
top
2
21
2
pop()
top
pop()
2
5
pop()
At the start, the stack is empty. First of all, we push the value 2 in the stack. As a
result, the number 2 is placed in the stack. We have a top pointer that points at the top
element. Then we said push(5). Now see how 2 and 5 are stacked. The number 5 is
placed at the top of number 2 and the pointer top moves one step upward. Then we
pushed the number 7 which is placed on the top and the number 2 and 5 are below.
Similarly, we push number 1. The last figure in the first row shows the stacked values
of the numbers- 1, 7, 5 and 2.
Lets pop the elements from the stack. The first figure of second row shows the pop
operation. As a result, the number 1 is popped. Than again we push the number 21 on
the stack. The number 7, 5, and 2 are already in the stack and number 21 is pushed at
the top. If we pop now, the number 21 is popped. Now number 7 is at the top. If we
pop again, the number 7 is popped. Pop again and the number 5 is popped and number
2 remains in the stack. Here with the help of this diagram, we are proving that the
values are added at the top and removed at the top in a stack.
The last element to go into the stack is the first to come out. That is why, a stack is
known as LIFO (Last In First Out) structure. We know that the last element pushed in
the stack is at the top which is removed when we call pop. Lets see some other
scenarios. What happens if we call pop() while there is no element? One possible wayout is that we have isEmpty() function that returns true if stack is empty and false
otherwise. This is a boolean function that returns false if there is no element in the
stack. Otherwise, it will return true. The second option is this that when we call pop on
an empty stack, it throws an exception. This is a concept of advanced C++. Exception
is also a way to convey that some unusual condition has arisen or something has gone
wrong. Suppose, if we have a division method and try to divide some number with
zero. This method will throw division by zero exception. Currently we will not throw
an exception but use the isEmpty() method. The user who is employing the stack is
Page 6 of 10
top
7
5
2
top = 3
In the above diagram, on the left side we have a stack. There are four elements in the
stack i.e. 1, 7, 5 and 2. The element 1 is the extreme-most that means that it is inserted
in the end whereas 7, 5, and 2 have been added before. As this is a LIFO structure so
the element 1 should be popped first. On the right side we have an array with positions
0, 1, 2, 3 and so on. We have inserted the numbers 2, 5, 7 and 1. We have decided that
the elements should be inserted at the end of the array. Therefore the most recent
element i.e. 1 is at position 3. The top is the index representing the position of the most
recent element. Now we will discuss the stack implementation in detail using array.
We have to choose a maximum size for the array. It is possible that the array may fillup if we push enough elements. Now more elements cannot be pushed. Now what
should the user of the stack do? Internally, we have implemented the stack using array
which can be full. To avoid this, we write isFull() method that will return a boolean
value. If this method returns true, it means that the stack (array) is full and no more
elements can be inserted. Therefore before calling the push(x), the user should call
isFull() method. If isFull() returns false, it will depict that stack is not full and an
element can be inserted. This method has become the part of the stack interface. So we
Page 7 of 10
Page 8 of 10
};
// The main method
int main()
{
Stack stack;
// creating a stack object
// pushing the 10 elements to the stack
for(int i = 0; i < 12; i++)
{
if(!stack.isFull())
// checking stack is full or not
stack.push(i);
// push the element at the top
else
cout <<"\n Stack is full, can't insert new element";
}
Page 9 of 10
Page 10 of 10