(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
Stack concepts by Divya
1. STACK CONCEPT BY DIVYA
LIFO-Last in first out
Operations of stack:
1. Insertion - push
2. Deletion -pop
3. Peek - returns topmost element of the stack
2. STACK CONCEPT BY DIVYA
Parsing- compiler
if, else,main,switch-32 keywords in C, identifiers
int a_12;
if()
{
statement 1;
…….
if()
3. STACK CONCEPT BY DIVYA
{
statement 2;
.….
Parsing -stack: no of open parenthesis=no. of closing
parenthesis
Whenever Opening brace is encountered -push element
Whenever Closing brace is encountered-pop element
{
Preprocessing
#include<stdio.h>
#define max 90
main()
{
int a;
if(a>max)
}
At the end stack is non-empty, means there is
syntax error in above code snippet and the
parenthesis are unmatched
4. STACK CONCEPT BY DIVYA
Memory representation of a process
Stack Representation using Array
Consider the number of elements the stack can store is N.
Following four conditions are possible depending on the value
of top( which points to the topmost position in the stack):
5. STACK CONCEPT BY DIVYA
1. If top=-1, it means the stack is empty and there are no
elements present in the stack. If we want to pop an element
from stack, it will result in underflow condition.
2. If top=N, it means the stack is full. If we want to push an
element inside stack, it will result in the overflow condition.
3. If the value of top is incremented by 1, means an element has
been pushed(inserted) inside the stack.
4. If the value of top is decremented by 1, means an element has
been popped(deleted) from the stack.
OPERATIONS ON STACK
I. Push operation
Algorithm
PUSH(STACK, TOP, N, ITEM)
1. Start
2. IF TOP = N THEN
DISPLAY Overflow
EXIT
END IF
3. TOP = TOP + 1
4. STACK[TOP] = ITEM [Insert Item into new TOP Position]
5. Stop
Static allocation
Program:
#include<stdio.h>
#define N 3
int top=-1,stack[N];
void push(int item)
{
if(top==N-1)
{
6. STACK CONCEPT BY DIVYA
printf("nOverflow Conditionn");
return;
}
top=top+1;
stack[top]=item;
}
void display (int stack[])
{
int i;
for(i=0;i<=top;i++)
printf(" %d ",stack[i]);
}
int main()
{
int item,num_elements,i;
printf("n No. of elements you want to insert inside the stack");
scanf("%d",&num_elements);
for(i=0;i<num_elements;i++)
{
printf("n Enter the element to be inserted ");
scanf("%d",&item);
push(item);
}
printf("nDisplay elements inside the stack");
display(stack);
printf("nDelete an element fron the stack");
//pop();
//printf("nDisplay elements inside the stack after deletion");
//display(stack);
return 0;
}
II. Pop operation
Algorithm
7. STACK CONCEPT BY DIVYA
POP(STACK, TOP, ITEM)
1. Start
2. IF TOP = 0 THEN
DISPLAY Underflow
EXIT
END IF
3. SET ITEM = STACK[TOP] [Call by reference]
4. TOP = TOP -1
5. Stop
Program: Pop function definition
void pop()
{
int Popped_Item;
if(top==-1)
{
printf("n underflow Condition");
return;
}
Popped_Item=stack[top];
printf("nDeleted element is%d",Popped_Item);
top=top-1;
}
8. STACK CONCEPT BY DIVYA
STACK REPRESENTATION USING LINKED LIST
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
}*top;
void push(int item)
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
if(!temp)
{
printf("nOverflow condition");
return;
}
temp->info=item;
temp->link=top;
9. STACK CONCEPT BY DIVYA
top=temp;
}
void display()
{
struct node *temp;
if(top==NULL)
printf("nstack is empty");
else
{
temp=top;
while(temp!=NULL)
{
printf(" %d ",temp->info);
temp=temp->link;
}
}
}
void pop()
{
struct node *temp;
if(top==NULL){
printf("nstack underflow");
return;
}
temp=top;
top=top->link;
temp->link=NULL;
free(temp);
}
int main()
{
int num_elem,i,item;
printf("nenter the number of elements you want to push inside
stack");
10. STACK CONCEPT BY DIVYA
scanf("%d",&num_elem);
for(i=0;i<num_elem;i++)
{
printf("nenter the element you want to push inside the
stack") ;
scanf("%d",&item);
push(item);
}
printf("ndisplay elements");
display();
printf("ndelete an element from stack");
pop();
printf("nDisplay after deleting an element form stack");
display();
return 0;
}