Unit-2_1
Unit-2_1
STACK
Stack is a special type of data structure where elements are inserted from one end and elements are
deleted from the same end. The position from where elements a are inserted and from where
elements are deleted is termed as top of the stack.
Thus stack is a homogeneous collection of elements of any one type, arranged linearly with access
at one end only. Stack is also called Last In First Out (LIFO) data structure.
OPERATION ON STACK:
• Push
• Pop
1. Push Operation: The procedure of inserting a new element to the top of the stack is known
as push operation. A pointer TOP is used to point the top element in the stack, which gets
incremented by 1 after every insertion.
If the stack is full and does not contain enough space to accept the given element or data, the
stack is then considered to be in an overflow state.
2. Pop Operation: The procedure of removing element from the top of the stack is called pop
operation. Only one element can be deleted at a time and element has to be deleted only
from the top of the stack. After every deletion, TOP gets decremented by 1.
When elements are being deleted, there is a possibility of stack being empty. When stack is
empty, it is not possible to delete any element. Trying to delete an element from an empty
stack results in stack underflow.
Implementation of stack:
Push Operation:
Description: Here STACK is an array with MAX locations. TOP points to the top most
element and ITEM is the value to be inserted.
Pop Operation:
Description: Here STACK is an array with MAX locations. TOP points to the top most
element.
To implement stack using linked list, we need to set the following things before implementing
actual operations.
• Define a 'Node' structure with two members data and next.
• Define a Node pointer 'top' and set it to NULL.
Push Operation:
Pop Operation:
Display Operation:
// Node Structure
struct Node
{
int data;
struct Node *next;
}*top = NULL; // Initially top is NULL
//Function declaration
void push(int);
void pop();
void display();
void main()
{
int choice, value;
/* Push operation */
void push(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode>data = value;
if(top == NULL)
newNode>next = NULL;
else
newNode>next = top;
top = newNode;
printf("\nInsertion is Success!!!\n");
display();
} // End of Push
/* Pop operation */
void pop()
{
if(top == NULL)
printf("\nStack is Empty!!!\n");
else{
struct Node *temp = top;
printf("\nDeleted element: %d\n", temp>data);
top = temp>next;
free(temp);
}
display();
} // End of Pop
/* Display */
void display()
{
if(top == NULL)
printf("\nStack is Empty!!!\n");
else{
struct Node *temp = top;
while(temp>next != NULL){
printf("%d>",temp>data);
temp = temp > next;
}
printf("%d>NULL \n",temp>data);
}
} // End of display
Output
Insertion is Success!!!
10>NULL
Insertion is Success!!!
30>10>NULL
Insertion is Success!!!
60>30>10>NULL
Insertion is Success!!!
20>60>30>10>NULL
Insertion is Success!!!
50>20>60>30>10>NULL
Deleted element: 50
20>60>30>10>NULL
3. Display
4. Exit
Enter your choice: 2
Deleted element: 20
60>30>10>NULL
Deleted element: 60
30>10>NULL
Deleted element: 30
10>NULL
Deleted element: 10
Stack is Empty!!!
Stack is Empty!!!
Stack is Empty!!!
APPLICATION OF STACK:
1. Stack is used by compilers to check for balancing of parentheses, brackets and braces.
2. Stack is used to evaluate a postfix expression.
3. Stack is used to convert an infix expression into postfix/prefix form.
4. In recursion, all intermediate arguments and return values are stored on the processor’s
stack.
5. During a function call the return address and arguments are pushed onto a stack and on
return they are popped off.
Infix to postfix:
5. If the incoming symbol has higher precedence than the top of the stack, push it on
the stack.
6. If the incoming symbol has lower precedence than the symbol on the top of the
stack, pop the stack and print the top operator. Then test the incoming operator
against the new top of stack.
7. At the end of the expression, pop and print all operators on the stack. (No
parentheses should remain.)
Infix to postfix: