//链栈相当于一条链表
//头指针指向的为栈顶元素,NULL区域为栈底
//入栈操作即不断头插
//出栈操作即输出栈顶指针下移
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int data;
struct node *next;
}ElemSN;
ElemSN *Createlinkstack(int *a,int n)//push
{
ElemSN *head,*tail;
head=NULL;
for(int i=0;i<n;i++){//头插模拟入栈 栈顶指针上移
tail=(ElemSN *)malloc(sizeof(ElemSN));
tail->data=a[i];
tail->next=head;
head=tail;
}
return head;
}
void Printlinkstack(ElemSN *head)//pop
{
while(head){//操作栈顶元素 栈顶指针下移
printf("%4d",head->data);
head=head->next;
}
}
int main()
{
int n;
printf("Please input the n:");
scanf("%d",&n);
int *a;
a=(int *)malloc(n*sizeof(int));
for(int i=0;i<n;i++){
scanf("%d",a+i);
}
ElemSN *head=NULL;
//入栈
head=Createlinkstack(a,n);
//出栈
Printlinkstack(head);
return 0;
}