Stack - Coder's point

Saturday, April 6, 2019

Stack

 #include<stdio.h>  
 #include<conio.h>  
 #define MAX 11  
 struct stack  
 {  
 int arr[MAX];  
 int top;  
 };  
 void initstack(struct stack*);  
 void push(struct stack*,int);  
 int pop(struct stack*);  
 void main()  
 {  
 int i;  
 struct stack s;  
 clrscr();  
 initstack(&s);  
 push(&s,11);  
 push(&s,67);  
 push(&s,57);  
 push(&s,-7);  
 push(&s,24);  
 push(&s,12);  
 push(&s,63);  
 push(&s,13);  
 push(&s,98);  
 push(&s,23);  
 push(&s,97);  
 i=pop(&s);  
 printf("\nitem popped:%d",i);  
 i=pop(&s);  
 printf("\nitem popped:%d",i);  
 i=pop(&s);  
 printf("\nitem popped:%d",i);  
 i=pop(&s);  
 printf("\nitem popped:%d",i);  
 i=pop(&s);  
 printf("\nitem popped:%d",i);  
 getch();  
 }  
 void initstack(struct stack *s)  
 {  
 s->top=-1;  
 }  
 void push(struct stack *s,int item)  
 {  
 if(s->top==MAX-1)  
 {  
 printf("\nstack is full");  
 return;  
 }  
 s->top++;  
 s->arr[s->top]=item;  
 }  
 int pop(struct stack *s)  
 {  
 int data;  
 if(s->top==-1)  
 {  
 printf("\nstack is empty");  
 return NULL;  
 }  
 data=s->arr[s->top];  
 s->top--;  
 return data;  
 }  

OUTPUT:
ELEMENTS IN A DEQUE:                                                                                                            
FRONT-> 56      -25     19      18      21      88      -12     23      167                                                  1  
2       15      -6<-REAR                                                                                                        
TOTAL NUMBER OF ELEMENTS IN DEQUE:12                                                                                            
ITEM EXTRACTED:56                                                                                                               
ITEM EXTRACTED:-25                                                                                                              
ITEM EXTRACTED:19                                                                                                               
ELEMENTS IN DEQUE AFTER DELETION:                                                                                               
FRONT-> 0       0       0       18      21      88      -12     23      167                                                  1  
2       15      -6<-REAR                                                                                                        
ELEMENTS IN DEQUE AFTER ADDITION:                                                                                               
FRONT-> 0       11      18      21      88      -12     23      167     12                                                   1  
5       -6      23<-REAR                                                                                                        
ITEM EXTRACTED:23                                                                                                               
ITEM EXTRACTED:-6                                                                                                               
ELEMENTS IN DEQUE AFTER DELETION:                                                                                               
FRONT-> 0       11      18      21      88      -12     23      167     12                                                   1  
5       0       0<-REAR                                                                                                         
TOTAL NUMBER OF ELEMENTS IN DEQUE:9

No comments:

Post a Comment