STACK USING LINK LIST
#include
#include
#include
#include
struct node{
int data;
struct node *next;
};
void main()
{
struct node *head=NULL,*ptr=NULL,*temp=NULL,*top,*ptr2,*ptr3,*ptr4=NULL;
int choice;
clrscr();
while(1)
{
printf("--------------------program of stack using link list-------------\n");
printf("press 1 for insertion\npress 2 for deletion\npress 3 for view the stack\npress 4 for source code\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
temp=(struct node *)malloc(sizeof(struct node));
temp->next=NULL;
printf("\nenter the data in temp variable");
scanf("%d",&temp->data);
if(temp==NULL)
{
printf("stack is overflow\n");
}
else if(head==NULL)
{
head=temp;
top=temp;
}
else
{
ptr=head;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=temp;
top=temp;
}
break;
case 2:
if(temp==NULL)
{
printf("stack is underflow\n");
}
else
{
ptr2=head;
while(ptr2->next!=NULL)
{
ptr4=ptr2;
ptr2=ptr2->next;
}
if(ptr2==head)
{ head=NULL;
free(ptr2);
}
else
{
printf("deleted item in stack is =%d",ptr2->data);
ptr4->next=NULL;
free(ptr2);
}}
break;
case 3:
printf("remaining number in stack is =\n");
ptr3=head;
if(head==NULL)
{
printf("stack is empty\n");
}
else{
while(ptr3!=NULL)
{
printf("%d ",ptr3->data);
ptr3=ptr3->next;
}
}
break;
case 4:
exit(0);
break;
default :
printf("you have entered wrong choice\n");
break;
}
}
getch();
}
x
#include
#include
#include
struct node{
int data;
struct node *next;
};
void main()
{
struct node *head=NULL,*ptr=NULL,*temp=NULL,*top,*ptr2,*ptr3,*ptr4=NULL;
int choice;
clrscr();
while(1)
{
printf("--------------------program of stack using link list-------------\n");
printf("press 1 for insertion\npress 2 for deletion\npress 3 for view the stack\npress 4 for source code\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
temp=(struct node *)malloc(sizeof(struct node));
temp->next=NULL;
printf("\nenter the data in temp variable");
scanf("%d",&temp->data);
if(temp==NULL)
{
printf("stack is overflow\n");
}
else if(head==NULL)
{
head=temp;
top=temp;
}
else
{
ptr=head;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=temp;
top=temp;
}
break;
case 2:
if(temp==NULL)
{
printf("stack is underflow\n");
}
else
{
ptr2=head;
while(ptr2->next!=NULL)
{
ptr4=ptr2;
ptr2=ptr2->next;
}
if(ptr2==head)
{ head=NULL;
free(ptr2);
}
else
{
printf("deleted item in stack is =%d",ptr2->data);
ptr4->next=NULL;
free(ptr2);
}}
break;
case 3:
printf("remaining number in stack is =\n");
ptr3=head;
if(head==NULL)
{
printf("stack is empty\n");
}
else{
while(ptr3!=NULL)
{
printf("%d ",ptr3->data);
ptr3=ptr3->next;
}
}
break;
case 4:
exit(0);
break;
default :
printf("you have entered wrong choice\n");
break;
}
}
getch();
}
x