#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
typedef struct Stack
{
int data;
struct Stack *next;
} Stack, *ptrStack;
// 添加方式:前插
void InitStack(ptrStack p)
{
p->next = NULL;
}
ptrStack CreatNode(ElemType data)
{
ptrStack node = (ptrStack)malloc(sizeof(Stack));
if (node)
{
node->next = NULL;
node->data = data;
return node;
}
else
return NULL;
}
int Push(ptrStack p, ElemType data)
{
ptrStack node = CreatNode(data);
if (node)
{
ptrStack temp = p->next;
p->next = node;
node->next = temp;
return 1;
}
else
return 0;
}
int Pop(ptrStack p)
{
if (p->next == NULL)
return 0;
ptrStack temp = p->next;
p->next = p->next->next;
free(temp);
return 1;
}
void display(ptrStack s)
{
if (s->next == NULL)
printf("null\n");
else
{
s = s->next;
printf("from top to bottom: ");
while (s)
{
printf("%d ", s->data);
s = s->next;
}
putchar('\n');
}
}
void Destroy(ptrStack p)
{
ptrStack temp;
while (p->next)
{
temp = p->next;
p->next = p->next->next;
free(temp);
}
}
int main()
{
Stack HeadStack;
ptrStack pStack = &HeadStack;
InitStack(pStack);
Push(pStack, 1);
Push(pStack, 2);
Push(pStack, 3);
Push(pStack, 4);
display(pStack);
Pop(pStack);
display(pStack);
Destroy(pStack);
// Pop(pStack);
// Pop(pStack);
// Pop(pStack);
// Pop(pStack);
display(pStack);
return 0;
}