STACK
A stack is a data structure that stores data in such a way that the last element stored ia the first one retrieved.
Also called as Last - in first - out data structure (LIFO)
Only access to the stack is the top element
Two operation on stack
1) Push (insert)
2) Pop (delete)
Push:-
The operation to place a new item at the top of the stack.
Pop:-
The operation to remove the next item from the top of the stack.
Stack:-
a) static implementation (array)
b) Dynamic implementation (Link-list)
Properties of stack
PUSH OPERATION:
Initialize top = -1
Step 1: [check for stack overflow]
If top==MAXSIZE - 1
output "Stack overflow " and exit
Step 2 : [Increment the pointer value by one]
top=top+1
Step 3 : [Insert Element ]
S[top]=value
Step 4 : Exit
Program:-
Void push ()
{
int value;
if (top==mansize-1)
{
cout<<"stack is full";
}
else
{
cout<<"Enter the element to be pushed";
cin>>value;
top=top+1;
[top]=value;
}
}
POP OPERATION:-
Step 1 : [check whether stack is empty]
if top == -1
output "stack underflow" and exit
Step 2 : [Remove the item from the top of the stack]
value = s[top]
Step 3 : [Decrement the pointer value by one]
top = top - 1
Step 4 : [Return the delete item from stack]
return (value);
Step 5 : Exit
Program:-
int pop()
{
int value;
if(top == -1)
{
cout<<"stack is empty";
}
else
{
value = s[stop];
top = top -1;
cout<<"No. deleted is "<<value;
}
return (value);
}
Application of stack
1) Reversing a string
2) Expression
3) Function call
4) Calculation of postfix expression
Also called as Last - in first - out data structure (LIFO)
Only access to the stack is the top element
Two operation on stack
1) Push (insert)
2) Pop (delete)
Push:-
The operation to place a new item at the top of the stack.
Pop:-
The operation to remove the next item from the top of the stack.
Stack:-
a) static implementation (array)
b) Dynamic implementation (Link-list)
Properties of stack
- All insertions and deletions can occure only at top of the stack
- Only one element can be pushed or popped from the stack at a time.
- The elements are removed from stack in the reverse order in which they were inderted.
- It works in last-in-first-out (LIFO) mannar.
PUSH OPERATION:
Initialize top = -1
Step 1: [check for stack overflow]
If top==MAXSIZE - 1
output "Stack overflow " and exit
Step 2 : [Increment the pointer value by one]
top=top+1
Step 3 : [Insert Element ]
S[top]=value
Step 4 : Exit
Program:-
Void push ()
{
int value;
if (top==mansize-1)
{
cout<<"stack is full";
}
else
{
cout<<"Enter the element to be pushed";
cin>>value;
top=top+1;
[top]=value;
}
}
POP OPERATION:-
Step 1 : [check whether stack is empty]
if top == -1
output "stack underflow" and exit
Step 2 : [Remove the item from the top of the stack]
value = s[top]
Step 3 : [Decrement the pointer value by one]
top = top - 1
Step 4 : [Return the delete item from stack]
return (value);
Step 5 : Exit
Program:-
int pop()
{
int value;
if(top == -1)
{
cout<<"stack is empty";
}
else
{
value = s[stop];
top = top -1;
cout<<"No. deleted is "<<value;
}
return (value);
}
Application of stack
1) Reversing a string
2) Expression
3) Function call
4) Calculation of postfix expression
No comments