//
// main.c
// Link stack
//
// Created by 丘** on 2021/7/22.
//链栈实现 by c
#include <stdio.h>
#include <stdlib.h>
typedef struct StackNode
{
int data;
struct StackNode* next;
}StackNode;
StackNode* push(StackNode*s,int e)//入栈
{
StackNode*p;
p=(StackNode*)malloc(sizeof(StackNode));
p->data=e;
p->next=s;
s=p;
return s;
}
void GetTop(StackNode*s)//取栈顶元素
{
if(s!=NULL)
printf("the top number is %d\n",s->data);
}
StackNode* Pop(StackNode*s)//弹栈
{
int e;
e=s->data;
printf("%d will be pop",e);
StackNode* p;
p=s;
s=s->next;
free(p);
return s;
}
int main(int argc, const char * argv[]) {
printf("Hello, World!\n");
StackNode* s=NULL;
for(int i=0;i<5;i++)
{
int e;
printf("enter number\n");
scanf("%d",&e);
s=push(s,e);
}
GetTop(s);
s=Pop(s);
return 0;
}