棧和佇列的基本操作實現
阿新 • • 發佈:2018-12-24
《資料結構》(c語言版)——第三章、棧和佇列
抽象資料型別——棧
順序儲存結構:
#include<stdio.h> #include<stdlib.h> #include<string.h> #define ok 1 #define error 0 #define overflow -1 #define STACK_INIT_SIZE 100 //儲存空間初始分配量 #define STACKINCREMENT 10 //儲存空間的分配增量 typedef int Status; typedef int ElemType; /*棧的順序儲存表示*/ typedef struct{ ElemType *base; ElemType *top; //棧頂指標 int stacksize; //當前已分配的儲存空間,以元素為單位 }SqStack; /*基本操作函式的原型說明*/ Status InitStack(SqStack &S); //構造空棧S Status GetTop(SqStack S,ElemType &e); //返回棧頂元素 Status Push(SqStack &S,ElemType e); //插入e為新的棧頂元素 Status Pop(SqStack &S,ElemType &e); //刪除棧頂元素用e返回 /*基本操作的演算法實現*/ Status InitStack(SqStack &S) { S.base=(ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType)); if(!S.base) exit(overflow); //儲存分配失敗 S.top=S.base; S.stacksize=STACK_INIT_SIZE; return ok; } Status GetTop(SqStack S,ElemType &e) { if(S.top==S.base) exit(overflow); e=*(S.top-1); return ok; } Status Push(SqStack &S,ElemType e) { if(S.top-S.base>=S.stacksize) //棧滿,追加儲存結構 { S.base=(ElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(ElemType)); if(!S.base) exit(overflow); //儲存分配失敗 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 /*簡單的測試函式*/ int main() { SqStack S; InitStack(S); int i,n; ElemType e; printf("Please input the SqStack's length: "); scanf("%d",&n); printf("\nThe sequence of pushstack is \n"); for(i=0;i<n;i++) { Push(S,i*2); printf("%d ",i*2); } printf("\n\n"); printf("The top of stack is \n"); GetTop(S,e); printf("%d\n\n",e); printf("The sequence of popstack is \n"); for(i=0;i<n;i++) { Pop(S,e); printf("%d ",e); } return 0; }
抽象資料型別——佇列
單鏈佇列(鏈式儲存)
#include<stdio.h> #include<stdlib.h> #include<string.h> #define ok 1 #define error 0 #define overflow -1 typedef int Status; typedef int ElemType; /*單鏈佇列的鏈式儲存結構*/ typedef struct QNode { ElemType data; struct QNode *next; }QNode,*QueuePtr; typedef struct { QueuePtr front; //隊頭指標 QueuePtr rear; //隊尾指標 }LinkQueue; /*基本操作函式的原型說明*/ Status InitQueue(LinkQueue &Q); //構造一個空佇列 Status DestroyQueue(LinkQueue &Q); //銷燬佇列Q Status EnQueue(LinkQueue &Q,ElemType e); //插入元素e作隊尾 Status DeQueue(LinkQueue &Q,ElemType &e); //若佇列不空,刪除隊頭元素,用e返回 /*基本操作函式的實現*/ Status InitQueue(LinkQueue &Q) { Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode)); if(!Q.front) exit(overflow); //儲存分配失敗 Q.front->next=NULL; return ok; } Status DestroyQueue(LinkQueue &Q) { while(Q.front) { Q.rear=Q.front->next; free(Q.front); Q.front=Q.rear; } return ok; } Status EnQueue(LinkQueue &Q,ElemType e) { QueuePtr p; p=(QueuePtr)malloc(sizeof(QNode)); if(!p) exit(overflow); //儲存分配失敗 p->data=e; p->next=NULL; Q.rear->next=p; Q.rear=p; return ok; } Status DeQueue(LinkQueue &Q,ElemType &e) { QueuePtr 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; free(p); return ok; } /*簡單的測試函式*/ int main() { LinkQueue Q; InitQueue(Q); int i,n; ElemType e; printf("please input the sequence's length : "); scanf("%d",&n); printf("\nThe sequence of enqueue is \n"); for(i=0;i<n;i++) { EnQueue(Q,2*i); printf("%d ",2*i); } printf("\n\n"); printf("The sequence of dequeue is \n"); for(i=0;i<n;i++) { DeQueue(Q,e); printf("%d ",e); } printf("\n\n"); printf("Destroy queue! \n"); DestroyQueue(Q); return 0; }
迴圈佇列(順序儲存)
#include<stdio.h> #include<stdlib.h> #include<string.h> #define ok 1 #define error 0 #define overflow -1 #define MAXQSIZE 100 //最大佇列長度 typedef int Status; typedef int ElemType; /*迴圈佇列的順序儲存結構*/ typedef struct{ ElemType *base; //初始化的動態分配儲存空間 int front; //頭指標,若佇列不空,指向佇列的頭元素 int rear; //尾指標,若佇列不空,指向佇列尾元素的下一個位置 }SqQueue; /*迴圈佇列的基本函式原型說明*/ Status InitQueue(SqQueue &Q); //構造一個空佇列 Status QueueLength(SqQueue Q); //返回佇列的長度 Status EnQueue(SqQueue &Q,ElemType e); //插入元素e為新的隊尾元素 Status DeQueue(SqQueue &Q,ElemType &e); //若佇列不空,刪除隊頭元素,用e返回 /*迴圈佇列的基本操作演算法實現*/ Status InitQueue(SqQueue &Q) { Q.base=(ElemType *)malloc(MAXQSIZE * sizeof(ElemType)); if(!Q.base) exit(overflow); //儲存分配失敗 Q.front=Q.rear=0; return ok; } Status QueueLength(SqQueue Q) { return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE; } Status EnQueue(SqQueue &Q,ElemType e) { if((Q.rear+1)%MAXQSIZE==Q.front) return error; //佇列滿 Q.base[Q.rear]=e; Q.rear=(Q.rear+1)%MAXQSIZE; //隊尾指標加1 return ok; } Status DeQueue(SqQueue &Q,ElemType &e) { if(Q.front==Q.rear) return error; //隊空 e=Q.base[Q.front]; Q.front=(Q.front+1)%MAXQSIZE; //隊頭指標加1 return ok; } /*簡單測試函式*/ int main() { SqQueue Q; InitQueue(Q); int i,n; ElemType e; //printf("please input the sequence's length : "); //scanf("%d",&n); printf("The sequence of enqueue is \n"); for(i=0;i<10;i++) { EnQueue(Q,2*i); printf("%d ",2*i); } printf("\n\n"); printf("The sequence's length: \n"); n=QueueLength(Q); printf("%d\n\n",n); printf("The sequence of dequeue is \n"); for(i=0;i<10;i++) { DeQueue(Q,e); printf("%d ",e); } printf("\n\n"); printf("The sequence's length: \n"); n=QueueLength(Q); printf("%d\n\n",n); return 0; }