#include<stdio.h>#include<malloc.h>#defineERROR0#defineOK1#defineTrue1#defineFalse0#defineSTACK_INT_SIZE10/*存储空间初始分配量*/#defineSTACKINCREMENT5/*存储空间分配增量*/typedefint ElemType;/*定义元素的类型*/typedefint Status;/*定义元素的类型*/typedefstruct{
ElemType *base;
ElemType *top;int stacksize;/*当前已分配的存储空间*/}SqStack;
Status InitStack(SqStack &S);/*构造空栈*/
Status push(SqStack &S,ElemType e);/*入栈*/
Status Pop(SqStack &S,ElemType &e);/*出栈*/
Status GetTop(SqStack S, ElemType &e);/*取顺序栈栈顶元素*/
Status StackEmpty(SqStack S);/*判断顺序栈是否为空*/intStackLength( SqStack S );/*求顺序栈的长度*/
Status CreateStack(SqStack &S);/*创建栈*/voidPrintStack(SqStack &S);/*出栈并输出栈中元素*/
Status DestroyStack( SqStack &S );/*销毁栈*/
Status InitStack(SqStack &S){
S.base=(ElemType *)malloc(STACK_INT_SIZE *sizeof(ElemType));if(!S.base)return ERROR;
S.top=S.base;
S.stacksize=STACK_INT_SIZE;return OK;}/*InitStack*/
Status Push(SqStack &S,ElemType e)/*入栈*/{while(S.top-S.base>=S.stacksize){
S.base=(ElemType *)realloc(S.base,(S.stacksize + STACKINCREMENT)*sizeof(ElemType));if(!S.base)return False;
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;}*S.top++=e;return OK;}/*Push*/
Status Pop(SqStack &S,ElemType &e)/*出栈*/{if(S.top ==S.base)return ERROR;
e =*--S.top;return OK;}/*Pop*/
Status GetTop(SqStack S, ElemType &e)/*取顺序栈栈顶元素*/{if(S.top ==S.base)return ERROR;
e=*(S.top -1);return OK;}/*GetTop*/
Status StackEmpty(SqStack S)/*判断顺序栈是否为空*/{if(S.top==S.base)return True;elsereturn ERROR;}/*StackEmpty*/intStackLength( SqStack S )/*求顺序栈的长度*/{return S.top-S.base;}/*StackLength*/
Status CreateStack(SqStack &S)/*创建栈*/{int e;if(InitStack(S))printf("Init Success!\n");else{printf("Init Fail!\n");return ERROR;}printf("input data:(Terminated by inputing a character)\n");while(scanf("%d",&e))Push(S,e);return OK;}/*CreateStack*/voidPrintStack(SqStack &S){
ElemType e;while(Pop(S,e))printf("%3d",e);printf("\n");}/*Pop_and_Print*/
Status DestroyStack(SqStack &S)/*销毁栈*/{if( S.base ){free(S.base);
S.stacksize =0;
S.base = S.top =NULL;}return OK;}intmain(){
SqStack ss;
ElemType e;printf("\ncreate Stack\n");CreateStack(ss);GetTop(ss,e);printf("\nData on the top of the Stack is %d\n",e);printf("\n");if(StackEmpty(ss))printf("\nthe Stack is empty.\n");elseprintf("\nthe Stack is not empty.\n");printf("\n");printf("\nThere are %d datas in the Stack \n",StackLength(ss));printf("\n");printf("\nPop&Print\n");PrintStack(ss);if(StackEmpty(ss))printf("\nthe Stack is empty.\n");elseprintf("\nthe Stack is not empty.\n");printf("\n");DestroyStack(ss);return0;}
2.链栈
#include<stdio.h>#include<malloc.h>#defineERROR0#defineOK1#defineTrue1#defineFalse0typedefint SElemType;/*定义元素的类型*/typedefint Status;typedefstructStackNode{
SElemType data;structStackNode*next;} StackNode,*LinkStack;
Status InitStack(LinkStack &S);/*构造空栈*/
Status push(LinkStack &S,SElemType e);/*入栈*/
Status Pop(LinkStack &S,SElemType &e);/*出栈*/
Status GetTop(LinkStack S, SElemType &e);/*取链栈栈顶元素*/
Status StackEmpty(LinkStack S);/*判断链栈是否为空*/intStackLength(LinkStack S );/*求链栈的长度*/
Status CreateStack(LinkStack &S);/*创建栈 栈中元素依次入栈*/voidPrintStack(LinkStack &S);/*出栈并输出栈中元素*/
Status DestroyStack(LinkStack &S );/*销毁栈*/
Status InitStack(LinkStack &S)/*构造空栈*/{
S=NULL;return True;}
Status push(LinkStack &S,SElemType e)/*入栈*/{
StackNode *p;
p=(StackNode *)malloc(sizeof(StackNode));if(!p)return False;
p->data=e;
p->next=S;
S=p;return OK;}
Status Pop(LinkStack &S,SElemType &e)/*出栈*/{
StackNode *p;if(S==NULL)return ERROR;
e = S->data;
p=S;
S=S->next;free(p);return OK;}
Status GetTop(LinkStack S, SElemType &e)/*取链栈栈顶元素*/{if(S==NULL)return ERROR;elsereturn e=S->data;}
Status StackEmpty(LinkStack S)/*判断链栈是否为空*/{if(S==NULL)return True;elsereturn ERROR;}intStackLength(LinkStack S )/*求链栈的长度*/{int j=0;while(S!=NULL){
j++;
S=S->next;}return j;}
Status CreateStack(LinkStack &S)/*创建栈 栈中元素依次入栈*/{int e;if(InitStack(S))printf("Init Success!\n");else{printf("Init Fail!\n");return ERROR;}printf("input data:\n");while(scanf("%d",&e))push(S,e);return OK;}voidPrintStack(LinkStack &S)/*出栈并输出栈中元素*/{
SElemType e;while(Pop(S,e)){printf("%d",e);}}
Status DestroyStack(LinkStack &S )/*销毁栈*/{while(S){free(S);}return OK;}intmain(){
LinkStack ss;
SElemType e;printf("\ncreate Stack\n");CreateStack(ss);GetTop(ss,e);printf("\nData on the top of the Stack is %d\n",e);printf("\n");if(StackEmpty(ss))printf("\nthe Stack is empty.\n");elseprintf("\nthe Stack is not empty.\n");printf("\n");printf("\nThere are %d datas in the Stack \n",StackLength(ss));printf("\n");printf("\nPop&Print\n");PrintStack(ss);if(StackEmpty(ss))printf("\nthe Stack is empty.\n");elseprintf("\nthe Stack is not empty.\n");printf("\n");DestroyStack(ss);return0;}
3.链队列
#include<stdio.h>#include<malloc.h>#defineERROR0#defineOK1#defineTrue1#defineFalse0typedefint QElemType;/*定义元素的类型*/typedefint Status;typedefstructQNode{//结点类型
QElemType data ;structQNode*next ;}QNode,*QueuePtr;typedefstruct{// 链队列类型
QueuePtr front;//队头指针
QueuePtr rear;//队尾指针}LinkQueue;
Status InitQueue(LinkQueue &Q);/*初始化链队列*/
Status EnQueue(LinkQueue &Q, QElemType e);/*入队*/
Status DeQueue(LinkQueue &Q, QElemType &e);/*出队*/
Status GetHead(LinkQueue Q, QElemType &e);/*取链队列的队头元素*/
Status QueueEmpty(LinkQueue Q);/*判断链队列是否为空*/intQueueLength(LinkQueue Q);/*求队长*/
Status CreateQueue(LinkQueue &Q);/*创建链队列 链队列中元素依次入队*/voidPrintQueue(LinkQueue &Q);/*输出链队列中元素*/
Status DestroyQueue(LinkQueue &Q);/*销毁链队列*/
Status InitQueue(LinkQueue &Q)/*初始化链队列*/{
Q.front=Q.rear=(QNode *)malloc(sizeof(QNode));if(!Q.front)return False;
Q.front->next=NULL;return OK;}
Status EnQueue(LinkQueue &Q, QElemType e)/*入队*/{
QNode *p;
p=(QNode *)malloc(sizeof(QNode));if(!p)return False;
p->data=e;
p->next=NULL;
Q.rear->next=p;
Q.rear=p;return OK;}
Status DeQueue(LinkQueue &Q, QElemType &e)/*出队*/{
QNode *p;if(Q.front==Q.rear)return ERROR;
p=Q.front->next;
e=p->data;
Q.front->next=p->next;if(Q.rear==p)Q.rear=Q.front;//Q.rear=Q.front不可换边,程序提前结束free(p);return OK;}
Status GetHead(LinkQueue Q, QElemType &e)/*取链队列的队头元素*/{
QNode *p;if(Q.front==Q.rear)return ERROR;
p=Q.front->next;
e=p->data;
Q.front->next=p->next;if(Q.rear==p)Q.front=Q.rear;free(p);return OK;}
Status QueueEmpty(LinkQueue Q)/*判断链队列是否为空*/{if(Q.front==Q.rear)return True;elsereturn ERROR;}intQueueLength(LinkQueue Q)/*求队长*/{
QNode *p;
QElemType j=0;while(Q.front){
p=Q.front->next;
j++;
Q.front=Q.front->next;}return j;}
Status CreateQueue(LinkQueue &Q)/*创建链队列 链队列中元素依次入队*/{
QElemType e;if(InitQueue(Q))printf("Init Success!\n");else{printf("Init Fail!\n");return ERROR;}printf("input data:\n");while(scanf("%d",&e))EnQueue(Q,e);return OK;}voidPrintQueue(LinkQueue &Q)/*输出链队列中元素*/{
QElemType e;while(DeQueue(Q,e))printf(" %d ",e);}
Status DestroyQueue(LinkQueue &Q)/*销毁链队列*/{while(Q.front){
Q.rear=Q.front->next;free(Q.front);
Q.front=Q.rear;}return OK;}intmain(){
LinkQueue ss;
QElemType e;printf("\ncreate Queue\n");CreateQueue(ss);GetHead(ss,e);printf("\nData on the ahead of the Queue is %d\n",e);printf("\n");if(QueueEmpty(ss))printf("\nthe Queue is empty.\n");elseprintf("\nthe Queue is not empty.\n");printf("\n");printf("\nThere are %d datas in the Queue \n",QueueLength(ss));printf("\n");printf("\n DeQueue & Print\n");PrintQueue(ss);if(QueueLength(ss))printf("\nthe Queue is empty.\n");elseprintf("\nthe Queue is not empty.\n");printf("\n");DestroyQueue(ss);return0;}
4.循环队列
#include<stdio.h>#include<malloc.h>#defineERROR0#defineOK1#defineTrue1#defineFalse0#defineMAXQSIZE10// 最大队列长度typedefint QElemType;/*定义元素的类型*/typedefint Status;typedefstruct{
QElemType *base;// 动态分配存储空间基址int front;//队头指针 int rear;//队尾指针} SqQueue;
Status InitQueue(SqQueue &Q);/*初始化循环队列*/
Status EnQueue(SqQueue &Q, QElemType e);/*入队*/
Status DeQueue(SqQueue &Q, QElemType &e);/*出队*/
Status GetHead(SqQueue Q, QElemType &e);/*取循环队列的队头元素*/
Status QueueEmpty(SqQueue Q);/*判断循环队列是否为空*/intQueueLength(SqQueue Q);/*求队长*/
Status CreateQueue(SqQueue &Q);/*创建循环队列 循环队列中元素依次入队*/voidPrintQueue(SqQueue &Q);/*输出循环队列中元素*/
Status DestroyQueue(SqQueue &Q);/*销毁栈循环队列*/
Status InitQueue(SqQueue &Q)/*初始化循环队列*/{
Q.base=(QElemType *)malloc(MAXQSIZE *sizeof(QElemType));if(!Q.base)return False;
Q.front=Q.rear=0;return OK;}
Status EnQueue(SqQueue &Q, QElemType e)/*入队*/{if((Q.rear+1)%MAXQSIZE==Q.front)return ERROR;
Q.base[Q.rear]= e;
Q.rear=(Q.rear+1)%MAXQSIZE;return OK;}
Status DeQueue(SqQueue &Q, QElemType &e)/*出队*/{if(Q.front==Q.rear)return ERROR;
e=Q.base[Q.front];
Q.front=(Q.front+1)%MAXQSIZE;return OK;}
Status GetHead(SqQueue Q, QElemType &e)/*取循环队列的队头元素*/{if(Q.front==Q.rear)return ERROR;
e = Q.base[Q.front];
Q.front=(Q.front+1)%MAXQSIZE;return OK;}
Status QueueEmpty(SqQueue Q)/*判断循环队列是否为空*/{if(Q.front==Q.rear)return True;elsereturn ERROR;}intQueueLength(SqQueue Q)/*求队长*/{return(Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;}
Status CreateQueue(SqQueue &Q)/*创建循环队列 循环队列中元素依次入队*/{
QElemType e;if(InitQueue(Q))printf("Init Success!\n");else{printf("Init Fail!\n");return ERROR;}printf("input data:\n");while(scanf("%d",&e))EnQueue(Q,e);return OK;}voidPrintQueue(SqQueue &Q)/*输出循环队列中元素*/{
QElemType e;while(DeQueue(Q,e))printf("%3d",e);printf("\n");}
Status DestroyQueue(SqQueue &Q)/*销毁栈循环队列*/{while( Q.front!= Q.rear){free(&Q.base[Q.front]);
Q.front=(Q.front+1)%MAXQSIZE;}return OK;}intmain(){
SqQueue ss;
QElemType e;printf("\ncreate Queue\n");CreateQueue(ss);GetHead(ss,e);printf("\nData on the ahead of the Queue is %d\n",e);printf("\n");if(QueueEmpty(ss))printf("\nthe Queue is empty.\n");elseprintf("\nthe Queue is not empty.\n");printf("\n");printf("\nThere are %d datas in the Queue \n",QueueLength(ss));printf("\n");printf("\n DeQueue & Print\n");PrintQueue(ss);DestroyQueue(ss);printf("\nthe Queue is destroy......\n");printf("\n");return0;}