#include<stdio.h>
#include<malloc.h>
typedef int DataType;
typedef struct snode //定義結構體
{
DataType data;
struct snode *next;
}LSNode;
void InitStact(LSNode **head) //初始化
{
*head = (LSNode *)malloc(sizeof(LSNode));
(*head)->next = NULL;
}
int NotEmpty(LSNode *head){ //判斷堆棧是否為空
if(head->next == NULL){
return 0;
}
else{
return 1;
}
}
void Push(LSNode *head,DataType x) //入棧
{
LSNode *p;
p = (LSNode *)malloc(sizeof(LSNode));
p->data = x;
p->next = head ->next;
head->next = p;
}
int Pop(LSNode *head,DataType *x){ //出棧
LSNode *p = head->next;
if(p == NULL){
printf("堆棧已空出錯!");
return 0;
}
head->next = p->next;
*x = p->data;
free(p);
return 1;
}
int Top(LSNode *head, DataType *x){ //取棧頂元素
LSNode *p = head->next;
if(p == NULL){
printf("堆棧已空出錯!");
return 0;
}
*x = p->data;
return 1;
}
int main()
{
LSNode *s;
int x;
InitStact(&s);
// scanf_s("%d",&x);
for(x=0;x<10;++x)
{
Push(s,x+1);
}
Top(s,&x);
printf("%d \n",x);
while(NotEmpty(s))
{
Pop(s,&x);
printf("%d ",x);
}
return 0;
}