Stack PDF
Stack PDF
Stack PDF
Stream: - B.Tech
Subject: Programming Lab – II (ESC – 295)
Stack
1. Stack is an ordered list in which, insertion and deletion can be performed only at one
end that is called top.
2. Stack is a recursive data structure having pointer to its top element.
3. Stacks are sometimes called as Last-In-First-Out (LIFO) lists i.e. the element which is
inserted first in the stack, will be deleted last from the stack.
Applications of Stack
1. Recursion
2. Expression evaluations and conversions
3. Parsing
4. Browsers
5. Editors
6. Tree Traversals
Operations on Stack
There are various operations which can be performed on stack.
-1 Empty
N Overflow
Increment the variable Top so that it can now refere to the next memory location.
Add element at the position of incremented top. This is referred to as adding new element
at the top of the stack.
Stack is overflown when we try to insert an element into a completely filled stack
therefore, our main function must always avoid stack overflow condition.
Algorithm:
begin
if top = n then stack full
top = top + 1
stack (top) : = item;
end
implementation of push algorithm in C language
void push (int val,int n) //n is size of the stack
{
if (top == n )
printf("\n Overflow");
else
{
top = top +1;
stack[top] = val;
}
}
Deletion of an element from a stack (Pop operation)
Deletion of an element from the top of the stack is called pop operation. The value of the
variable top will be incremented by 1 whenever an item is deleted from the stack. The top
most element of the stack is stored in an another variable and then the top is decremented
by 1. the operation returns the deleted value that was stored in another variable as the
result.
The underflow condition occurs when we try to delete an element from an already empty
stack.
Algorithm :
begin
if top = 0 then stack empty;
item := stack(top);
top = top - 1;
end;
Implementation of POP algorithm using C language
int pop ()
{
if(top == -1)
{
printf("Underflow");
return 0;
}
else
{
return stack[top - - ];
}
}
Visiting each element of the stack (Peek operation)
Peek operation involves returning the element which is present at the top of the stack
without deleting it. Underflow condition can occur if we try to return the top element in
an already empty stack.
Algorithm :
Begin
if top = -1 then stack empty
item = stack[top]
return item
End
Implementation of Peek algorithm in C language
int peek()
{
if (top == -1)
{
printf("Underflow");
return 0;
}
else
{
return stack [top];
}
}
Code:
#include <stdio.h>
int stack[100],i,j,choice=0,n,top=-1;
void push();
void pop();
void show();
void main ()
{
void pop ()
{
if(top == -1)
printf("Underflow");
else
top = top -1;
}
void show()
{
for (i=top;i>=0;i--)
{
printf("%d\n",stack[i]);
}
if(top == -1)
{
printf("Stack is empty");
}
}
1. Write a C program to implement stack using linked list, each node should have
the following information about a Student: S_Name (string), S_address (string),
S_Marks. (Use local pointer i.e. with return statement)
Solution:
Stack:
A stack is basically a container of object that store the data in order of Last in First Out
(LIFO).
A stack basically perform three operation.
Push: for adding the element
Pop: For removing the element
Peek: For finding top most element.
In this example, we implement the stack using linked list and structure is used for storing the
information of student.
Program
#include<stdio.h>
#include<stdlib.h>
struct node
{
char s_name[20],s_address[50];
int s_marks;
struct node *next;
}s;
s *push(s*);
s *pop(s *);
void display(s *);
int main()
{
s *top=NULL;
int ch,x,c=0;
printf("Enter 1 for push\n");
printf("Enter 2 for pop\n");
printf("Enter 3 for display\n");
do
{
printf("Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
top=push(top);
break;
case 2:
top=pop(top);
break;
case 3:
display(top);
break;
}
printf("do you want to continuoe press 1: ");
scanf("%d",&c);
}while(c==1);
}
s *push(s *top)
{
s *p;
p=(s *)malloc(sizeof(s));
if(p==NULL)
{
printf("no memory allocated");
}
else
{
printf("\nEnter the student name: ");
scanf("%s",&p->s_name);
printf("Enter student address: ");
scanf("%s",&p->s_address);
printf("Enter the marks of students: ");
scanf("%d",&p->s_marks);
p->next=top;
top=p;
}
return(top);
}
s *pop(s *top)
{
s *p;
if(top==NULL)
{
printf("nothing to pop");
}
else
{
printf("\nThe student name is: %s",top->s_name);
printf("\nThe student address is: %s",top->s_address);
printf("\nThe marks of the student is: %d",top->s_marks);
top=top->next;
}
return(top);
}
void display(s *top)
{
if(top==NULL)
{
printf("nothing to display");
}
else
{
while(top!=NULL)
{
printf("\nThe student name is: %s",top->s_name);
printf("\nThe student address is: %s",top->s_address);
printf("\nThe marks of the student is: %d",top->s_marks);
top=top->next;
}
}
}
Output:
2. Write a C program to convert infix expression into postfix expression.
Solution:
In infix expression, Operators are written in-between their operands. This is the
common way for writing expression. E.g. a+b*c.
In postfix expression, the operators are written after the their operands. It is also
known as “reverse polish notation”. E.g. abc*+.
In this example we convert the infix notation into the postfix notation.
Program
#include<stdio.h>
#include<string.h>
char stack[50];
int top=-1;
void post(char infix[]);
void push(char);
char pop();
void main()
{
char infix[25];
printf("\nENTER THE INFIX EXPRESSION = ");
gets(infix);
post(infix);
getch();
}
Output: