链表版
#include<iostream>
#include<string>
using namespace std;
struct node{
int element;
struct node *next;
};
void Init(node *header){
int n; cin >> n;
for(int i=0; i<n; ++i){
node *temp = (node*)malloc(sizeof(node));
cin >> temp->element;
temp->next = NULL;
node *t = header;
while(t->next!=NULL){
t = t->next;
}
t->next = temp;
}
return ;
}
void Push(node *header){
node *t = header->next;
node *temp = (node*)malloc(sizeof(node));
cin >> temp->element;
temp->next = NULL;
while(t->next!=NULL){
t = t->next;
}
t->next = temp;
return ;
}
void Pop(node *header){
node *t = header->next;
cout << t->element << " ";
header->next = t->next;
return ;
}
int main(){
node *header = (node*)malloc(sizeof(node));
header->next = NULL;
Init(header);
Push(header);
Pop(header);
return 0;
}
数组版
#include<iostream>
using namespace std;
int Init(int *a){
int n; cin >> n;
for(int i=0; i<n; ++i){
cin >> a[i];
}
return n;
}
int Push(int *a,int num){
int t; cin >> t;
a[num++] = t;
return num;
}
int Pop(int *a,int num){
cout << a[--num] << endl;
return num;
}
int main(){
int a[100000];
int num = Init(a);
num = Push(a,num);
num = Pop(a,num);
return 0;
}