From b4c6b1490e527307bb1189433d194ccf3a83bc22 Mon Sep 17 00:00:00 2001 From: Manish-Banait <93503452+Manish-Banait@users.noreply.github.com> Date: Sun, 23 Oct 2022 20:33:59 +0530 Subject: [PATCH] Create stack using singly linked list.cpp --- stack using singly linked list.cpp | 153 +++++++++++++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 stack using singly linked list.cpp diff --git a/stack using singly linked list.cpp b/stack using singly linked list.cpp new file mode 100644 index 0000000..04ca5be --- /dev/null +++ b/stack using singly linked list.cpp @@ -0,0 +1,153 @@ +// C++ program to Implement a stack +// using singly linked list +#include +using namespace std; + +// creating a linked list; +class Node { +public: + int data; + Node* link; + + // Constructor + Node(int n) + { + this->data = n; + this->link = NULL; + } +}; + +class Stack { + Node* top; + +public: + Stack() { top = NULL; } + + void push(int data) + { + + // Create new node temp and allocate memory in heap + Node* temp = new Node(data); + + // Check if stack (heap) is full. + // Then inserting an element would + // lead to stack overflow + if (!temp) { + cout << "\nStack Overflow"; + exit(1); + } + + // Initialize data into temp data field + temp->data = data; + + // Put top pointer reference into temp link + temp->link = top; + + // Make temp as top of Stack + top = temp; + } + + // Utility function to check if + // the stack is empty or not + bool isEmpty() + { + // If top is NULL it means that + // there are no elements are in stack + return top == NULL; + } + + // Utility function to return top element in a stack + int peek() + { + // If stack is not empty , return the top element + if (!isEmpty()) + return top->data; + else + exit(1); + } + + // Function to remove + // a key from given queue q + void pop() + { + Node* temp; + + // Check for stack underflow + if (top == NULL) { + cout << "\nStack Underflow" << endl; + exit(1); + } + else { + + // Assign top to temp + temp = top; + + // Assign second node to top + top = top->link; + + // This will automatically destroy + // the link between first node and second node + + // Release memory of top node + // i.e delete the node + free(temp); + } + } + + // Function to print all the + // elements of the stack + void display() + { + Node* temp; + + // Check for stack underflow + if (top == NULL) { + cout << "\nStack Underflow"; + exit(1); + } + else { + temp = top; + while (temp != NULL) { + + // Print node data + cout << temp->data; + + // Assign temp link to temp + temp = temp->link; + if (temp != NULL) + cout << " -> "; + } + } + } +}; + +// Driven Program +int main() +{ + // Creating a stack + Stack s; + + // Push the elements of stack + s.push(11); + s.push(22); + s.push(33); + s.push(44); + + // Display stack elements + s.display(); + + // Print top element of stack + cout << "\nTop element is " << s.peek() << endl; + + // Delete top elements of stack + s.pop(); + s.pop(); + + // Display stack elements + s.display(); + + // Print top element of stack + cout << "\nTop element is " << s.peek() << endl; + + return 0; +}