0% found this document useful (0 votes)
17 views5 pages

Algorithm:: Start The Program. Define Node Structure

Uploaded by

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

Algorithm:: Start The Program. Define Node Structure

Uploaded by

lol lol
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

11.Write a program to perform Stack operations using Linked List implementation.

Algorithm:
1. Start the program.
2. Define Node Structure:
• Create a class Node with two members:
• data: stores the value.
• next: a pointer to the next node.
3. Define Stack Class:
• Create a class Stack with the following members:
• top: a pointer to the top of the stack.
• Include the following operations in the class:

• Push: Adds a new node at the top of the stack.


• Pop: Removes the top node from the stack.
• Peek: Displays the value of the top node.
• Display: Prints all the elements in the stack.
• isEmpty: Checks whether the stack is empty.
Detailed Steps for Each Operation:
Push Operation:
1. Start.
2. Create a new node with the given value.
3. If the stack is empty (top is nullptr), set top to point to the new node.
4. If the stack is not empty:
• Set the next pointer of the new node to the current top.
• Update top to point to the new node.
5. End.
Pop Operation:
1. Start.
2. If the stack is empty, display “Stack is empty” and return.
3. If the stack is not empty:
• Save the current top node in a temporary variable.
• Update top to point to the next node.
• Delete the temporary node.
4. End.
Peek Operation:
1. Start.
2. If the stack is empty, display “Stack is empty”.
3. If the stack is not empty, display the value of the node at top.
4. End.
isEmpty Operation:
1. Start.
2. Check if top is nullptr.
3. If top is nullptr, return true (stack is empty), otherwise return false.
4. End.
Display Operation:
1. Start.
2. If the stack is empty, display “Stack is empty”.
3. If the stack is not empty:
• Traverse the linked list starting from top.
• Print each node’s value until reaching nullptr.
4. End.
Main Function:
1. Start.
2. Initialize an empty stack.
3. Display a menu with the following options:
• Push
• Pop
• Peek
• Display
• Exit
4. Depending on the user’s choice, perform the corresponding stack
operation.
5. Repeat until the user selects “Exit”.
6. End the program.

Source code:
#include <iostream>
using namespace std;

class Node {
public:
int data;
Node* next;
Node(int value) {
data = value;
next = NULL;
}
};

class Stack {
private:
Node* top;
public:
Stack() {
top = NULL;
}

void push(int value) {


Node* newNode = new Node(value);
if (top == NULL) {
top = newNode;
} else {
newNode->next = top;
top = newNode;
}
cout << "Pushed " << value << " onto the stack.\n";
}

void pop() {
if (top == NULL) {
cout << "Stack is empty. Cannot pop.\n";
return;
}
Node* temp = top;
top = top->next;
cout << "Popped " << temp->data << " from the stack.\n";
delete temp;
}

void peek() {
if (top == NULL) {
cout << "Stack is empty.\n";
return;
}
cout << "Top element: " << top->data << endl;
}

bool isEmpty() {
return top == NULL;
}

void display() {
if (top == NULL) {
cout << "Stack is empty.\n";
return;
}
Node* temp = top;
cout << "Stack elements: ";
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
};

int main() {
Stack stack;
int choice, value;
while (true) {
cout << "\n--- Stack Menu ---\n";
cout << " 1.Push--";
cout << " 2.Pop--";
cout << " 3.Peek--";
cout << " 4.Display--";
cout << " 5.Exit--\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter value to push: ";
cin >> value;
stack.push(value);
break;
case 2:
stack.pop();
break;
case 3:
stack.peek();
break;
case 4:
stack.display();
break;
case 5:
cout << "Exiting...\n";
return 0;
default:
cout << "Invalid choice. Try again.\n";
}
}
}

Output:
Conclusion:
This C++ program demonstrates the implementation of stack operations using
a linked list. It provides the fundamental stack operations such as push, pop,
peek, and display, along with checking if the stack is empty. By using a linked
list, the stack is dynamic, meaning it can grow and shrink as needed without
being constrained by a fixed size, unlike array-based stacks.

• The push operation adds an element to the top of the stack.


• The pop operation removes and returns the top element.
• The peek operation retrieves the top element without removing it.
• The display operation shows all elements in the stack.

This linked-list-based approach offers flexibility and avoids issues like stack
overflow that may occur with an array-based stack when the array is of limited
size.

You might also like