//Date:2006.12.26
//Author:Guo R.H
//USTC
#include
#include
typedef struct SNode
{
int data;
struct SNode *next;
}SNode,*SPtr;
typedef struct SLink
{
SNode *base,*top;
}SLink;
void InitLStack(SLink *S)
{
S->base = S->top = (SPtr)malloc(sizeof(SNode));
if(!S->base)
{
printf("memory error!\n");
exit(0);
}
S->base->next = NULL;
}
void ClearLStack(SLink *S)
{
S->top = S->base;
SPtr p;
while(!S->base->next)
{
p = S->base->next;
S->base->next = p->next;
free(p);
}
S->top->next = NULL;
}
void DestroyLStack(SLink *S)
{
SPtr p = S->base;
while(p)
{
S->top = p;
p = p->next;
free(S->top);
}
}
void Push(SLink *S, int i)
{
SPtr p;
p = (SPtr)malloc(sizeof(SNode));
if(!p)
{
printf("memory error!\n");
exit(0);
}
p->data = i;
p->next = NULL;
S->top->next = p;
S->top = p;
}
void Pop(SLink *S, int *i)
{
if(S->base == S->top)
{
printf("empty stack!\n");
exit(0);
}
*i = S->top->data;
SPtr p = S->base;
while(p->next != S->top)
p = p->next;
S->top = p;
free (p->next);
p->next = NULL;
}
int main()
{
int i, t;
SLink S;
InitLStack(&S);
for(i=0; i<10; i++)
Push(&S, i+1);
for(i=0; i<11; i++)
{
Pop(&S, &t);
printf("%d ",t);
}
ClearLStack(&S);
DestroyLStack(&S);
}
阅读(974) | 评论(0) | 转发(0) |