Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views3 pages

Stack Linked List

Linked list

Uploaded by

thoratgauri97
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Stack Linked List

Linked list

Uploaded by

thoratgauri97
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <bits/stdc++.

h>

using namespace std;

// Node structure

class Node {

public:

int data;

Node* next;

Node(int new_data) {

this->data = new_data;

this->next = nullptr;

};

// Stack using linked list

class Stack {

Node* head;

public:

Stack() {

this->head = nullptr;

// Check if stack is empty

bool isEmpty() {

return head == nullptr;

// Push an element onto stack

void push(int new_data) {

Node* new_node = new Node(new_data);


new_node->next = head;

head = new_node;

// Pop the top element

void pop() {

if (isEmpty()) return;

Node* temp = head;

head = head->next;

delete temp;

// Return the top element

int peek() {

if (!isEmpty()) return head->data;

return INT_MIN;

};

int main() {

Stack st;

st.push(11);

st.push(22);

st.push(33);

st.push(44);

cout << st.peek() << endl;

st.pop();

st.pop();
cout << st.peek() << endl;

return 0;

You might also like