栈:
#include<stdio.h>
#include<stdlib.h>
typedef struct SNode *PtrToSNode;
typedef int ElementType;
typedef int Position;
struct SNode{
ElementType *Data;
Position Top;
int MAXSIZE;
};
typedef PtrToSNode Stack;
Stack MakeEmpty(int Maxsize){
Stack S;
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){
if (S->Top == S->MAXSIZE - 1)
return true;
else
return false;
}
bool Push(Stack S,ElementType x){
if(IsFull(S)){
printf("栈满!\n");
return false;
}
else{
S->Data[++S->Top] = x;
printf("%d入栈成功!\n",x);
return true;
}
}
bool IsEmpty(Stack S){
return (S->Top == -1);
}
bool Pop(Stack S){
if(IsEmpty(S))
{
printf("栈为空\n");
return false;
}
else
{
printf("%d出栈成功!\n",S->Data[S->Top--]);
return true;
}
}
void main(){
Stack S;
S = MakeEmpty(5);
Push(S,1);
Push(S,2);
Push(S,3);
Push(S,4);
Push(S,5);
Push(S,6);
printf("=========\n");
Pop(S);
Pop(S);
Pop(S);
Pop(S);
Pop(S);
Pop(S);
Pop(S);
}
循环栈:
从两头向中 - - - - > > > < < < - - - -
#include<stdio.h>
#include<stdlib.h>
typedef struct SNode *PtrToSNode;
typedef int ElementType;
typedef int Position;
struct SNode{
ElementType *Data;
Position Top1;
int Top2;
int MAXSIZE;
};
typedef PtrToSNode Stack;
Stack MakeEmpty(int Maxsize){
Stack S;
S = (Stack)malloc(sizeof(struct SNode));
S->Data = (ElementType*)malloc(Maxsize*sizeof(ElementType));
S->Top1 = -1;
S->Top2 = Maxsize;
S->MAXSIZE =Maxsize;
return S;
}
bool IsFull(Stack S){
return (S->Top2 - S->Top1 == 1);
}
bool Push(Stack S,int flag,ElementType x){
if(IsFull(S)){
printf("栈满!\n");
return false;
}
else if(flag==0){
S->Data[++S->Top1] = x;
printf("%d入栈成功!\n",x);
return true;
}else if(flag ==1){
S->Data[--S->Top2] =x;
printf("%d入栈成功!\n",x);
return true;
}else
{
printf("进栈方式错误!\n");
return false;
}
}
bool Pop(Stack S,int flag){
if(flag == 0)
{
if(S->Top1 != -1)
{
printf("%d出栈成功!\n",S->Data[S->Top1--]);
return true;
}
else
{
printf("栈为空\n");
return false;
}
}
if(flag == 1){
if(S->Top2 == S->MAXSIZE)
{
printf("栈为空\n");
return false;
}
else
{
printf("%d出栈成功!\n",S->Data[S->Top2++]);
return true;
}
}
printf("出栈方式错误!\n");
return false;
}
void main(){
Stack S;
S = MakeEmpty(5);
Push(S,9999,6);
Push(S,0,1);
Push(S,1,5);
Push(S,0,2);
Push(S,0,3);
Push(S,1,4);
Push(S,0,6);
Push(S,1,6);
printf("=======\n");
Pop(S,0);
Pop(S,0);
Pop(S,0);
Pop(S,1);
Pop(S,1);
Pop(S,0);
Pop(S,1);
Pop(S,9999);
}
栈链式存储:
#include<stdio.h>
#include<stdlib.h>
typedef struct SNode *PtrToSNode;
typedef int ElementType;
typedef int Position;
struct SNode{
ElementType Data;
PtrToSNode Next;
};
typedef PtrToSNode Stack;
Stack MakeEmpty(int Maxsize){
Stack S;
S = (Stack)malloc(sizeof(struct SNode));
S->Next = NULL;
return S;
}
bool Push(Stack S,ElementType x){
Stack Tmp;
Tmp = (Stack)malloc(sizeof(struct SNode));
Tmp->Data = x;
Tmp->Next = S->Next;
S->Next = Tmp;
printf("%d入栈成功\n",x);
return true;
}
bool Pop(Stack S){
Stack Tmp;
if(S->Next == NULL)
{
printf("栈为空\n");
return false;
}
else{
Tmp = S->Next;
printf("%d出栈成功\n",Tmp->Data);
S->Next = Tmp ->Next;
free(Tmp);
return true;
}
}
void main(){
Stack S;
S = MakeEmpty(5);
Push(S,1);
printf("=======\n");
Push(S,2);
Push(S,3);
Pop(S);
Pop(S);
Pop(S);
printf("=======\n");
Pop(S);
Pop(S);
}
堆栈应用:。。。。。。。
#include <stdio.h> #include <stdlib.h> #include <ctype.h> #define MAXOP 100 #define ERROR -1 #define INFINITY 9999999 typedef struct SNode *PtrToSNode; typedef double ElementType; typedef int Position; typedef enum { num, opr , end} Type; struct SNode{ ElementType *Data; Position Top; int MAXSize; }; typedef PtrToSNode Stack; 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)) {printf("栈满"); return false; } else{ s->Data[++s->Top] = x; printf("%d ",x); return true; } } bool IsEmpty(Stack s){ return (s->Top == -1); } ElementType Pop(Stack s){ if(IsEmpty(s)){ printf("堆栈空"); return ERROR; } else return (s->Data[s->Top--]); } Type GetOp(char *Expr ,int *start ,char *str){ int i = 0; while((str[0] = Expr[(*start)++]) == ' '); while(str[1] != ' ' && str[i]!= '\0') str[i++] = Expr[(*start)++]; if(str[1] == '\0') (*start)--; str[i] = '\0'; if(i==0) return end; else if(isdigit(str[0])||isdigit(str[1])) return num; else return opr; } ElementType PostfixExp(char * Expr){ Stack S; Type T; ElementType Op1,Op2; char str[MAXOP]; int start = 0; S = CreateStack(MAXOP); Op1 = Op2 = 0; while(T = GetOp(Expr,&start,str)){ if(T == num) Push(S,atof(str)); else{ if(!IsEmpty(S)) Op2 =Pop(S); else Op2 = INFINITY; switch(str[0]){ case '+': Push(S,Op1+Op1);break; case '-': Push(S,Op1-Op2);break; case '*': Push(S,Op1*Op2);break; case '/': if(Op2!=0.0) Push(S,Op1/Op2); else { printf("分母为零\n"); Op2 = INFINITY; }; break; default: printf("错误;未知运算符\n"); Op2 = INFINITY; break; } if(Op2 >= INFINITY) break; } } if(Op2<INFINITY) if(!IsEmpty(S)) Op2 = Pop(S); else Op2 = INFINITY; free(S); return Op2; } int main(){ char Expr[MAXOP]; ElementType f; gets(Expr); f = PostfixExp(Expr); if(f<INFINITY) printf("%.4f\n",f); else printf("表达式错误"); return 0; }