//文件名:algo3-2.cpp
#include <stdio.h>
#include <malloc.h>
typedef char ElemType;
typedef struct linknode
{
ElemType data; //数据域
struct linknode *next; //指针域
} LiStack;
void InitStack(LiStack *&s)
{
s=(LiStack *)malloc(sizeof(LiStack));
s->next=NULL;
}
void ClearStack(LiStack *&s)
{
LiStack *p=s->next;
while (p!=NULL)
{
free(s);
s=p;
p=p->next;
}
}
int StackLength(LiStack *s)
{
int i=0;
LiStack *p;
p=s->next;
while (p!=NULL)
{
i++;
p=p->next;
}
return(i);
}
int StackEmpty(LiStack *s)
{
return(s->next==NULL);
}
void Push(LiStack *&s,ElemType e)
{
LiStack *p;
p=(LiStack *)malloc(sizeof(LiStack));
p->data=e;
p->next=s->next; //插入*p结点作为第一个数据结点
s->next=p;
}
int Pop(LiStack *&s,ElemType &e)
{
LiStack *p;
if (s->next==NULL) //栈空的情况
return 0;
p=s->next; //p指向第一个数据结点
e=p->data;
s->next=p->next;
free(p);
return 1;
}
int GetTop(LiStack *s,ElemType &e)
{
if (s->next==NULL) //栈空的情况
return 0;
e=s->next->data;
return 1;
}
void DispStack(LiStack *s)
{
LiStack *p=s->next;
while (p!=NULL)
{
printf("%c ",p->data);
p=p->next;
}
printf("\n");
}
//文件名:exp3-2.cpp
#include <stdio.h>
#include <malloc.h>
typedef char ElemType;
typedef struct linknode
{
ElemType data; //数据域
struct linknode *next; //指针域
} LiStack;
extern void InitStack(LiStack *&s);
extern void ClearStack(LiStack *&s);
extern int StackLength(LiStack *s);
extern int StackEmpty(LiStack *s);
extern void Push(LiStack *&s,ElemType e);
extern int Pop(LiStack *&s,ElemType &e);
extern int GetTop(LiStack *s,ElemType &e);
extern void DispStack(LiStack *s);
void main()
{
ElemType e;
LiStack *s;
printf("(1)初始化链栈s\n");
InitStack(s);
printf("(2)链栈为%s\n",(StackEmpty(s)?"空":"非空"));
printf("(3)依次进链栈元素a,b,c,d,e\n");
Push(s,'a');
Push(s,'b');
Push(s,'c');
Push(s,'d');
Push(s,'e');
printf("(4)链栈为%s\n",(StackEmpty(s)?"空":"非空"));
printf("(5)链栈长度:%d\n",StackLength(s));
printf("(6)从链栈顶到链栈底元素:");DispStack(s);
printf("(7)出链栈序列:");
while (!StackEmpty(s))
{ Pop(s,e);
printf("%c ",e);
}
printf("\n");
printf("(8)链栈为%s\n",(StackEmpty(s)?"空":"非空"));
printf("(9)释放链栈\n");
ClearStack(s);
}