#include <stdio.h>
#include "linked_list.h"
#define MAX 50
typedef LNode SNode;
typedef LinkList LinkStack;
int InitStack_link(LinkStack *stack)
{
*stack=(SNode *)malloc(sizeof(SNode));
if(NULL==*stack)
return -1;
(*stack)->next=NULL;
return 0;
}
int StackEmpty_link(LinkStack stack)
{
if(NULL==stack->next)
return 1;
return 0;
}
int StackLength_link(LinkStack stack)
{
int count=0;
SNode *p=stack;
while(NULL!=p->next)
{
p=p->next;
count++;
}
return count;
}
int GetTop_link(LinkStack stack,ElemType *e)
{
if(StackEmpty_link(stack))
return -1;
*e=stack->next->data;
return 0;
}
int Push_link(LinkStack stack,ElemType e)
{
SNode *p;
p=(SNode *)malloc(sizeof(SNode));
if(NULL==p)
return -1;
p->data=e;
p->next=stack->next;
stack->next=p;
return 0;
}
int Pop_link(LinkStack stack,ElemType *e)
{
SNode *p=NULL;
if(StackEmpty_link(stack))
return -1;
p=stack->next;
*e=p->data;
stack->next=p->next;
free(p);
return 0;
}
int main()
{
int i=0;
ElemType a='e';
LinkStack z;
InitStack_link(&z);
Push_link(z,'a');
Push_link(z,'b');
Push_link(z,'c');
Push_link(z,'d');
Push_link(z,'e');
Push_link(z,'f');
for(i=0;i<6;i++)
{
Pop_link(z,&a);
printf("%d\n",StackLength_link(z));
printf("%c",a);
}
if(1==StackEmpty_link(z))
printf("is NULL!");
printf("\nok!\n");
return 0;
}
栈的练习
最新推荐文章于 2022-05-14 21:37:10 发布