点击查看代码
//Stack-link list implementation
#include<iostream>
using namespace std;
struct node {
int data;
node* next;
};
node* top;
void push(int x) {
node* temp = new node;
temp->data = x;
temp->next = top;
top = temp;
}//开头插入
int Top() {
return top->data;
}
int isempty() {
if (top == NULL) return 1;
else return 0;
}
void pop() {
if (top == NULL) return;
node* temp = top;//保存原头指针
top = top->next;//先修改指向
delete temp;//后利用原头指针删除1节点
}
int main() {
top = NULL;
push(2);
push(4);
push(6);
cout << Top() << endl;
cout << isempty() << endl;
pop();
cout << Top() << endl;
}