Monday, April 18, 2011

Stack (Dynamic implemetation) by Sweta Mam

/* Stack Dynamic implemetation by Sweta Mam */

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

struct stack
{
int info;
struct stack *next;
}*front;


int main(void)
{
int n, ch;
while(1)
{
clrscr();
puts("1. Push\n2. Pop\n3. Display\n4. Exit");
puts("Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter info : ");
scanf("%d",&n);
push(front, n);
break;
case 2: n = pop(front);
if(n == -1)
{
printf("\nPopped %d",n);
getch();
}
break;
case 3: disp(front);
break;
case 4: exit(0);

default: puts("Invalid choice.");
getch();
}
}
return 0;
}


/* push function here */

push(struct stack *s, int n)
{
struct stack *temp;
temp = (struct stack *)malloc(sizeof(struct stack));
temp->info = n;
temp->next = s;
front = temp;
}

/* pop function here */

pop(struct stack *s)
{
int n;
if(s==NULL)
{
printf("Stack underflow.");
return -1;
}
n = s->info;
front = s->next;
free(s);
return n;
}

/* display function here */

disp(struct stack *s)
{
clrscr();
if(s!=NULL)
{
printf("\nStack are : \n");

while(s!=NULL)
{
printf("\n\t %d",s->info);
s = s->next;
}
getch();
}
else
{
printf("Stack is empty.");
getch();
}
}

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites