File tree 2 files changed +92
-0
lines changed
2 files changed +92
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+
4
+ class Node
5
+ {
6
+ public:
7
+ int val;
8
+ Node *prev;
9
+ Node *next;
10
+ Node (int val)
11
+ {
12
+ this ->val = val;
13
+ this ->prev = NULL ;
14
+ this ->next = NULL ;
15
+ }
16
+ };
17
+
18
+ class myStack
19
+ {
20
+ public:
21
+ Node *head = NULL ;
22
+ Node *tail = NULL ;
23
+ int sz = 0 ;
24
+
25
+ void push (int val)
26
+ {
27
+ sz++;
28
+ Node *newNode = new Node (val);
29
+ if (head == NULL )
30
+ {
31
+ head = newNode;
32
+ tail = newNode;
33
+ return ;
34
+ }
35
+ tail->next = newNode;
36
+ newNode->prev = tail;
37
+ tail = newNode;
38
+ }
39
+
40
+ void pop ()
41
+ {
42
+ if (tail == NULL )
43
+ return ;
44
+ Node *deleteNode = tail;
45
+ tail = tail->prev ;
46
+ if (tail == NULL )
47
+ head = NULL ;
48
+ else
49
+ tail->next = NULL ;
50
+ delete deleteNode;
51
+ sz--;
52
+ }
53
+
54
+ int top ()
55
+ {
56
+ return tail->val ;
57
+ }
58
+
59
+ int size ()
60
+ {
61
+ return sz;
62
+ }
63
+
64
+ bool empty ()
65
+ {
66
+ if (sz == 0 )
67
+ return true ;
68
+ else
69
+ return false ;
70
+ }
71
+ };
72
+
73
+ int main ()
74
+ {
75
+ myStack st;
76
+ int n;
77
+ cin >> n;
78
+ for (int i = 0 ; i < n; i++)
79
+ {
80
+ int x;
81
+ cin >> x;
82
+ st.push (x);
83
+ }
84
+
85
+ while (!st.empty ())
86
+ {
87
+ cout << st.top () << endl;
88
+ st.pop ();
89
+ }
90
+
91
+ return 0 ;
92
+ }
You can’t perform that action at this time.
0 commit comments