Stacks in Data Structure
Stacks in Data Structure
Objective After this lecture you will be able to: Describe a stack Describe the representation of stack using linear array Describe the representation of stack using linear linked list Implementation of various operations on stack Describe some applications of stacks
Stack
Stack is one of the commonly used data structures. Stack is also called last in first out (LIFO) system Stack is a linear list in which insertion and deletion can take place only at one end called top. This structure operates in much the same way as stack of trays.
Stack of Trays
The following figure illustrate a stack, which can accommodate maximum of 10 elements figure shows stack after pushing elements 8,10,12,-5,6
9 8
7
6 5 top 4 3 2 1 0 6
-5
12 10 8
7
6 5 4 3 top 2 1 0 12 10 8
9 8
7
top 6 5 4 3 2 1 0 55 9 6
-5
12 10 8
Operations on stacks
Createstack(s)to create s as an empty stack Push(s,i)--to push elementi onto stack s. Pop(s)to access and remove the top element of the stack s Peek(s)to access the top element of the stacks without removing it from the stack s. Isfull(s)to check whether the stack s is full isemptyto check whether the stack s is empty
stack
In addition to the previous declaration, we will use the declaration typedef enum {false, true } Boolean; This statement defined new data type named Boolean which can take value false or true.
10 12 -5
10 12
0 6 top
10 12 -5
55
Push Operation
Before the push operation, if the stack is empty, then the value of the top will be -1and if the stack is not empty then the value of the top will be the index of the element currently on the top. Therefore we place the value onto the stack, the value of top is incremented so that it points to the new top of stack, where incoming element is placed. Void push(stack *ps, int value) { ps->top++; ps->elements[ps->top]=value; }
Pop Operation
The element on the top of the stack is assigned to a local variable, which later on will be returned via the return statement. After assigning the top element to a local variable, the variable top is decremented so that it points to a new top Int pop(stack *ps) { int temp; temp=ps->elements[ps->top]; ps->top--; return temp; }
Declaration of stack
The linked list representation allows a stack to grow to a limit of the computers memory. Typedef struct nodetype { int info; struct nodetype *next; }stack; Stack *top; Here I have defined my own data type named stack, which is a self referential structure and whose first element info hold the element of the stack and the second element next hold the address of the element under it in the stack. The last line declares a pointer variable top of type stack.
-5
12
10
top 6 -5 12 X
top 55 9 6 -5 12 10 8 X
Push operation
To push a new element onto the stack, the element is inserted in the beginning of the linked list. void push(stack **top, int value) { stack *ptr; ptr=(stack*)malloc(sizeof(stack)); if(ptr==NULL) { printf(\n unable to allocate memory for new node); printf(\npress any key to exit..); getch(); return; } ptr->info=value; ptr->next=*top; *top=ptr; }
Pop operation
To pop an element from the stack, the element is removed from the beginning of the linked list. Int pop(stack **top) { int temp; stack *ptr; temp=(*top)->info; ptr=*top; *top=(*top)->next; free(ptr); return temp; }
Dispose a stack
Because the stack is implemented using linked lists, therefore it is programmers job to write the code to release the memory occupied by the stack. Void disposestack(stack **top) { stack *ptr; while(*top!=NULL) { ptr=*top; *top=(*top)->next; free(ptr); } }
Applications of Stacks
Stacks are used to pass parameters between functions. On a call to function, parameter and local variables are stored on stack. High level programming languages, such as Pascal c etc. that provide support for recursion use stack for book keeping. In each recursive call, there is need to save the current values of parameters, local variables and the return address. In addition to above stack are used to solve the various problems. 1. Parenthesis checker 2. Mathematical notation translation
1. Polish (prefix) notation 2. Reverse polish (postfix) Notation
Parenthesis checker
Parenthesis checker is a program that checks whether a mathematical expression is properly parenthesized. We will consider three sets of grouping symbols:
The standard parenthesis ( ) The braces { } the brackets [ ] For an input expression, it verifies that for each left parenthesis, braces or racket, there is a corresponding closing symbol and the symbols are appropriately nested.
( { } [ ])
({[][]})
( { } [ ]))
(( { [ ] [ ] } )
[ { { { } ( )} [ ] } [ ] ( ) { } ]
([ { { { } ( )} [ ] } }[ ] ( ) { } ]
Infix notation
In this notation, the operator symbol is placed between its two operands. To add A to B we can write as A+B or B+A To subtract D from C we write as C-D, but we can not write D-C as this operation is not commutative.
7
5 9 2 / *
7
75 2 29 292 2 4.5 9
End of expression