Tuesday, 4 November 2014

program for stack using linked list...........

#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
}*start;

void push(int x);
void pop();
void display();

int main()
{

  int a, dt;
   start = NULL;
   while(1)
     {
      printf("\n 1 for push");
      printf("\n 2 for pop");
      printf("\n 3 for display");
      printf("\n 4 for exit");
      printf("\n Enter your choice");
      scanf("%d",&a);
      switch(a)
      {
      case 1: printf("\n Enter element to satck");
             scanf("%d",&dt);
             push(dt);
             break;
           
          case 2: pop();    
                 break;
               
          case 3: display();
       break;

//case 4: exit(0);
 
      }
     
     }

return 0;
}

void push(int d)
{
struct node *data, *tmp;

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

data->info = d;

data->link = NULL;
 
    if(start == NULL)
    start = data;
    else if(start->link == NULL)
    start->link = data;
    else
          {
             data->link = start;
             start = data;
         }
       
     
     
}

void pop()
{
struct node  *tmp;
if(start== NULL)
printf("stack is empty.....");
else
{
      tmp = start;
     start= tmp->link;
     free(tmp);
 }
}

void display()
{
struct node  *tmp=start;
if(start== NULL)
printf("stack is empty.....");
else
{  
     
 while(tmp != NULL)
    {
     printf("%3d",tmp->info);
     tmp=tmp->link;
}


}
}





No comments:

Post a Comment

Note: only a member of this blog may post a comment.