#include <iostream>
using namespace std;
#define null 0
typedef struct Stack{
int a;
struct Stack *next ;
}Node;
Stack * top;
void push(Stack *top); //压栈
void pop(Stack *top); //弹栈
void show(Stack *top); //显示
void clear(Stack *top);//清空
int main(){
top = (Stack *)malloc(sizeof(Node));
top ->next = null;
while(1){
printf("1.压栈\n2.弹栈\n3.查看栈中元素\n4.清空栈\n5.退出\n");
int n ;
cin >> n;
cout << "***************************" <<endl;
if(n == 1) push(top);
else if (n == 2) pop(top);
else if (n == 3) show(top);
else if (n == 4) clear(top);
else break;
}
return 0;
}
void pop(Stack *top){
if(top ->next){
Stack *p;
p = top ->next; //让top直接指向栈的第二个;
top ->next = p ->next;
}
else cout << "Stack Null!"<<endl;
cout << "***************************" <<endl;
}
void push(Stack *top){
Stack * p;
p = (Stack *)malloc(sizeof(Node));
cin >> p ->a;
p ->next = top ->next;
top ->next = p;
cout << "***************************" <<endl;
}
void show(Stack *top){
if(!top ->next) cout << "Stack Null!"<<endl;
else{
Stack *p ;
p = top ->next;
while(p){
cout << p ->a <<endl;
p = p ->next;
}
}
cout << "***************************" <<endl;
}
void clear(Stack *top){
top ->next = null;
}
栈(链表)
最新推荐文章于 2023-10-07 17:47:05 发布