write a program to implementation of stack using linked list in data structure.
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<dos.h>
struct stack
{
int data;
struct steck*next;
};
typedef struct stack STACK;
STACK*top = NULL;
void push()
{
STACK*temp;
if(top = = NULL)
{
top=(STACK*)malloc(sizeof(STACK));
printf("\n enter data : ");
scanf("%d",&top->data);
top->next=NULL;
return;
}
temp=(STACK*)malloc(sizeof(STACK));
printf("\n Enter data: ");
scanf("%d",&temp->data);
temp->next=top;
top=temp;
return;
}
void pop()
{
STACK*temp;
if(top==NULL)
{
printf("stack is inderflow \n");
system("PAUSE");
return;
}
printf("pop element is :%d\n",top->data);
temp=top;
top=top->next;
temp->next=NULL;
free(temp);
system("PAUSE");
return;
}
void peek()
{
STACK*temp;
if(top= = NULL)
{
printf("stack is empty \n");
system("PAUSE");
return;
}
printf("stack elements:");
temp = top;
do
{
printf("d",temp>data);
temp = temp->next;
}while(temp!=NULL);
system("PAUSE");
return;
}
int main(void)
{
int option;
while(1)
{
system("CLS");
printf("\n PUSH PRESS 1: ");
printf("\n PUSH PRESS 2: ");
printf("\n PUSH PRESS 3: ");
printf("\n PUSH PRESS 4: ");
scanf("%d",&option);
switch(option)
{
case 1: push();
break;
case 2: pop();
break;
case 3: peek();
break;
case 4: free(top);
return EXIT_SUCCESS;
default: printf("INVALID OPTION \n");
system("PAUSE");
}
}
}