8/12/2019 stack details....in c++
1/21
Data Structures
Dr Kazi A KalpomaAssociate Professor
Faculty of Science and Information Technology
American International University Bangladesh (AIUB)
Contact: [email protected]
DS Lecture 06Stack
3/9/2014 Dr. Kazi A. Kalpoma 1
mailto:[email protected]:[email protected]8/12/2019 stack details....in c++
2/21
8/12/2019 stack details....in c++
3/21
3
A stack is open at one end
(the top) only. You can push
entry onto the top, or popthe top entry out of the stack.
Note that you cannot
add/extract entry in the
middle of the stack.
Stack
A
B
C
bottom
top
pushpop
8/12/2019 stack details....in c++
4/21
4
Last-in First-out (LIFO)
A A
B
A
B
C
When we push entries
onto the stack and thenpop them out one by one,
we will get the entries in
reverse order.
The last one
pushed in is the
first one poppedout! (LIFO)
A A
B
Insert(A);
Insert(B);
Insert(C);
Remove();
Remove();
Remove();
8/12/2019 stack details....in c++
5/21
5
Stack Operations
Push(X) insert X as the topelement of the stack
Pop()remove the top element ofthe stack and return it.
Top()return the top element
without removing it from the stack.
8/12/2019 stack details....in c++
6/21
Stack Operations
push(2)
top 2
push(5)
top
2
5
push(7)
top
2
5
7
push(1)
top
2
5
7
1
1 pop()
top
2
57
push(21)
top
2
57
21
21 pop()
top
2
57
7 pop()
2
5top
5 pop()
2top
top
8/12/2019 stack details....in c++
7/21
Stack using an Array
top
2
5
7
12 5 7 1
0 1 32 4
top = 3
8/12/2019 stack details....in c++
8/21
8
#define stack_size 6;
int top;
char stack[stack_size];
Array implementation
A
B
?
?
?
?
top = 2
stack
stack
8/12/2019 stack details....in c++
9/21
9
Initialization of Stack
stack_initialize()
{
top = -1;
}
?
?
?
?
?
?
top = 0
entrytop
8/12/2019 stack details....in c++
10/21
10
Push()
void push(char i)
{
if(top==stack_size - 1)
cout
8/12/2019 stack details....in c++
11/21
11
Pop()void pop()
{if(top
8/12/2019 stack details....in c++
12/21
12
Traversing a stack
void show()
{
int i;for(i=0;i
8/12/2019 stack details....in c++
13/21
13
Other operations
int top(){
return stack[top];}
int IsEmpty(){
return ( top == -1 );}
int IsFull(){return ( top == stack_size-1);
}
8/12/2019 stack details....in c++
14/21
Example of PUSH() and POP()
3/9/2014 Dr. Kazi A. Kalpoma 14
top0
1
2
3
stack
void main()
{
push(7);
push (8);
push (9);pop ( );
push (5);
pop ( );
pop ( );
}
4
5
stack[0]=7 and top = 0
stack[1]=8 and top = 1
stack[2]=9 and top = 2x = 9 and top = 1
stack[2]=5 and top = 2
x = 5 and top = 1
x = 8 and top = 0
8/12/2019 stack details....in c++
15/21
Stack Using Linked List
We can avoid the size limitation of a stackimplemented with an array by using a linked
list to hold the stack elements.
8/12/2019 stack details....in c++
16/21
Stack Using Linked List
For a singly-linked list, insert at startor endtakes constant time using the headand currentpointers respectively.
Removing an element at the startis constanttime but removal at the endrequired traversingthe list to the node one before the last.
Make sense to place stack elements at the start
of the list because insert and removal areconstant time.
8/12/2019 stack details....in c++
17/21
8/12/2019 stack details....in c++
18/21
Stack Operation: List
int pop(){
int x = head->data;
Node* p = head;
head = head->Next;
delete p;
return x;}
top
2
5
7 1 7 5 2
head
8/12/2019 stack details....in c++
19/21
Stack Operation: List
void push(int x){
Node* newNode = new Node();
newNode->data = x;
newNode->Next = head;
head = newNode;}
top
2
5
7
9
7 5 2
head
push(9)
9
newNode
8/12/2019 stack details....in c++
20/21
Stack Operation: List
int top(){
return head->data;}
int IsEmpty(){
return ( head == NULL );}
IsFull()is not needed anymore.
8/12/2019 stack details....in c++
21/21
Stack: Array or List
Since both implementations support stackoperations in constant time, any reason to chooseone over the other?
Allocating and de-allocating memory for list nodes
does take more time than pre-allocated array.
List uses only as much memory as required by thenodes; array requires allocation ahead of time.
List pointers (head, next) require extra memory.
Array has an upper limit; List is limited by dynamicmemory allocation.