Search more programs

C Program for implementation of stack using Linked List.

".C" file is available in downloadable format at :



(Open the link and click on the download arrow on the top right to download the file.)


Given Below is the code for implementation of stack using Linked List:



---------------------------------------------------------------------------------

#include<stdio.h>
#include<stdlib.h>

typedef struct ll
{
int info;
struct ll* next;
}node;

node* top; 

void push()
 {
 node* n;
 int ele;
 printf("\nEnter the ele to be inserted:\t");
 scanf("%d", &ele);
 n=(node*)malloc(sizeof(node));
 n->info=ele;
 n->next=top;
 top=n;
 }


int pop()
{
int data;
if(top==NULL)
 {
 printf("\nStack Underflow");
 return -1;
 }
else
 {
 data = top->info;
 printf("\nElement output: %d", data);
 top=top->next;
 return data;
 }
}


void display ()
{
node* p=top;

while(p!=NULL)
 {
 printf(" %d ", p->info);
 p=p->next;
 }
 printf("\n");
}

void main ()
{
int a, disp;

while(1)
 {
 printf("\n\n1. Push\n2. Pop\n3. Display.\n4. Exit.\n\nYour Choice:\t");
 scanf("%d", &a);

   switch (a)
   {
   case 1: push(); break;
   case 2: disp = pop(); break;
   case 3: display (); break;
   case 4: exit(0);
   default: printf("\nHey, wrong choice\n");   break;
   }
 }
}
-------------------------------------------------------------------------------

No comments:

Post a Comment