Wednesday 11 June 2014

stack implementation using Linked List In C Programing

#include<stdio.h>
#include<conio.h>
void push_stack ();
void pop_stack ();
void display_stack ();

typedef struct node
{
int data_stack ;
struct node *link;
}d;
d*top=NULL;


main()
{
int d;
printf("\tMENU\n1.PUSH_STACK \n2.POP_STACK \n3.DISPLAY_STACK \n4.EXIT\n");
do
{
printf("\nEnter your choice\n");
scanf("%d",&d);
switch(d)
{
case 1:
push_stack ();
break;
case 2:
pop_stack ();
break;
case 3:
display_stack ();
break;
case 4:
break;
default:
printf("Invalid choice\n");
break;
}
}
while(d!=4);
}


void push_stack ()
{
int item;
d *temp;
printf("Enter the item\n");
scanf("%d",&item);
temp=(d*)malloc(sizeof(d));
temp->data_stack =item;
temp->link=top;
top=temp;
}

void pop_stack ()
{
d *temp;
if(top==NULL)
printf("Stack is empty\n");
else
{
temp=top;
printf("The element deleted = %d\n",temp->data_stack );
top=top->link;

free(temp);
}
}

void display_stack ()
{
d *save;
if(top==NULL)
printf("Stack is empty\n");
else
{
save=top;
printf("The elements of the stack are :");
while(save!=NULL)
{
printf("%d\t",save->data_stack );
save=save->link;
}
printf("\nTopmost element = %d\n",top->data_stack );
}
}

No comments:

Post a Comment