Unit 3 - Stack
Unit 3 - Stack
9
8
7
6
5
top 4 6
3 -5
2 12
1 10
0 8
Stack after popping top two
elements
9
8
7
6
5
4
3
top 2 12
1 10
0 8
stack after pushing elements 8,10,12,-5,6,9,55
9
8
7
top 6 55
5 9
4 6
3 -5
2 12
1 10
0 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
• isempty—to check whether the stack s is
empty
Representation of stack in
memory
• Representation of stack using array:
Suppose elements of the stack are integer type and stack can
store maximum 10 elements..
#define MAX 10
typedef struct
{
int top;
int elements[MAX];
}stack;
stack s;
• Here we have defined our own data type named stack.
• First element top will be used to index top element
• Array elements hold the elements of the stack
• Last line declares variable s of type stack
stack
• In addition to the previous declaration, we
will use the declaration
typedef enum {false, true }
Boolean;
0 1 2 3 4 5 6 7 8 9
2 8 10 12
top
0 1 2 3 4 5 6 7 8 9
6 8 10 12 -5 6 9 55
top
Creating an empty stack
• Before we can use a stack, it is to be initialized.
• As the index of array elements can take any value in the
range 0 to MAX-1, the purpose of initializing the stack is
served by assigning value -1 to the top of variable.
• This simple task can be accomplished by the following
function.
6 -5 12 10 8 X
top
6 -5 12 X
top
55 9 6 -5 12 10 8 X
Creating an empty stack
Before we can use a stack, it is to be initialized.
To initialize a stack, we will create an empty
linked list.
The empty linked list is created by setting pointer
variable top to value NULL.
Void createstack(stack **top)
{
*top=NULL;
}
Testing stack for underflow
Boolean isempty(stack *top)
{
if(top==NULL)
return true;
else
return false;
}
or
Boolean is empty(stack *top)
{
return ((top==NULL)?true:false);
}
Testing stack for overflow
Since stack represented using a linked list
can grow to a limit of computers memory,
there overflow condition never occurs.
Hence this operation is not implemented
for linked list.
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;
}
Accessing top element
Int peek(stack *top)
{
return(top->info)
}
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
3. Quick sort algorithm
Parenthesis checker
() [(( )
( { } [ ]) ( { } [ ]))
({[][]}) (( { [ ] [ ] } )
[ { { { } ( )} [ ] } [ ] ( ) { } ] ([ { { { } ( )} [ ] } }[ ] ( ) { } ]