1.顺序堆栈
#include<stdio.h>
#include<stdlib.h>
#define MAX 100
typedef int ElementType;
ElementType ERROR=-1;
typedef struct SNode *Stack;
struct SNode{
ElementType data[MAX];
int top;
};
Stack Creat()
{
Stack p = (Stack)malloc(sizeof(struct SNode));
p->top = -1;
return p;
}
void Push(Stack PtrS,ElementType item)
{
if(PtrS->top == MAX-1)
{
printf("堆栈满");
return ;
}
else
{
PtrS->data[++(PtrS->top)] = item;
return ;
}
}
ElementType Pop(Stack PtrS)
{
if(PtrS->top == -1)
{
printf("堆栈空");
return ERROR;
}
else
return (PtrS->data[(PtrS->top)--]);
}
ElementType GetTop(Stack PtrS)
{
if(PtrS->top == -1)
{
printf("堆栈空");
return ERROR;
}
else
return (PtrS->data[(PtrS->top)]);
}
bool Empty(Stack PtrS)
{
if(PtrS->top == -1){
return true;
}else{
return false;
}
}
void clear(Stack PtrS)
{
PtrS->top = -1;
}
int main()
{
Stack S=Creat();
Push(S,5);
Push(S,15);
printf("%d\n",Pop(S));
printf("%d\n",GetTop(S));
Push(S,25);
if(!Empty(S))
printf("当前栈不为空\n");
else
printf("当前栈为空\n");
clear(S);
if(!Empty(S))
printf("当前栈不为空\n");
else
printf("当前栈为空\n");
}
2.双端堆栈
#include<stdio.h>
#include<stdlib.h>
#define MAX 100
typedef int ElementType;
ElementType ERROR=-1;
typedef struct DSNode{
ElementType data[MAX];
int top1;
int top2;
}DStack;
DStack *Creat()
{
DStack* p = (DStack*)malloc(sizeof(DStack));
p ->top1 = -1;
p->top2 = MAX-1;
return p;
}
void Push(DStack* S,ElementType item,int Tag)
{
if(S->top2 - S->top1 == 1)
{
printf("堆栈满");
return ;
}
if(Tag == 1)
S->data[++(S->top1)] = item;
else
S->data[--(S->top2)] = item;
}
ElementType Pop(DStack* S, int Tag)
{
if(Tag == 1)
{
if(S->top1 == -1)
{
printf("堆栈1为空");
return ERROR;
}
else
return S->data[S->top1--];
}
else
{
if(S->top2 == MAX-1)
{
printf("堆栈2为空");
return ERROR;
}
else
return S->data[S->top2++];
}
}
ElementType GetTop(DStack* S,int Tag)
{
if(Tag == 1)
{
if(S->top1 == -1)
{
printf("堆栈1为空");
return ERROR;
}
else
return S->data[S->top1];
}
else
{
if(S->top2 == MAX-1)
{
printf("堆栈2为空");
return ERROR;
}
else
return S->data[S->top2];
}
}
bool Empty(DStack* S,int Tag)
{
if(Tag == 1)
{
if(S->top1 == -1)
return true;
}
if(Tag == 2)
{
if(S->top2 == MAX - 1)
return true;
}
return false;
}
void Clear(DStack* S,int Tag)
{
if(Tag == 1)
S->top1 == -1;
if(Tag == 2)
S->top2 == MAX - 1;
}
int main()
{
DStack *S = Creat();
Push(S,15,1);
Push(S,25,2);
printf("%d\n",GetTop(S,2));
printf("%d\n",GetTop(S,1));
Pop(S,1);
Pop(S,2);
printf("%d\n",GetTop(S,2));
printf("%d\n",GetTop(S,1));
if(!Empty(S,1))
printf("当前栈1不为空\n");
else
printf("当前栈1为空\n");
Push(S,15,1);
Push(S,25,2);
if(!Empty(S,2))
printf("当前栈2不为空\n");
else
printf("当前栈2为空\n");
}
3.链表堆栈
#include<stdio.h>
#include<stdlib.h>
typedef int ElementType;
ElementType ERROR = -1;
typedef struct SNode *Stack;
struct SNode{
ElementType data;
struct SNode *next;
};
Stack Creat()
{
Stack S;
S = (Stack)malloc(sizeof(struct SNode));
S->next = NULL;
return S;
}
int IsEmpty(Stack S)
{
return S->next == NULL;
}
void Push(Stack S,ElementType item)
{
Stack NewNode = (Stack)malloc(sizeof(struct SNode));
NewNode->data = item;
NewNode->next = S->next;
S->next = NewNode;
}
ElementType Pop(Stack S)
{
Stack temp;
ElementType result;
if(!IsEmpty(S))
{
temp = S->next;
S->next = temp->next;
result = temp->data;
free(temp);
return result;
}
else
{
printf("当前栈为空\n");
return ERROR;
}
}
void Clear(Stack S)
{
S->next = NULL;
}
int main()
{
Stack S;
S = Creat();
Push(S,5);
Push(S,15);
printf("%d\n",Pop(S));
printf("%d\n",Pop(S));
Push(S,15);
if(!IsEmpty(S))
printf("当前栈不为空\n");
else
printf("当前栈为空\n");
Clear(S);
if(!IsEmpty(S))
printf("当前栈不为空\n");
else
printf("当前栈为空\n");
}