Saturday, May 9, 2009

PROGRAM FOR IMPLEMENTING STACK USING LINKED LIST

#include
#include
#include
#define NULL 0
struct stack
{
int info;
struct stack *next;
};
typedef struct stack node;
node *first,*list,*root;
void push();
void pop();
void print();
void main()
{
int ch;
root=NULL;
do
{
printf("\n 1->push");
printf("\n 2->pop");
printf("\n 3->print");
printf("\n 4->exit");
printf("\n Enter ur choice");
scanf("%d",&ch);
switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: print();
break;
case 4: exit(0);
break;
}
}while(ch!=4);
}
void push()
{
first=(node *)malloc(sizeof(node));
printf("Enter element");
scanf("%d",&first->info);
first->next=root;
root=first;
}
void pop()
{
if(root==NULL)
printf("\n List is empty\n");
else
{
printf("The popped element is %d\n",root->info);
root=root->next;
}
}
void print()
{
list=root;
if(list==NULL)
printf("\nList is empty\n");
else
{
printf("\n The stack elements are");
while(list!=NULL)
{
printf("\n %d",list->info);
list=list->next;
}
}
}

No comments:

Post a Comment