Sei sulla pagina 1di 16

Stacks

Stack is a linear D.S that contains list of elements in


which insertion and deletions are done at one end of
the stack.
BASIC TERMINOLOGY :
1.TOP
2.OVERFLOW
3.UNDERFLOW
1.TOP : It keeps track of current position of the stack.
2.OVERFLOW :This condition occurs when we try to insert
element into the stack when the stack reaches its
maximum size.
3.UNDERFLOW : This condition occurs when we try to
delete element from the stack when the stack is
empty;

BASIC OPERATIONS ON STACK


1.PUSH
2.POP
3.ISFULL
4.ISEMPTY
1.PUSH :Inserting an element into the stack is called
push.
2.POP :Deleting an element from the stack is called
pop.
3.ISFULL :This function is used to check whether stack
is empty or not.
4.ISEMPTY : This function is used to check whether top
reaches its maximum size.
EXAMPLES ON STACK :

Plate trays

Pile of books

Representation of STACK

PUSH
6
5
TOP

PO
P

Characteristics of stacks
1.Stack is an ordered list with the restriction that
insertion or deletions are done only at the top of
the stack.
2.Whenever a new element is inserted older
element moves down one position.
3.Stack is based on LIFO (Last In First Out) .
IMPLEMENTATION OF STACKS
1.By using ARRAYS.
2.By using Linked Lists

1.By using ARRAYS


#include<stdio.h>
#include<stdlib.h>
struct stack
{
int top,size,*p;
};
struct stack*create(int max)
{
struct stack*s;
s=(struct stack*)malloc(sizeof(struct stack));
s->top=-1;
s->size=max;
s->p=(int*)malloc(sizeof(int)*max);
return s;
}
int isfull(struct stack*s)
{
if(s->top==s->size-1)
return 1;
else
return 0;
}

void push(struct stack*s,int x)


{
if(!isfull(s))
s->p[++s->top]=x;
else
printf("over flow\n");
}
int isempty(struct stack*s)
{
if(s->top==-1)
return 1;
else
return 0;
}

void pop(struct stack*s)


{
if(!isempty(s))
printf("deleted element is%d",s->p[s->top--]);
else
printf("under flow\n");
}
void display(struct stack*s)
{
int i;
printf("elements are\n");
for(i=0;i<=s->top;i++)
printf("%d\t",s->p[i]);
}

main()
{
struct stack*s;
int n,ch,x;
printf("enter the capacity of stack\n");
scanf("%d",&n);
s=create(n);
while(1)
{
printf("1.push\t2.pop\t3.display\t4.exit\n");
printf("enter your choice\n");
scanf("%d",&ch);

switch(ch)
{
case 1:printf("enter the element to be insert\n");
scanf("%d",&x);
push(s,x);
printf("elements after insertion acre\n");
display(s);
break;
case 2:pop(s);
printf("elements after deleting are\n");
display(s);
break;
case 3:display(s);
break;
case 4:exit(0);
break;
}
}
}

1.By using Linked lists


#include<stdio.h>
#include<stdlib.h>
struct stack
{
int top,size,*p,data;
struct stack*next;
};
struct stack*create(int x)
{
struct stack*s;
s=(struct stack*)malloc(sizeof(struct
stack));
s->data=x;
s->next=NULL;
return s;
}
struct stack*push(struct stack*s,int x)
{
struct stack*temp=create(x);
temp->next=s;
return temp;
}

struct stack*pop(struct stack*s)


{
if(s!=NULL)
printf("deleted element is %d",s>data);
s=s->next;
return s;
}
void display(struct stack*s)
{
if(s==NULL)
printf("stack is empty\n");
else
{
printf("\nelements are");
while(s!=NULL)
{
printf("%d\t",s->data);
s=s->next;
}
}
}

main()
{
struct stack*s=NULL;
int x,ch;
while(1)
{
printf("\n1.push\t2.pop\t3.display\t4.e
xit");
printf("\nenter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("enter the element to
insert\n");
scanf("%d",&x);
s=push(s,x);
display(s);
break;

case 2:
s=pop(s);
display(s);
break;
case 3:
display(s);
break;
case 4:exit(0);
break;
}
}
}

Applications Of Stacks
1.Conversion of infix to postfix
expression.
2.Conversion of infix to prefix
expression.
3.Evaluation of postfix expression.
4.Paranthesis matching.
5.Recursion.

Potrebbero piacerti anche