笔记
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
typedef struct node stack;
typedef stack *ptr;
ptr createstack()
{
ptr s;
s = (ptr)malloc(sizeof(stack));
if(s==NULL)
{
printf("栈头申请空间失败");
return NULL;
}
s->next=NULL;
return s;
}//创建栈头
void push(ptr stack,int item)
{
ptr node;
node = (ptr)malloc(sizeof(stack));
if(node==NULL)
{
printf("入栈申请空间失败");
return;
}
node->data=item;
node->next = stack->next;
stack->next=node;
}//入栈
int isempty(ptr stack)
{
return (stack->next==NULL);//is=0为非空,is=1为空
}//判断栈是否为空
void pop(ptr stack)
{
if(isempty(stack))
{
printf("栈已空");
return;
}
int data;
ptr firstnode;
firstnode = stack->next;
stack->next=firstnode->next;
printf("%d",firstnode->data);
free(firstnode);
}//出栈
void show(ptr stack)
{
ptr previous;
previous = stack->next;
while(previous!=NULL)
{
printf("%d",previous->data);
previous = previous->next;
}
printf("\n");
}//打印栈内容
int main()
{
ptr createstack();
ptr stack;
stack=createstack();
void push(ptr stack,int item);
void pop(ptr stack);
int isempty(ptr stack);
/*push(stack,2);
push(stack,3);
show(stack);
pop(stack);
pop(stack);
pop(stack);试验*/
return 0;
}