第四周 栈
```cpp
#include<bits/stdc++.h>
using namespace std;
const int N=5;
int a[N],top=-1;
bool isFull(){
if(top=N-1)return 0;
else return 1;
}
void Push(int t){
if(isFull)a[++top]=t;
return ;
}
int Pop(){
if(top==-1)return top;
top--;
return top;
}
void show(){
int i=0;
while(i<=top){
cout<<a[i]<<" ";
i++;
}
cout<<endl;
}
int main(){
int t;
while(cin>>t){
if(!t)break;
Push(t);
}
cout<<"this is completely show:";
show();
cout<<"now pop it:"<<endl;
while(top!=-1){
top=Pop();
show();
}return 0;
}
顺序栈
```cpp
#include<bits/stdc++.h>
using namespace std;
const int N=5;
struct node{
int ata;
node *next;
};
node* Push(int t,node* pr){
node *p=new node;
p->ata=t;
p->next=pr;
pr=p;
return pr;
}
node* Pop(node *pr){
if(pr){
node *t=pr;
pr=pr->next;
cout<<"pop it:"<<t->ata<<endl;
if(pr)cout<<"top is:"<<pr->ata<<endl;
else cout<<"nothing "<<endl;
free(t);
}
else cout<<"nothing ada";
return pr;
}
void show(node* pr){
while(pr){
cout<<pr->ata<<" ";
pr=pr->next;
}
cout<<endl;
}
int main(){
int t;
node *h;
node* pr=h;
while(cin>>t){
if(!t)break;
pr=Push(t,pr);
}
// pr->next=NULL;
cout<<"this is completely show:"<<endl;
show(pr);
/// pr=h;
cout<<"now pop it:"<<endl;
while(pr!=h){
pr=Pop(pr);
show(pr);
}return 0;
}链表栈
|