#include <stdio.h>
#include <stdlib.h>
typedef struct linkstack
{
int data;
struct linkstack* next;
}linkstack;
void pushStack(linkstack** top, int data)
{
linkstack* node = (linkstack*)malloc(sizeof(linkstack));
if (node == NULL)
{
perror("push malloc error\n");
return;
}
node->data = data;
node->next =*top;
*top = node;
}
int isFullStack(linkstack* top)
{
return top == NULL;
}
int popStack(linkstack** top)
{
if (isFullStack(*top))
{
printf("空栈!\n");
return -1;
}
linkstack* p = *top;
int temp = p->data;
(*top) = (*top)->next;
free(p);
p = NULL;
return temp;
}
void stackLength(linkstack* top)
{
int temp = 0;
while (top)
{
top = top->next;
temp++;
}
printf("栈数量:%d\n", temp);
}
void getTopData(linkstack* top)
{
printf("栈顶:%d\n", top->data);
}
void noneStack(linkstack** top)
{
while (!isFullStack(*top))
{
popStack(top);
}
printf("已清空!\n");
}
void printStack(linkstack* top)
{
while (top)
{
printf("%d->", top->data);
top = top->next;
}
printf("NULL\n");
}
int main(int argc, char const *argv[])
{
linkstack* top = NULL;
pushStack(&top, 1);
pushStack(&top, 2);
pushStack(&top, 3);
pushStack(&top, 4);
pushStack(&top, 5);
printStack(top);
stackLength(top);
printf("出栈:%d\n", popStack(&top));
printStack(top);
getTopData(top);
printf("出栈:%d\n", popStack(&top));
printStack(top);
getTopData(top);
noneStack(&top);
stackLength(top);
return 0;
}
