0% found this document useful (0 votes)
2 views2 pages

Stack Operations Using Linked List

The document provides an implementation of stack operations using a linked list in C. It includes functions for pushing, popping, peeking, and displaying elements in the stack, as well as a check for whether the stack is empty. The main function presents a menu for user interaction with these stack operations.

Uploaded by

25071a6601
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Stack Operations Using Linked List

The document provides an implementation of stack operations using a linked list in C. It includes functions for pushing, popping, peeking, and displaying elements in the stack, as well as a check for whether the stack is empty. The main function presents a menu for user interaction with these stack operations.

Uploaded by

25071a6601
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

//implementation of stack operations using linked list int num;

#include <stdio.h> temp =(NODE*)malloc(1*sizeof(NODE));

#include <stdlib.h> printf("\n enter an element");

struct node scanf("%d",&num);

{ temp->data = num;

int data; if (top == NULL)

struct node *link; {

}; top = temp;

typedef struct node NODE; top->link = NULL;

NODE *top=NULL; }

/* Checks if Stack is empty or not */ else

int isEmpty() {

{ temp->link = top;

if (top == NULL) top = temp;

return 1; }

else }

return 0; /*Pop Operation: Removes Top Element of the Stack */

} void pop()

/* Returns the top element of Stack */ {

int peek() NODE *temp;

{ if (isEmpty(top))

printf("\n the top of the stack is %d",top->data); {

return; printf("\nStack is Empty\n");

} return;

/* Push an Element in Stack */ }

void push() else

{ {

NODE *temp; temp = top;


top = top->link; printf("\n 1. push");

printf("Removed Element : %d\n", temp->data); printf("\n 2. pop");

free(temp); printf("\n 3. peek");

} printf("\n 4. Display");

} printf("\n 5. Exit");

/* Prints the linked list representation of a stack */ while(choice!=5)

void display() {

{ printf("\nEnter Your choice : ");

NODE *temp; scanf("%d",&choice);

if(top==NULL) switch(choice)

printf("\n stack is empty"); {

else case 1: push();break;

{ case 2: pop();break;

temp=top; case 3: peek();break;

while (temp != NULL) case 4: display();break;

{ case 5: exit(0);

printf("%d ", temp->data); default: printf("\n wrong choice");

temp = temp->link; }

if(temp != NULL) }

printf("->"); return 0;

} }

printf("\n");

void main()

int choice;

printf("\n stack using linked list operations");

You might also like