#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct Node{
ElemType data;
struct Node* next;
}*LiStack,stack;
bool InitStack(LiStack& S)
{
S = NULL;
return true;
}
bool Empty(LiStack S)
{
return (S == NULL);
}
bool Push(LiStack& S,ElemType e)
{
LiStack p = (LiStack)malloc(sizeof(stack));
p->data = e;
if (S == NULL)
{
p->next = NULL;
}
else
{
p->next = S;
}
S = p;
return true;
}
bool Pop(LiStack& S, ElemType& e)
{
if (Empty(S)) return false;
e = S->data;
LiStack p = S;
if (S->next == NULL)
{
free(S);
S = NULL;
return true;
}
S = S->next;
free(p);
p = NULL;
return true;
}
bool GetTop(LiStack S, ElemType& e)
{
if (InitStack(S)) return false;
e = S->data;
return true;
}
bool DestroyStack(LiStack& S)
{
LiStack p = (LiStack)malloc(sizeof(stack));
while (S)
{
p = S;
S = S->next;
free(p);
p = NULL;
}
return true;
}
int main()
{
LiStack S;
ElemType e;
InitStack(S);
Push(S, 3);
Push(S, 5);
Push(S, 7);
Pop(S, e);
printf("Pop e=%3d\n", e);
GetTop(S, e);
printf("Get e=%3d\n", e);
Pop(S, e);
printf("Pop e=%3d\n", e);
GetTop(S, e);
printf("Get e=%3d\n", e);
DestroyStack(S);
if (Pop(S, e))
printf("Pop e=%3d\n", e);
else
printf("Pop fail\n");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct Node {
ElemType data;
struct Node* next;
}*headLiStack, stack;
bool InitStack(headLiStack& S)
{
S = (headLiStack)malloc(sizeof(stack));
S ->next= NULL;
return true;
}
bool Empty(headLiStack S)
{
return (S->next== NULL);
}
bool Push(headLiStack& S, ElemType e)
{
headLiStack p = (headLiStack)malloc(sizeof(stack));
p->data = e;
if (S->next == NULL)
{
p->next = NULL;
}
else
{
p->next = S->next;
}
S->next = p;
return true;
}
bool Pop(headLiStack& S, ElemType& e)
{
if (Empty(S)) return false;
headLiStack p = S->next;
e = p->data;
S->next = p->next;
free(p);
p = NULL;
return true;
}
bool GetTop(headLiStack S, ElemType& e)
{
if (InitStack(S)) return false;
e = S->next->data;
return true;
}
bool DestroyStack(headLiStack& S)
{
headLiStack p = (headLiStack)malloc(sizeof(stack));
while (S->next)
{
p = S->next;
S->next= p->next;
free(p);
p = NULL;
}
return true;
}
int main()
{
headLiStack S;
ElemType e;
InitStack(S);
Push(S, 3);
Push(S, 5);
Push(S, 7);
Pop(S, e);
printf("Pop e=%3d\n", e);
GetTop(S, e);
printf("Get e=%3d\n", e);
Pop(S, e);
printf("Pop e=%3d\n", e);
GetTop(S, e);
printf("Get e=%3d\n", e);
DestroyStack(S);
if (Pop(S, e))
printf("Pop e=%3d\n", e);
else
printf("Pop fail\n");
return 0;
}