Er. Ganesh Ram Suwal
/***** STACK IMPLEMENTATION USING SINGLY LINKED LIST *****/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <alloc.h>
/* Global Variable */
int data;
/* Function Prototype */
void push();
void pop();
void display();
/* Structure For Node */
struct node
{
int info;
struct node *pnext;
};
struct node *pfirst,*pnew,*pthis;
void main()
{
clrscr();
int choice = 0;
printf("n********* Stack Operations using Linked List *********n");
printf("---------------------------------------------------------------------");
while(choice != 4)
{
printf("n1.Pushn2.Popn3.Shown4.Exit");
printf("nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:printf("Exiting....");
break;
default:printf("nSORRY !!! Please Enter valid choice ");
} // End of switch Statement
} // End of while loop
} // End main function
/* PUSH function */
void push()
{
printf("n*********************************************************n");
pnew = (struct node*)malloc(sizeof(struct node));
Er. Ganesh Ram Suwal
if(pnew == NULL)
{
printf("SORRY ! Unable to PUSH element");
}
else
{
printf("Enter data : ");
scanf("%d",&data);
pnew->info = data;
if(pfirst == NULL)
{
pnew->pnext = NULL;
pfirst = pnew;
}
else
{
pnew->pnext = pfirst;
pfirst = pnew;
}
printf("The PUSHED element = %d",pfirst->info);
}
printf("n*********************************************************n");
}
/* POP Function */
void pop()
{
printf("n*********************************************************n");
if (pfirst == NULL)
{
printf(" SORRY there is no data in STACK (STACK UNDERFLOW)");
}
else
{
data = pfirst->info;
pthis = pfirst;
pfirst = pfirst->pnext;
free(pthis);
printf("The POPED element = %d",data);
}
printf("n*********************************************************n");
}
void display()
{
int i;
printf("n*********************************************************n");
if(pfirst == NULL)
{
printf(" Stack is empty ");
}
else
{
pthis = pfirst;
Er. Ganesh Ram Suwal
printf("The element in the STACK: ");
while(pthis != NULL)
{
printf("%d ",pthis->info);
pthis = pthis->pnext;
}
}
printf("n*********************************************************n");
}
Er. Ganesh Ram Suwal
Er. Ganesh Ram Suwal

STACK IMPLEMENTATION USING SINGLY LINKED LIST

  • 1.
    Er. Ganesh RamSuwal /***** STACK IMPLEMENTATION USING SINGLY LINKED LIST *****/ #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <alloc.h> /* Global Variable */ int data; /* Function Prototype */ void push(); void pop(); void display(); /* Structure For Node */ struct node { int info; struct node *pnext; }; struct node *pfirst,*pnew,*pthis; void main() { clrscr(); int choice = 0; printf("n********* Stack Operations using Linked List *********n"); printf("---------------------------------------------------------------------"); while(choice != 4) { printf("n1.Pushn2.Popn3.Shown4.Exit"); printf("nEnter your choice : "); scanf("%d",&choice); switch(choice) { case 1:push(); break; case 2:pop(); break; case 3:display(); break; case 4:printf("Exiting...."); break; default:printf("nSORRY !!! Please Enter valid choice "); } // End of switch Statement } // End of while loop } // End main function /* PUSH function */ void push() { printf("n*********************************************************n"); pnew = (struct node*)malloc(sizeof(struct node));
  • 2.
    Er. Ganesh RamSuwal if(pnew == NULL) { printf("SORRY ! Unable to PUSH element"); } else { printf("Enter data : "); scanf("%d",&data); pnew->info = data; if(pfirst == NULL) { pnew->pnext = NULL; pfirst = pnew; } else { pnew->pnext = pfirst; pfirst = pnew; } printf("The PUSHED element = %d",pfirst->info); } printf("n*********************************************************n"); } /* POP Function */ void pop() { printf("n*********************************************************n"); if (pfirst == NULL) { printf(" SORRY there is no data in STACK (STACK UNDERFLOW)"); } else { data = pfirst->info; pthis = pfirst; pfirst = pfirst->pnext; free(pthis); printf("The POPED element = %d",data); } printf("n*********************************************************n"); } void display() { int i; printf("n*********************************************************n"); if(pfirst == NULL) { printf(" Stack is empty "); } else { pthis = pfirst;
  • 3.
    Er. Ganesh RamSuwal printf("The element in the STACK: "); while(pthis != NULL) { printf("%d ",pthis->info); pthis = pthis->pnext; } } printf("n*********************************************************n"); }
  • 4.
  • 5.