Monday, April 18, 2011

Stack static implementation program by Sweta Mam

/* Stack static implementation program by Sweta Mam */

#include"conio.h"
#include"stdio.h"
#include"stdlib.h"

#define MAX 5

struct stack
{
int info[MAX];
int top;
} stk;

int main(void)
{
int n, ch;
stk.top = -1;
clrscr();
while(1)
{
clrscr();
puts("\n\n1. Push\n2. Pop\n3. Display\n4. Exit.");
printf("Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter info : ");
scanf("%d",&n);
push(&stk, n);
break;
case 2: n = pop(&stk);
if(n!=-1)
{
printf("Popped : %d",n);
getch();
}
break;
case 3: disp(&stk);
break;
case 4: exit(0);
default: puts("\nInvalid Input.\nPlease enter 1-4.");
getch();
}
}
}
push(struct stack *s, int n)
{
if(s->top == MAX-1)
{
puts("Stack overflow.");
getch();
return;
}
s->info[++s->top] = n;
}

pop(struct stack *s)
{
int n;
if(s->top == -1)
{
printf("Stack underflow.");
getch();
return;
}
n = s->info[s->top--];
return n;
}

disp(struct stack *s)
{
int i;
clrscr();
printf("\nStacks are : \n");
if(s->top!= -1)
{
for(i=s->top; i>=0; i--)
printf("\n\t%d",s->info[i]);
}
else
printf("No stacks created yet.");
getch();
}

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites