define MaxSize<储存数据元素的最大个数>
typedef struct SNode*Stack;
struct SNode
{
ElementType Data[MaxSize];
int Top;//当前栈顶元素的位置
}
1.入栈
void Push(Stack PtrS,ElementType item)
{
if(PtrS->Top==MaxSize)
{
printf(“堆栈满”);
return;
}
else
{
PtrS->Data[++(PtrS->Top)]=item;
return;
}
}
2.出栈
ElementType Pop(Stack PtrS)
{
if(PtrS->Top==-1)
{
printf(“堆栈空”);
return ERROR;//ERROR是ElementType的特殊值,标志错误
}
else
{
return (PtrS->Data[(PtrS->Top)—]);
}
}

——> <——
从左往右和从右往左
#define MaxSize<存储数据元素的最大个数>
struct DStack
{
ElementType Data[MaxSize];
int Top1;//堆栈1的栈顶指针
int Top2;//堆栈2的栈顶指针
}S;
S.Top1=-1;
S.Top=MaxSize;
1.入栈
void Push(struct DStack PtrS,ElementType item,int Tag)
{
if(PtrS->Top2-PtrS->Top1==1)
{
printf(“堆栈慢”);
return ;
}
if(Tag==1)//第一个堆栈(从前往后)
PtrS->Data[++(PtrS->Top1)]=item;
else//第二个堆栈(从右往左)
PtrS->Data[—(PtrS->Top2)]=item;
}
2.出栈
ElementType Pop(struct DStack
PtrS,int Tag)
{
if(Tag==1)
{
if(PtrS->Top1==-1)
{
printf(“堆栈1空”);
return NULL;
}
else return PtrS->Data[(PtrS->Top1)—];
}
else
{
if(PtrS->Top2==MaxSize)
{
printf(“堆栈2空”);
return NULL;
}
else return PtrS->Data[(PtrS->Top2)—];
}
}

堆栈的链式存储实现
typedef struct SNode Stack;
struct SNode
{
ElementType Dsta;
struct Snode
Next;
};
Stack CreateStack()
{
Stack S;
S=(Stack)malloc(sizeof(struct SNode));
S->Next=NULL;
return S;
}
int IsEmpty(Stack S)
{//判断堆栈S是否为空,若为空函数返回整数1,否则返回0
return (S->Next==NULL);
}
(1)入栈
void Push(ElementType item,Stack S)
{
struct SNode TmpCell;
TmpCell=(struct SNode
)malloc(sizeof(struct SNode));
TmpCell->Element=item;
TmpCell->Next=S->Next;
S->Next=TmpCell;
}
(2)出栈
ElementType Pop(Stack S)
{
struct SNode *FirstCell;
ElementType TopElem;
if(IsEmpty(S))
{
printf(“堆栈空”);
return NULL;
}
else
{
FirstCell=S->Next;
S->Next=FirstCell->Next;
TopElem=FirstCell->Element;
frree(FirstCell);
return TopElem;
}
}