Data Structure
Definition: -
                                        
1.         
An array stack [MAX],
where MAX is the maximum size of the array.
                                        
2.         
Initialization
top = -1
Source Code:
/*******Program of stack using array *************************/
- #include<stdio.h>
 - #define size 5
 - int stack[size];
 - int tos= -1;
 - void push();
 - void pop();
 - void display();
 - int main()
 - {
 - int ch,c;
 - do
 - {
 - printf("\n1. push\n 2.pop \n3. exit\n Enter your choice:\t");
 - scanf("%d",&ch);
 - switch(ch)
 - {
 - case 1:
 - push();
 - printf("\nstack:\t");
 - display();
 - break;
 - case 2:
 - pop();
 - printf("\nstack:\t");
 - display();
 - break;
 - case 3:
 - break;
 - default:
 - printf(" invalid choice!");
 - }
 - printf("\nreopen the series:?press 1:");
 - scanf("%d",&c);
 - } while(c==1);
 - return 0;
 - }
 - void push()
 - {
 - int data;
 - if (tos,(size-1))
 - { tos=tos+1;
 - printf("\n Enter data:");
 - scanf("%d",&data);
 - stack[tos]=data;
 - if(tos==(size-1))
 - printf("\nstack is full");
 - }
 - else
 - printf("\n overflow");
 - }
 - void pop()
 - {
 - int data;
 - if (tos==-1)
 - printf("\n underflow");
 - else
 - {
 - data=stack[tos];
 - printf("\n popped=%d",data);
 - tos=tos-1;
 - }
 - }
 - void display()
 - {
 - int i;
 - if(tos==-1)
 - printf("stack is empty.");
 - else
 - {
 - for(i=tos;i>=0;i--)
 - {
 - printf("\n%d",stack[i]);
 - }
 - }
 - }
 
/********** End of Program ********************
OUTPUT:
1. push
 2.pop
3. exit
 Enter your
choice:     1
 Enter data:12
stack:
12
reopen the series:?press 1:1
1. push
 2.pop
3. exit
 Enter your
choice:     1
 Enter data:23
stack:
23
12
reopen the series:?press 1:1
1. push
 2.pop
3. exit
 Enter your
choice:     1
 Enter data:34
stack:
34
23
12
reopen the series:?press 1:1
1. push
 2.pop
3. exit
 Enter your
choice:     2
 popped=34
stack:
23
12
reopen the series:?press 1:1
1. push
 2.pop
3. exit
 Enter your
choice:     2
 popped=23
stack:
12
reopen the series:?press 1:1
1. push
 2.pop
3. exit
 Enter your
choice:     3
0 comments:
Post a Comment