Linked Stack

/*linked stack*/

#include <stdio.h>
#include <conio.h>

struct node
{
int data;
struct node *next;
}*list,*p;

void getnode()
{
p=(struct node *) malloc (sizeof (struct node));
}

void main()
{
void getnode();
int a,b,d,n;
static int i=0;
char c;
struct node *q;
clrscr();
do
{
printf ("\nDo u want to:");
printf ("\n1.Push an element in linked stack.");
printf ("\n2.Pop an element from linked stack.\n");
printf ("3.Display linked stack.\n");
scanf ("%d",&a);
switch (a)
{
 case 1:
	{
	 printf ("Enter element to Push");
	 scanf ("%d",&b);
	 if (i==0)
	 {
	 getnode();
	 p->data=b;
	 p->next=NULL;
	 list=p;
	 i++;
	 }
	 else
	 {
	 getnode();
	 p->data=b;
	 p->next=list;
	 list=p;
	 i++;
	 }
	 break;
	}
  case 2:
	{
	if (i==0)
	 {
	  printf ("linked stack is empty!!!");
	  break;
	 }
	else
	 {
	 n=list->data;
	 p=list;
	 list=p->next;
	 free(p);
	 printf ("\nThe element %d is poped",n);
	 i--;
	 }
	 break;
	}
  case 3:
	if (i==0)
	 {
	  printf ("linked stack is empty!!!");
	  break;
	 }
	else
	{
	cprintf ("\n Do you want to:");
	printf ("\n1.Display stack elements only");
	printf ("\n2.Display stack elements as nodes\n");
	scanf ("%d",&d);
	p=list;
	switch (d)
	 {
	  case 1:
		{
		 for (a=0;a<i;a++)
		 {
		  printf ("\n| %d |",p->data);
		  p=p->next;
		 }
		 break;
		}
	  case 2:
		{
		 for (a=0;a<i;a++)
		 {
		  printf ("| %d | %x |  ",p->data,p->next);
		  p=p->next;
		 }
		 break;
		}
	  default:
		  {
		  printf ("Invalid input");
		  break;
		  }
	 }
	 break;
       }
  default: {
	    printf ("Invalid input");
	    break;
	   }
}
 printf ("\ndo u want 2 continue?");
 c=getche();
}
while (c=='y' || c=='Y');
getch();
}

OUTPUT

Do u want to:
1.Push an element in linked stack.
2.Pop an element from linked stack.
3.Display linked stack.
1
Enter element to Push:1

do u want 2 continue?y
Do u want to:
1.Push an element in linked stack.
2.Pop an element from linked stack.
3.Display linked stack.
1
Enter element to Push:2

do u want 2 continue?y
Do u want to:
1.Push an element in linked stack.
2.Pop an element from linked stack.
3.Display linked stack.
1
Enter element to Push:3

do u want 2 continue?y
Do u want to:
1.Push an element in linked stack.
2.Pop an element from linked stack.
3.Display linked stack.
3

Do you want to:
1.Display stack elements only
2.Display stack elements as nodes
1

| 3 |
| 2 |
| 1 |
do u want 2 continue?y
Do u want to:
1.Push an element in linked stack.
2.Pop an element from linked stack.
3.Display linked stack.
3

 Do you want to:
1.Display stack elements only
2.Display stack elements as nodes
2
| 3 | 8aa |  | 2 | 8a2 |  | 1 | 0 |
do u want 2 continue?y
Do u want to:
1.Push an element in linked stack.
2.Pop an element from linked stack.
3.Display linked stack.
2

The element 3 is poped

do u want 2 continue?y
Do u want to:
1.Push an element in linked stack.
2.Pop an element from linked stack.
3.Display linked stack.
2

The element 2 is poped

do u want 2 continue?y
Do u want to:
1.Push an element in linked stack.
2.Pop an element from linked stack.
3.Display linked stack.
2

The element 1 is poped

do u want 2 continue?y
Do u want to:
1.Push an element in linked stack.
2.Pop an element from linked stack.
3.Display linked stack.
2

linked stack is empty!!!

do u want 2 continue?n

Linked stack program with sample output
Title: Linked Stack C Program (238 clicks)
Caption: Linked stack program with sample output
Filename: linkedstack.zip
Size: 6 KB