Skip to content
This repository was archived by the owner on Oct 22, 2021. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions Data Structures/Stack/Stack From Linked Lists/StackLinkedLists.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <iostream>
#include <bits/stdc++.h>
using namespace std;

class StackNode {
public:
int data;
StackNode *next;
};

StackNode *root = NULL;

StackNode* newNode(int data){
StackNode *stackNode = new StackNode();
stackNode->data = data;
stackNode->next = NULL;
return stackNode;
}

int isEmpty() {
return !root;
}

void push(int data) {
StackNode* stackNode = newNode(data);
stackNode->next = root;
root = stackNode;
}

int pop() {
if (isEmpty())
return INT_MIN;
StackNode* temp = root;
root = (root)->next;
int popped = temp->data;
free(temp);

return popped;
}

int peek() {
if (isEmpty())
return INT_MIN;
return root->data;
}

// Driver code
int main() {
// Adding Item to Stack
push(1);
push(2);
push(3);
cout<< "1, 2, 3 added to stack\n";

// Pop up Stack
cout << pop() << " popped from stack\n";


// Get the top values
cout << "Top element is " << peek() << endl;

// Print Stack by getting peek and then popping that item
cout<<"Print Stack : ";
while(!isEmpty()) {
cout<<peek()<<" ";
pop();
}

return 0;
}