0% found this document useful (0 votes)
2 views

Stack in Linked List

Uploaded by

SSPriya SSPriya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Stack in Linked List

Uploaded by

SSPriya SSPriya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Stack in linked list

#include <stdio.h>

#include <stdlib.h>

// Define the structure for a Node

struct Node {

int data;

struct Node* next;

};

// Function to push an element onto the stack

struct Node* push(struct Node* top, int value) {

// Create a new node

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

if (newNode == NULL) {

printf("Stack Overflow\n");

return top;

// Assign data to the new node and make it the new top

newNode->data = value;

newNode->next = top;

printf("%d pushed onto stack\n", value);

return newNode; // Return the new top node

// Function to pop an element from the stack

struct Node* pop(struct Node* top) {

if (top == NULL) {

printf("Stack Underflow\n");
return NULL;

// Store the top element and move the top pointer

struct Node* temp = top;

printf("Popped element: %d\n", top->data);

top = top->next;

free(temp); // Free the memory of the popped node

return top; // Return the new top node

// Function to peek at the top element of the stack

int peek(struct Node* top) {

if (top == NULL) {

printf("Stack is Empty\n");

return -1;

return top->data;

// Function to check if the stack is empty

int isEmpty(struct Node* top) {

return top == NULL;

// Main function to take input from the user and perform stack operations

int main() {

struct Node* stack = NULL; // Initialize an empty stack

int choice, value;

while (1) {
// Display menu options

printf("\n--- Stack Operations ---\n");

printf("1. Push\n");

printf("2. Pop\n");

printf("3. Peek\n");

printf("4. Check if Stack is Empty\n");

printf("5. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value to push: ");

scanf("%d", &value);

stack = push(stack, value);

break;

case 2:

stack = pop(stack);

break;

case 3:

value = peek(stack);

if (value != -1) {

printf("Top element is: %d\n", value);

break;

case 4:

if (isEmpty(stack)) {

printf("Stack is empty\n");

} else {

printf("Stack is not empty\n");

}
break;

case 5:

printf("Exiting...\n");

exit(0);

default:

printf("Invalid choice, please try again.\n");

return 0;

Output

--- Stack Operations ---

1. Push

2. Pop

3. Peek

4. Check if Stack is Empty

5. Exit

Enter your choice: 1

Enter value to push: 10

10 pushed onto stack

--- Stack Operations ---

1. Push

2. Pop

3. Peek

4. Check if Stack is Empty

5. Exit

Enter your choice: 1

Enter value to push: 20

20 pushed onto stack


--- Stack Operations ---

1. Push

2. Pop

3. Peek

4. Check if Stack is Empty

5. Exit

Enter your choice: 3

Top element is: 20

--- Stack Operations ---

1. Push

2. Pop

3. Peek

4. Check if Stack is Empty

5. Exit

Enter your choice: 2

Popped element: 20

--- Stack Operations ---

1. Push

2. Pop

3. Peek

4. Check if Stack is Empty

5. Exit

Enter your choice: 4

Stack is not empty

Pseudo Code
Node Structure
STRUCT Node:

data // stores the value of the node


next // stores the address of the next node

Push Operation

FUNCTION Push(top, value):

newNode ← Create a new Node

IF newNode is NULL:

PRINT "Stack Overflow"

RETURN top

END IF

newNode.data ← value

newNode.next ← top

PRINT value, " pushed onto stack"

RETURN newNode // Return the updated top node

END FUNCTION

Pop Operation
FUNCTION Pop(top):

IF top is NULL:

PRINT "Stack Underflow"

RETURN NULL

END IF

temp ← top

PRINT "Popped element: ", top.data

top ← top.next

Free memory of temp

RETURN top // Return the updated top node

END FUNCTION

Peek Operation
FUNCTION Peek(top):

IF top is NULL:

PRINT "Stack is Empty"

RETURN -1
END IF

RETURN top.data

END FUNCTION

IsEmpty Operation
FUNCTION IsEmpty(top):

RETURN (top = NULL)

END FUNCTION

Main Program (User Input and Menu)

BEGIN

Initialize stack ← NULL

WHILE True:

PRINT "--- Stack Operations ---"

PRINT "1. Push"

PRINT "2. Pop"

PRINT "3. Peek"

PRINT "4. Check if Stack is Empty"

PRINT "5. Exit"

INPUT choice

IF choice = 1:

PRINT "Enter value to push: "

INPUT value

stack ← Push(stack, value)

ELSE IF choice = 2:

stack ← Pop(stack)

ELSE IF choice = 3:

value ← Peek(stack)
IF value ≠ -1:

PRINT "Top element is: ", value

ELSE IF choice = 4:

IF IsEmpty(stack):

PRINT "Stack is empty"

ELSE:

PRINT "Stack is not empty"

ELSE IF choice = 5:

PRINT "Exiting..."

EXIT PROGRAM

ELSE:

PRINT "Invalid choice, please try again."

END WHILE

END

You might also like