Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Stack PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

University of Engineering & Management, Kolkata

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. Push : Adding an element onto the stack


2. Pop : Removing an element from the stack

3. Peek : Look all the elements of stack without removing them.

How the stack grows?


Scenario 1 : Stack is empty

The stack is called empty if it doesn't contain any element


inside it. At this stage, the value of variable top is -1.

Scenario 2 : Stack is not empty


Value of top will get increased by 1 every time when we add any element to the stack. In the
following stack, After adding first element, top = 2.

Scenario 3 : Deletion of an element

Value of top will get decreased by 1 whenever an


element is deleted from the stack.

In the following stack, after deleting 10 from the stack,


top = 1.

Top and it’s value :

Top position Status of stack

-1 Empty

0 Only one element in the stack

N-1 Stack is full

N Overflow

Array implementation of Stack


In array implementation, the stack is formed by using the array. All the operations
regarding the stack are performed using arrays. Lets see how each operation can be
implemented on the stack using array data structure.
Adding an element onto the stack (push operation)
Adding an element into the top of the stack is referred to as push operation. Push
operation involves following two steps.

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 :

PEEK (STACK, TOP)

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 ()
{

printf("Enter the number of elements in the stack ");


scanf("%d",&n);
printf("********Stack operations using array*******");
printf("\n----------------------------------------------\n");
while(choice != 4)
{
printf("Chose one from the below options...\n");
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
printf("\n Enter your choice \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
show();
break;
}
case 4:
{
printf("Exiting....");
break;
}
default:
{
printf("Please Enter valid choice ");
}
};
}
}
void push ()
{
int val;
if (top == n )
printf("\n Overflow");
else
{
printf("Enter the value?");
scanf("%d",&val);
top = top +1;
stack[top] = val;
}
}

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();
}

void push(char symb)


{
if(top>=49)
{
printf("\nSTACK OVERFLOW");
getch();
return;
}
else
{
top=top+1;
stack[top]=symb;
}
}
char pop()
{
char item;
if(top==-1)
{
printf("\nSTACK IS EMPTY");
getch();
return(0);
}
else
{
item=stack[top];
top--;
}
return(item);
}
int preced(char ch)
{
if(ch==47)
{
return(5);
}
else if(ch==42)
{
return(4);
}
else if(ch==43)
{
return(3);
}
else
return(2);
}
void post(char infix[])
{
int l;
int index=0,pos=0;
char symbol,temp;
char postfix[40];
l=strlen(infix);
push('#');
while(index<l)
{
symbol=infix[index];
switch(symbol)
{
case '(': push(symbol);
break;
case ')': temp=pop();
while(temp!='(')
{
postfix[pos]=temp;
pos++;
temp=pop();
}
break;
case '+':
case '-':
case '*':
case '/':
case '^':
while(preced(stack[top])>=preced(symbol))
{
temp=pop();
postfix[pos]=temp;
pos++;
}
push(symbol);
break;
default: postfix[pos++]=symbol;
break;
}
index++;
}
while(top>0)
{
temp=pop();
postfix[pos++]=temp;
}
postfix[pos++]='\0';
puts(postfix);
return;
}

Output:

You might also like