Wednesday, 9 April 2014

write a programme of stack using linked list.
#include<stdio.h>
#include<stdlib.h>
void display(void);
void push(void);
void pop(void);
struct link_list
{
int data;
struct link_list *next;
}*start=NULL;
typedef struct link_list node;
node *current,*nnew;

int main()
{
int a;
char ch;
do
{
printf("\n enter your choice\n1->push\n2->pop\n3->display\n");
scanf("%d",&a);
switch(a)
{
case 3:
display();
break;
case 2:
       pop();
       break;
case 1:
push();
break;
default:
printf("wrong choice");
}
printf("\n press any key to continue and press n to quit->");
scanf("%s",&ch);
}while(ch!='n');
return (0);
}

void push(void)
{
int i=0,k;
printf("how many elements you want to enter->");
scanf("%d",&i);
while(i!=0)
{
nnew=malloc(sizeof(node));
nnew->next=NULL;
if(start==NULL)
{
printf("enter any integer data->");
scanf("%d",&k);
nnew->data=k;
start=nnew;
current=start;
printf("\t%d pushed successfully\n",start->data);
}
else
{
printf("enter any integer data->");
scanf("%d",&k);
nnew->data=k;
nnew->next=start;
start=nnew;
printf("\t%d pushed successfully\n",start->data);
}
i--;
}
}

void display(void)
{
              current=start;
              printf("\n");
              while(current!=NULL)
              {
              printf("\t%d\n" ,current->data);
              current=current->next;
       }
}
void pop(void)
{
       int count=0;
       printf("how many elements you want to pop->");
       scanf("%d",&count);
       current=start;
       while(count!=0)
       {
       if(start==NULL)
              printf("stack underflow!");
              else
              {
       printf("%d popped successfully\n",start->data);
       current=start;
start=start->next;
free(current);
}
       count--;
}

} 




No comments:

Post a Comment