Wednesday, 5 December 2012

program for the stack using linked list

// program for the stack using linked list//

#include<stdio.h>

#include<conio.h>

struct node

{

int data;

struct node *next;

}*top=NULL;

void push(int val)

{

struct node *new1;

new1=(struct node *) malloc (sizeof(struct node));

 

if(new1==NULL)

{

printf("\n stack overflow");

return;

}

new1->data=val;

new1->next=top;

top=new1;

}

 

int pop()

{

struct node *curr;

int temp;

if(top==NULL)

{

printf("\n stack underflow");

return 0;

}

curr=top;

top=top->next;

temp=curr->data;

free(curr);

return temp;

}

void main()

{

int ch,val;

clrscr();

do

{

printf("\n!!-----Menu-----!!");

printf("\n   1. push  ");

printf("\n   2. pop   ");

printf("\n   3. exit  ");

printf("\n-------------------");

printf("\n enter your choice");

scanf("%d",&ch);

if(ch==1)

{

printf("\n enter any value ");

scanf("%d",&val);

push(val);

}

else if(ch==2)

{

val=pop();

printf("\n item removed %d",val);

}

else if(ch==3)

{

printf("\n good bye");

}

else

{

printf("\n invalid choice");

}

}while(ch!=3);

getch();

}

No comments:

Post a Comment