-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex4.cpp
55 lines (55 loc) · 934 Bytes
/
ex4.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
//bibliotecas...
#include <iostream>
#include<cstdlib>
using namespace std;
//estrutura
struct no{
int valor;
no * anterior;
};
no*topo=NULL;
void push(int valor){
no*novoNo=(no*)malloc(sizeof(no));
novoNo->valor=valor;
if(topo==NULL){
novoNo->anterior=NULL;
}else{
novoNo->anterior=topo;
}
topo=novoNo;
}
int top(){
if(topo==NULL){
cout<<"Pilha vazia, sem topo"<<endl;
}else{
return topo->valor;
}
}
void pop(){
if(topo==NULL){
cout<<"ta vazia"<<endl;
}
else{
no*aux=topo;
topo=topo->anterior;
free(aux);
}
}
void imprimepilha(){
if(topo!=NULL){
for(no*aux=topo;aux!=NULL;aux=aux->anterior){
cout<<aux->valor<<" ";
}
}
else{
cout<<"Pilha vazia";
}
cout<<endl;
}
int main(){
top();
push(10);
cout<<top()<<endl;
pop();
imprimepilha();
}