Search This Blog

Tuesday 27 October 2015

stack using linked list

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
 int info;
 struct node*next;
};
struct node *top=NULL;
void push(int item)
{
 struct node *p;
 p=(struct node*)malloc(sizeof(struct node));
 p->info=item;
 p->next=top;
 top=p;
 printf("\ninserted element %d",p->info);
}
void pop()
{
 struct node *p;
 p=top;
 if(p==NULL)
 printf("\nempty");
 else
 {
  top=top->next;
  printf("\nelement deleted %d",p->info);
  free(p);
 }
}
void display()
{
 struct node *p;
 p=top;
 if(p==NULL)
 printf("\nempty");
 else
 {
  while(p!=NULL)
  {
   printf("\n%d",p->info);
   p=p->next;
  }
 }
}
void main()
{
 int item,ch;
 clrscr();
 printf("\nMENIU OPTIONS\n1.push\n2.pop\n3.display\n4.exit");
 do
 {
  printf("\nenter your choice");
  scanf("%d",&ch);
  switch(ch)
  {
   case 1:printf("\nenter the element to push");
                  scanf("%d",&item);
                  push(item);
                  break;
   case 2:pop();
                  break;
   case 3:display();
                  break;
   case 4:exit(0);
   default:printf("\ninvalid choice......");
  }
 }while(ch!=4);
 getch();

}

No comments:

Post a Comment