![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3e4d7fa44203c39443a225b6f8eff1b8.png)
数组实现堆栈
typedef int Position;
struct SNode {
ElementType *Data;
Position Top;
int MaxSize;
};
typedef struct SNode *Stack;
typedef int bool;
#define TRUE 1
#define FALSE 0
#define ERROR -1
#define MAXSIZE 1000
Stack CreateStack( int MaxSize )
{
Stack s =(Stack)malloc(sizeof(struct SNode));
s->Data = (ElementType *)malloc(MaxSize*sizeof(ElementType));
s->Top = -1;
s->MaxSize = MaxSize;
return s;
}
bool IsFull( Stack S )
{
return (S->Top ==S->MaxSize-1);
}
bool Push( Stack S, ElementType X )
{
if(IsFull(S)){
return FALSE;
}else{
S->Data[++S->Top] = X;
return TRUE;
}
}
bool IsEmpty( Stack S )
{
return (S->Top == -1);
}
ElementType Pop( Stack S )
{
if(IsEmpty(S)){
return NULL;
}else{
return S->Data[S->Top--];
}
}
链表实现堆栈
typedef struct SNode *PtrToSNode;
struct SNode {
ElementType Data;
PtrToSNode Next;
};
typedef PtrToSNode Stack;
typedef int bool;
#define TRUE 1
#define FALSE 0
#define ERROR -1
#define MAXSIZE 1000
Stack CreateStack( )
{
Stack s =(Stack)malloc(sizeof(struct SNode));
return s;
}
bool IsFull( Stack S )
{
return FALSE;
}
bool Push( Stack S, ElementType X )
{
PtrToSNode Ptemp;
Ptemp = (PtrToSNode) malloc(sizeof(struct SNode));
Ptemp ->Data =X;
Ptemp ->Next = S->Next;
S->Next = Ptemp;
return TRUE;
}
bool IsEmpty( Stack S )
{
return (S->Next == NULL);
}
ElementType Pop( Stack S )
{
PtrToSNode p;
ElementType e;
if(IsEmpty(S)){
return NULL;
}else{
p = S->Next;
S->Next = p ->Next;
e=p ->Data;
free(p);
return e;
}
}