-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01- Stack Using Linked List.cpp
More file actions
54 lines (49 loc) · 1.17 KB
/
Copy path01- Stack Using Linked List.cpp
File metadata and controls
54 lines (49 loc) · 1.17 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
//in the name of God
#include <iostream>
#include <limits.h>
#define ll long long
using namespace std;
struct Node {
int data;
Node *next;
};
class Stack {
private:
Node *head = NULL;
Node* createNode(int data) {
Node *nd = new Node();
nd->data = data;
nd->next = NULL;
return nd;
}
public:
int isEmpty() {
return !head;
}
void push(int data) {
Node* nd = createNode(data);
nd->next = head;
head = nd;
}
int pop() {
if (isEmpty()) return INT_MIN;
Node *nd = head;
head = head->next;
int rs = nd->data;
free(nd);
return rs;
}
int peek() {
if (isEmpty()) return INT_MIN;
return head->data;
}
};
int main() {
Stack *stack = new Stack();
stack->push(10);
stack->push(20);
stack->push(30);
cout << stack->pop() << endl;
cout << "Top element is " << stack->peek() << endl;
return 0;
}