一、栈的顺序存储
1.结构体
#define MAXSIZE //存储元素的最大个数
typedef struct SNode *Stack;
struct SNode{
ElementType Data[MaxSize];
int Top;
};
2.入栈
void Push( Stack PtrS, ElementType item )
{
if( PtrS->Top == MaxSize-1 ) {
printf("堆栈满");
return;
}else{
PtrS->Data[++(PtrS->Top)] = item;
return;
}
}
3.出栈
ElementType Pop(Stack PtrS)
{
if(PtrS->Top == -1){
printf("堆栈空");
return ERROR; //ERROR是ElementType的特殊值,标志错误
}else
return (ptrS->Data[(PtrS->Top)--]);
)
完整代码:
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 10
#define ERROR -1
#define ElementType int
typedef struct SNode *Stack;
struct SNode{
ElementType Data[MaxSize];
int Top;
};
Stack PtrS;
//1.初始化
Stack MakeEmpty()
{
Stack PtrS;
PtrS = (Stack)malloc(sizeof(struct SNode));
PtrS->Top=-1;
return PtrS;
}
//2.入栈
void Push(ElementType item,Stack PtrS)
{
if(PtrS->Top==MaxSize){
printf("栈已满\n");
return;
}
else{
PtrS->Data[++(PtrS->Top)]=item;
return;
}
}
//3.出栈
ElementType Pop(Stack PtrS)
{
if(PtrS->Top==-1){
printf("栈为空\n");
return ERROR;
}
else{
return PtrS->Data[(PtrS->Top)--];
}
}
int main()
{
int i;
PtrS=MakeEmpty();
Push(23,PtrS);
Push(28,PtrS);
Push(78,PtrS);
for(i=0;i<=PtrS->Top;i++ ){
printf("%d ",PtrS->Data[i]);
}
printf("\n");
printf("出栈为:%d\n",Pop(PtrS));
for(i=0;i<=PtrS->Top;i++ ){
printf("%d ",PtrS->Data[i]);
}
printf("\n");
return 0;
}
二、栈的链式存储
栈的链式存储结构实际上就是一个单链表,叫做链栈。插入和删除操作只能在链表的栈顶进行。
1.创建链表
typedef struct SNode *Stack;
strcut SNode{
ElementType Data;
struct SNode *Next;
};
2.初始化
Stack CreateStack()
{
Stack S;
S = (Stack)malloc(sizeof(struct SNode));
s->Next = NULL;
return S;
}
3.判断堆栈s是否为空
int IsEmpty(Stack S)
{
return (S->Next == NULL);//若堆栈S为空则还回1,否则返回0
}
4.入栈
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;
}
5.出栈
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;
free(FirstCell);
return TopElem;
}
}
完整代码:
#include<stdio.h>
#include<stdlib.h>
#define ElementType int
typedef struct SNode *Stack;
struct SNode{
ElementType Data;
Stack Next;
};
Stack PtrS;
//1.初始化
Stack MakeEmpty()
{
Stack PtrS;
PtrS = (Stack)malloc(sizeof(struct SNode));
PtrS->Next=NULL;
return PtrS;
}
//2.判断堆栈是否为空,空为1,不空为0
int IsEmpty(Stack PtrS)
{
return (PtrS->Next==NULL);
}
//3.入栈
void Push(ElementType item,Stack PtrS)
{
Stack TmpCell;
TmpCell = (Stack)malloc(sizeof(struct SNode));
TmpCell->Data=item;
TmpCell->Next=PtrS->Next;
PtrS->Next=TmpCell;
}
//4.出栈
ElementType Pop(Stack PtrS)
{
ElementType TopElement;
Stack FirstCell;
if(IsEmpty(PtrS)){
printf("栈为空\n");
return 0;
}
else{
FirstCell=PtrS->Next;
PtrS->Next=FirstCell->Next;
TopElement=FirstCell->Data;
free(FirstCell);
return TopElement;
}
}
int main()
{
int i;
PtrS=MakeEmpty();
Push(41,PtrS);
printf("%d\n",PtrS->Next->Data);
Push(14,PtrS);
Push(67,PtrS);
Stack S=PtrS;
for(i=0;i<3;i++){
S=S->Next;
printf("%d ",S->Data);
}
printf("\n");
Stack S2=PtrS;
printf("出栈为:%d\n",Pop(S2));
for(i=0;i<2;i++){
S2=S2->Next;
printf("%d ",S2->Data);
}
printf("\n");
return 0;
}