Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

PRAKHAR BHATNAGAR 17CSU139

STACKS IMPLEMENTATION OF LINKED LISTS

CODE:

#include<stdio.h>

#include<stdlib.h>

struct Node

int data;

struct Node *nxt;

};

struct Node *top=NULL;

void push(int x)

struct Node *ptr;

ptr = (struct Node*)malloc(sizeof(struct Node));

printf("Enter the data to be inserted: ");

scanf("%d", &x);

ptr->data = x;

if(top == NULL)

ptr->nxt = NULL;

else

ptr->nxt = top;

top = ptr;

void pop();

void display();
int c, x;

void main()

printf("Stacks implementation of linked list \t");

while(1){

printf("\n1. Push\n2. Pop\n3. Display\n10. Exit\n");

printf("Enter your choice: ");

scanf("%d",&c);

switch(c){

case 1: push(x);

break;

case 2: pop();

break;

case 3: display();

break;

case 10:

printf("Exiting the program is 3...2..1.");

break;

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

void pop()

struct Node *ptr;

if(top == NULL)
printf("\nEmpty stack");

else{

ptr= top;

printf("\nDeleted element: %d \n", ptr->data);

top = ptr->nxt;

free(ptr);

void display()

struct Node *ptr ;

if(top == NULL)

printf("\nEmpty stack");

else{

ptr= top;

while(ptr->nxt != NULL){

printf("%d\t",ptr->data);

ptr = ptr-> nxt;

printf("%d \t",ptr->data);

OUTPUT :

Stacks implementation of linked list

1. Push

2. Pop

3. Display

10. Exit
Enter your choice: 1

Enter the data to be inserted: 123

1. Push

2. Pop

3. Display

10. Exit

Enter your choice: 1

Enter the data to be inserted: 124

1. Push

2. Pop

3. Display

10. Exit

Enter your choice: 1

Enter the data to be inserted: 125

1. Push

2. Pop

3. Display

10. Exit

Enter your choice: 2

Deleted element: 125

1. Push

2. Pop

3. Display
10. Exit

Enter your choice: 3

124 123

1. Push

2. Pop

3. Display

10. Exit

Enter your choice: 10

Exiting the program is 3...2..1.

You might also like