Skip to content

Commit

Permalink
Update Stack_as_ll.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
japnit01 authored Oct 25, 2020
1 parent 7592ac2 commit 92eced6
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions C++/DataStructures/Stack_as_ll.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
#include <iostream>

using namespace std;

// structure/schema of any node in the stack
struct Node {
int data;
struct Node *next;
int data;
struct Node *next; // 'next' stores the address of node below the current node
};
struct Node* top = NULL;
int size = 0;
struct Node* top = NULL; //initializing the top node of the stack as 'NULL'
int size = 0; // size of the stack

// function to create and push a node on the top of the stack
void push(int val) {
struct Node* newnode = new Node;
newnode->data = val;
newnode->next = top;
top = newnode;
size++;
}

//function to pop the top node from the stack
void pop() {
if(top==NULL)
if(top==NULL) // if stack is empty
cout<<"Stack Underflow\n";
else {
cout<<"The popped element is "<< top->data <<"\n";
Node* temp = top;
cout<<"The popped element is "<< temp->data <<"\n";
top = top->next;
delete(temp);
size--;
}
}

// function to display the values of all the nodes of the stack
void display() {
struct Node* ptr;
if(top==NULL)
Expand All @@ -38,9 +47,10 @@ void display() {
cout<<endl;
}

// function to display the top element of the stack
void peek()
{
if(top==NULL)
if(top==NULL) // if stack is empty
{
cout<<"Underflow\n";
}
Expand All @@ -50,10 +60,12 @@ void peek()
}
}

// function to display the size of the stack
void get_size()
{
cout<<size<<"\n";
}

int main() {
int ch, val;
cout<<"1) Push\n";
Expand Down

0 comments on commit 92eced6

Please sign in to comment.