-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlistsstack.cpp
More file actions
74 lines (63 loc) · 1.3 KB
/
linkedlistsstack.cpp
File metadata and controls
74 lines (63 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define _z ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
struct Node {
int value;
Node* next;
};
class Stack {
private:
Node* top;
public:
Stack (){
top = nullptr;
}
void push(int val){
Node* newNode = new Node;
newNode -> value = val;
newNode -> next = top;
top = newNode;
}
int pop(){
if(top == nullptr){
cout<<"Stack is empty"<<endl;
}
Node* temp = top;
top = top -> next;
delete temp;
}
int peek(){
if(top == nullptr){
cout<<"stack is empty";
return -1;
}
return top -> value;
}
bool isEmpty(){
return top == nullptr;
}
int print(){
cout<<"The stack is :"<<endl;
Node* temp = top;
while(temp!=NULL){
cout<<temp->value;
cout<<endl;
temp = temp -> next;
}
cout<<endl;
}
};
int main(){
Stack s;
s.push(5);
s.push(10);
s.push(15);
cout<<s.peek()<<endl;
s.print();
s.pop();
s.pop();
s.pop();
s.pop();
return 0;
}