File tree 1 file changed +92
-0
lines changed
1 file 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
+ #define stack_empty -1
4
+ class pilha {
5
+ private:
6
+ struct node {
7
+ int dado;
8
+ node *prox;
9
+ };
10
+ node *head = nullptr ;
11
+
12
+ public:
13
+ void push (int val){
14
+ node* elemento;
15
+ elemento = new node;
16
+ elemento->dado = val;
17
+ if (head == nullptr ){
18
+ // pilha vazia
19
+ elemento->prox = nullptr ;
20
+ head = elemento;
21
+ }else {
22
+ // pilha com elemento
23
+ elemento->prox = head;
24
+ head = elemento;
25
+ }
26
+ }
27
+
28
+ bool isempty (){
29
+ if (head == nullptr ){
30
+ return true ;
31
+ }
32
+ return false ;
33
+ }
34
+
35
+ int pop (){
36
+ int val = stack_empty;
37
+ if (!isempty ()){
38
+ val = head->dado ;
39
+ node *next, *at;
40
+ next = head->prox ;
41
+ at = head;
42
+ head = next;
43
+ delete at;
44
+ }
45
+ return val;
46
+ }
47
+
48
+ int show_top (){
49
+ int val = stack_empty;
50
+ if (!isempty ()){
51
+ val = head->dado ;
52
+ }
53
+ return val;
54
+ }
55
+
56
+ int size (){
57
+ int cont = 0 ;
58
+ node* aux;
59
+ aux = head;
60
+ while (aux->prox !=nullptr ){
61
+ cont++;
62
+ aux = aux->prox ;
63
+ }
64
+ cont++;
65
+ return cont;
66
+ }
67
+ };
68
+
69
+ int main (){
70
+ pilha st;
71
+ int val;
72
+
73
+ for (int i = 0 ; i < 10 ; i++){
74
+ st.push ((i+1 ));
75
+ }
76
+ for (int i = 0 ; i <12 ; i++){
77
+ val = st.pop ();
78
+ if (val != stack_empty){
79
+ cout << val << endl;
80
+ }else {
81
+ cout << " stack is empty!" << endl;
82
+ }
83
+ }
84
+
85
+ for (int i = 0 ; i < 5 ; i++){
86
+ st.push ((i+1 ));
87
+ }
88
+
89
+ cout << " stack have " << st.size () << " elements" << endl;
90
+ cout << st.show_top () << " is top element of stack" << endl;
91
+ return 0 ;
92
+ }
You can’t perform that action at this time.
0 commit comments