-
Notifications
You must be signed in to change notification settings - Fork 0
/
pila.cpp
78 lines (67 loc) · 1.61 KB
/
pila.cpp
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
75
76
77
78
#include <iostream>
using namespace std;
struct Node
{
int val;
Node *next;
};
void addStack(Node *&, int);
void popStack(Node *&, int &);
int main()
{
int num, opcion = 0;
Node *node = NULL;
//node.next = NULL;
while (opcion != 3)
{
cout << "Elija una de las siguientes opciones:" << endl;
cout << "1) Insertar un numero..." << endl;
cout << "2) Mostrar la pila..." << endl;
cout << "3) Salir" << endl;
cin >> opcion;
if (opcion == 1)
{
fflush(stdin);
cout << "Ingrese un valor para PILA" << endl;
cin >> num;
addStack(node, num);
system("cls");
cout << "Operacion exitosa" << endl << endl;
}
else if (opcion == 2)
{
if (node == NULL){
system("cls");
cout << "PILA VACIA" << endl << endl;
}
else
{
while (node != NULL)
{
popStack(node, num);
if(node != NULL){
cout << num << "->";
}else{
cout << num << "." << endl;
}
}
}
}
}
cout << "Hasta luego!" << endl;
return 0;
}
void addStack(Node *&stack, int n)
{
Node *newNode = new Node();
newNode->val = n;
newNode->next = stack;
stack = newNode;
}
void popStack(Node *&stack, int &n)
{
Node *aux = stack;
n = aux->val;
stack = aux->next;
delete aux;
}