栈的顺序表实现
#include <cstdio>
#include <iostream>
#define MaxSize 100
using namespace std;
typedef int ElemType;
ty
typedef struct
{
ElemType data[MaxSize];
int top;
}SqStack;
void InitStack(SqStack &S){
S.top=-1;
}
bool StackPop(SqStack &S,ElemType &x){
if (S.top==-1)
{
return 0;
}
x=S.data[S.top--];
return 1
}
bool StackPush(SqStack &S,ElemType x){
if (S.top+1=MaxSize)
{
return 0;
}
S.data[++S.top]=x;
return 1;
}
bool getTopELem(SqStack &S,ElemType &x){
if (S.top==-1)
{
return 0;
}
x=S.data[S.top];
return 1;
}
bool EmptyStack(SqStack &S){
if (S.top==-1)
{
return 1;
}else{
return 0;
}
}
链表实现
#include <iostream>
using namespace std;
typedef int ElemType;
typedef strcut lnode {
ElemType data;
struct lnode *next;
}*LiStack,ListNode;
void InitStack(LiStack &S){
S=(LiStack)malloc(sizeof(ListNode));
S->next=NULL;
}
void StackPush(LiStack &S,ElemType x){
LiStack p,q;
p=S;
while(p->next!=NULL){
p=p->next;
}
q=(LiStack)malloc(sizeof(ListNode));
if (!q)
{
exit(0);
}
p->next=q;
q->data=x;
q->next=NULL;
}
bool StackPop(LiStack &S,ElemType &x){
if (S->next==NULL)
{
return 0;
}
LiStack p,q;
p=S;
while(p->next!=NULL){
q=p;
p=p->next;
}
x=p->data;
q->next=NULL;
free(p);
return 1;
}
bool GetTop(LiStack &S,ElemType &x){
if (S->next==NULL)
{
return 0;
}
LiStack p;
p=s;
while(p->next!=NULL){
p=p->next;
}
x=p->data;
return 1;
}
bool StackEmpty(LiStack &S){
if (S->next==NULL)
return true;
else
return false;
}