C++ Stack Exercises: Reverse the elements of a stack (using a linked list)
Write a C++ program to reverse the elements of a stack (using a linked list).
Test Data:
Input some elements onto the stack:
Stack elements are: 0 1 3 5 6
Display the reverse elements of the stack:
Stack elements are: 6 5 3 1 0
Sample Solution:
C++ Code:
#include <iostream>
using namespace std;
// Define the node structure for the linked list
struct Node {
int data;
Node* next;
};
class Stack {
private:
//This variable keeps track of the stack size
int size;
public:
// Top-of-stack pointer
Node* top;
// Constructor to initialize an empty stack
Stack() {
top = NULL; // Initialize top pointer to NULL for an empty stack
size = 0; // Initialize the size of the stack to zero
}
// Function to push an element onto the stack
void push(int x) {
Node* new_Node = new Node(); // Create a new node
new_Node->data = x; // Assign data to the new node
new_Node->next = top; // Point the new node's next to the current top node
top = new_Node; // Update top to the new node
size++; // Increment the size of the stack
}
// Function to pop an element from the stack
void pop() {
if (top == NULL) {
cout << "Stack is empty!" << endl; // Display message if the stack is empty
return;
}
Node* temp = top; // Store the current top node
top = top->next; // Move top to the next node
size--; // Decrement the size of the stack
delete temp; // Delete the previous top node
}
// Function to reverse the elements of the stack
void reverseStack(Node** top) {
if (*top == NULL) {
cout << "Stack is empty." << endl; // Display message if the stack is empty
return;
}
Node* prev = NULL;
Node* curr = *top;
Node* next = NULL;
while (curr != NULL) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
*top = prev; // Update the top pointer with the reversed stack
}
// Function to display the elements of the stack
void display() {
if (top == NULL) {
cout << "Stack is empty!" << endl; // Display message if the stack is empty
return;
}
Node* temp = top; // Create a temporary node to traverse the stack
cout << "Stack elements are: ";
while (temp != NULL) {
cout << temp->data << " "; // Display the data of each node
temp = temp->next; // Move to the next node
}
cout << endl;
}
};
int main() {
Stack stk; // Create an object of Stack class
cout << "Input some elements onto the stack:\n";
stk.push(6);
stk.push(5);
stk.push(3);
stk.push(1);
stk.push(0);
stk.display(); // Display the elements in the stack
cout << "\nDisplay the reverse elements of the stack:\n";
stk.reverseStack(&stk.top); // Reverse the stack elements
stk.display(); // Display the reversed stack elements
cout << "\nInput two more elements:\n";
stk.push(35);
stk.push(45);
stk.display(); // Display the updated elements in the stack
cout << "\nDisplay the reverse elements of the stack:\n";
stk.reverseStack(&stk.top); // Reverse the stack elements again
stk.display(); // Display the reversed stack elements
return 0;
}
Sample Output:
Input some elements onto the stack: Stack elements are: 0 1 3 5 6 Display the reverse elements of the stack: Stack elements are: 6 5 3 1 0 Input two more elements: Stack elements are: 45 35 6 5 3 1 0 Display the reverse elements of the stack: Stack elements are: 0 1 3 5 6 35 45
Flowchart:
CPP Code Editor:
Contribute your code and comments through Disqus.
Previous C++ Exercise: Check the size and empty status of a stack (linked list).
Next C++ Exercise: Sort the elements of a stack (using a linked list).
What is the difficulty level of this exercise?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics