Lecture11 of DSF
Lecture11 of DSF
stack
A stack is the
ordered list in
which all insertion
and deletion will 60 top
be done at the
50
single end.
40
Typical example of
stack is stack of 30
plates 20
10
Primitive stack operations
To create a stack
To insert one element
To delete one element
To check which element is onto top
To check whether the stack empty
or not.
Stack using array
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define size 5
struct stack
{
int s[size];
int top;
}st;
Stack using array
int stfull()
{
if(st.top>=size-1)
return 1;
else
return 0;
}
void push(int item)
{
st.top++;
st.s[st.top]=item;
}
int stempty()
{
if(st.top==-1)
return 1;
else
return 0;
}
int pop()
{
int item;
item=st.s[st.top];
st.top--;
return(item);
}
void display()
{
int i;
if(stempty())
printf("\n Stack is empty!!");
else
{
for(i=st.top;i>=0;i--)
printf("\n%d",st.s[i]);
}
}
void main(void)
{
int item,choice;
char ans;
st.top=-1;
clrscr();
printf("\n\t\t Implementation of stack");
do
{
printf("\n Main menu");
printf("\n1.Push\n2.Pop\n3.Display\n4.Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\nEnter the item to be
pushed: ");
scanf("%d",&item);
if(stfull())
printf("\nStack is full!");
else
push(item);
break;
case 2:if(stempty())
printf("\nEmpty Stack!
Underflow!!");
else
{
item=pop();
printf("\nThe popped element
is: %d",item);
}
break;
case 3:display();
break;
case 4: exit(0);
}
printf("\nDo you want to continue?");
ans=getche();
}
while(ans=='Y'||ans=='y');
getch();
}
STACK USING LINK LIST
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<alloc.h>
typedef struct stack{ int data;
struct
stack*next;}node;
void main()
{
node*top;
int val,item,choice;
char ans,ch;
void push(int ,node**);
void display(node**);
int pop(node**);
int sempty(node*);
clrscr();
top=NULL;
printf("\nstack using linklist");
do
{
printf("\n1.push\n2.pop\n3.display\n4.exit");
printf("\nenter ur choice");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\nenter the data");
scanf("%d",&val);
push(val,&top);
break;
case 2:if (sempty(top))
printf("\nstack is empty");
else
{
item=pop(&top);
printf("\npopped elementis%d",item);
}
break;
case 3:display(&top);
break;
case 4:printf("do u want 2 quit");
ch=getche();
if(ch=='y')
exit(0);
else
break;
default:printf("\nwrong choice");
}
printf("\ndo u want2 continue");
ans=getche();
}
while(ans=='y');
getch();
}
void push(int item,node**top)
{
node*New;
node*get_node(int);
New=get_node(item);
New->next=*top;
*top=New;
}
node*get_node(int item)
{
node*temp;
temp=(node*)malloc(sizeof(node));
if(temp==NULL)
printf("\n memory is not allocated");
temp->data=item;
temp->next=NULL;
return temp;
}
int sempty(node*temp)
{
if(temp==NULL)
return 1;
else
return 0;
}
int pop(node**top)
{
int item;
node*temp;
item=(*top) ->data;
temp=(*top);
*top=(*top) ->next;
free(temp);
return (item);
}
void display(node**head)
{
node*temp;
temp=*head;
if(sempty(temp))
printf("\nstack is empty");
else
{
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->next;
}
}
getch();
}
Application of stack
Various application of the stack
Expression conversion
Expression evaluation
Parsing well formed parenthesis
Decimal to binary conversion
Reversing a string
Storing function call
Concept of infix prefix and postfix
expression
Algorithm
Read the infix expression for left to right
one character at a time
Initially push $ onto the stack for $in
stack set priority as -1 if input symbol
read is( then push it on to the stack
If the input symbol read is an opersnd
then place it in postfix expression
If the input symbol read is operator
a)Check if priority of operator in stack is greater
than the priority of incoming operator then pop
the operator from the stack and place itin the
postfix expression.repeat step 4 a) till we get the
opertator from the stack which has greater
priority then incoming operator.
b)otherwise push the operator being read onto
the stack
C) if we read input operator as the ) then pop the
operator until we not get (
struct stack
{
double s[size];
int top;
}st;
void main()
{
char exp[size];
int len;
Double result;
Double post();
clrscr();
printf("\nEnter a postfix expression :
");
scanf(“%s”, &exp);
len=strlen(exp);
exp[len]=‘$’;
result=post[exp];
Printf(“\n the value of the
expression is%f”,result);
getch();
}
double post(char exp[])
{ char ch, *type;
Double result ,val,op1,op2;
Void push (double);
Double pop();
int i;
St.top=-1;
i=0;
Ch=exp[i];
While(ch!=‘$’)
{
if(ch>=‘0’&& ch<=‘9’)
type=“operand”;
elseIf(ch==‘+’||ch==‘-’||ch==‘*’||ch==‘/’||
ch==‘^’)
type=“operator”;
If (strcmp(type,”operand”)==0)
{val=ch-48;
Push(val);
}
Else if(strcmp(type,”operator”)==0)
{
op2=pop();
op1=pop();
switch(ch)
{
case '+': result=op1+op2;
break;
case '-:result=op1-op2;
break;
case '*':result=op1*op2;
break;
case
'/':result=op1/op2;break;
case‘^':result=pow(op1,op2);
break
}
Push(result);
}
i++;
Ch=exp[i];
}
Result=pop();
Return result;
}
Void push(double val)
{
If (st.top>=size-1)
Printf(“\n stack full”);
St.top++;
St.s[st.top]=val;
}
Double pop()
{
Double val;
If(st.top==-1)
Printf(“\n stack is empty”);
Val=st.s[st.top];
St.top--;
Return val;
}
queues
The queue is defined as the
ordered collection of elements that
has two ends front and rear from
the front end one can remove the
element and from the rear end one
can delet the element.
Ex people waiting for ticket counter.
Operation on queue
The queue is called as FIFO,
Operations like
Queue overflow
Insertion of the element into the
queue
Queue underflow
Deletion of the element
Display of the queue
Operation on queue
Before performing the insert operation we check the
queue overflow
10 20 30 40 50 60
Front
end
Rear end
}