stack implementation through linked list
Sure! Here is an implementation of a stack using a linked list in C++:
#include <iostream>
using namespace std;
// Node class to represent individual elements of the stack
class Node {
public:
int data;
Node* next;
};
// Stack class to implement the stack operations
class Stack {
private:
Node* top;
public:
// Constructor
Stack() {
top = nullptr;
}
// Function to check if the stack is empty
bool isEmpty() {
return top == nullptr;
}
// Function to push an element onto the stack
void push(int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = top;
top = newNode;
}
// Function to pop an element from the stack
int pop() {
if (isEmpty()) {
cout << "Stack is empty!" << endl;
return -1;
}
int poppedValue = top->data;
Node* temp = top;
top = top->next;
delete temp;
return poppedValue;
}
// Function to get the top element of the stack
int peek() {
if (isEmpty()) {
cout << "Stack is empty!" << endl;
return -1;
}
return top->data;
}
};
int main() {
Stack stack;
stack.push(10);
stack.push(20);
stack.push(30);
cout << "Top element: " << stack.peek() << endl;
cout << "Popped element: " << stack.pop() << endl;
cout << "Popped element: " << stack.pop() << endl;
cout << "Popped element: " << stack.pop() << endl;
cout << "Is stack empty? " << (stack.isEmpty() ? "Yes" : "No") << endl;
return 0;
}
Explanation:
The code begins by including the necessary headers and using the
std
namespace.The
Node
class is defined to represent individual elements of the stack. It contains two members:data
, which stores the value of the element, andnext
, which is a pointer to the next element in the stack.The
Stack
class is defined to implement the stack operations. It has a private membertop
, which is a pointer to the top element of the stack.The constructor of the
Stack
class initializes thetop
pointer tonullptr
, indicating an empty stack.The
isEmpty()
function checks if the stack is empty by comparing thetop
pointer tonullptr
. If they are equal, it returnstrue
; otherwise, it returnsfalse
.The
push()
function is used to push an element onto the stack. It creates a newNode
object, assigns the given value to itsdata
member, and sets itsnext
pointer to the currenttop
element. Then, it updates thetop
pointer to point to the new node.The
pop()
function is used to pop an element from the stack. It first checks if the stack is empty. If it is, it prints an error message and returns-1
. Otherwise, it retrieves the value of the top element, updates thetop
pointer to point to the next element, deletes the previous top node, and returns the retrieved value.The
peek()
function returns the value of the top element of the stack without removing it. It performs a similar check for an empty stack and returns-1
if it is empty. Otherwise, it returns the value of the top element.The
main()
function demonstrates the usage of theStack
class. It creates aStack
object, pushes three elements onto the stack, and then performs a series ofpop()
andpeek()
operations to retrieve and display the elements. Finally, it checks if the stack is empty and prints the result.The output of the program will be:
Top element: 30
Popped element: 30
Popped element: 20
Popped element: 10
Is stack empty? Yes
This indicates that the stack is initially filled with elements 10
, 20
, and 30
. The elements are then popped in the reverse order, and at the end, the stack is empty.