//Stack Operation Using Linked List Implementation.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<alloc.h>
void push();
void pop();
void display();
typedef struct stack {
		      int data;
		      struct stack *link;
		      }*node;
node top='\0',p,q,head;
int item;
main()
{
	int ch;
	char choice='y';
	clrscr();
	while(choice=='y')
	{
//		clrscr();
		printf(" \n\n\n\t\t  S T A C K   O P E R A T I O N\n");
		printf("\t\t\t  1.P U S H\n");
		printf("\t\t\t  2.P O P\n");
		printf("\t\t\t  3.E X I T\n");
		printf("Enter Your choice:");
		scanf("%d",&ch);
		switch(ch)
			{
			case 1:
				push();
				break;
			case 2:
				pop();
				break;
			case 3:
				exit(0);
			default:
				printf("Invalid Choice\n Retry:");
				break;
			}
		display();
		printf("\nDo you Want To Continue:");
		scanf("%c",&choice);
	choice=getchar();
	}
getch();
}

void push()
{
	printf("Enter The Data To Be Pushed:");
	scanf("%d",&item);
	q=(node*)malloc(sizeof(node));
	if(top==NULL)
	   head=q;
	q->data=item;
	q->link=NULL;
	top->link=q;
	top=q;
	return;
}
void pop()
{

	q=top;
	if(top==NULL)
	{
		printf("Stack Underflow\n");
		return;
	}
	item=q->data;
	if(head==top)
		{
		printf("The Popped Element IS:%d",item);
		head=NULL;
		return;
		}
	p=head;

	while(p->link->link!=NULL)
		p=p->link;
	top=p;
	top->link=NULL;
	printf("The Popped Element IS:%d",item);

	free(q);
	return;
}
void display()
{
	q=head;
	printf("\n Contents Of  The Stack:\n");
	while(q!=NULL)
	{
		printf("%d\t",q->data);
		q=q->link;
	}
	return;
}
