Implementation of Stack operations using Linked List lab program

 #include<stdio.h>

#include<stdlib.h>

struct node

{

int data;

struct node *link;

}*top = NULL;

void push();

void pop();

void peek();

void display();

int main()

{

int n;

printf("Stack Menu:-\n\n1.Push\n2.Pop\n3.Peek\n4.Display\n5.Exit\n\n");

while(1)

{

printf("Enter your choice\t");

scanf("%d",&n);

switch(n)

{

case 1: push(); break;

case 2: pop(); break;

case 3: peek(); break;

case 4: display(); break;

case 5: exit(1);

default: printf("Please enter a valid choice\n");

};

}

return 0;

}

void push()

{

int n;

struct node *temp;

temp = (struct node *)malloc(sizeof(struct node));

printf("Enter value\t");

scanf("%d",&n);

temp->data = n;

temp->link = NULL;

if(top == NULL)

top = temp;

else

{

temp->link = top;

top = temp;

}

}

void pop()

{

if(top == NULL)

printf("Underflow - No elements to delete\n");

else

{

printf("Top element %d is deleted\n",top->data);

top = top->link;

}

}

void peek()

{

if(top == NULL)

printf("Empty Stack\n");

else

printf("Top element = %d\n",top->data);

}

void display()

{

if(top == NULL)

printf("No elements to display\n");

else

{

struct node *temp=top;

for (temp = top ; temp!= NULL ; temp = temp->link)

printf("%d  ",temp->data);

printf("\n");

}

}


Comments

Popular posts from this blog

Data Structures UNIT-1 Chapter-1

Unit-2 chapter-1 STACKS AND ITS APPLICTIONS

Conversion of Infix to Postfix Notation lab program