




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、DONGFANG COLLEGE,F(xiàn)UJIAN AGRICULTURE AND FORESTRY UNIVERSITY課程名稱:數(shù)據(jù)結(jié)構系 別:計算機系年級專業(yè):2010級電子信息工程學 號: 1050302103姓名:廖少兵任課教師:謝儲輝成績:2012年12月25日實驗一線性表及其應用【實驗目的】1. 熟練掌握線性表的基本操作在順序存儲和鏈式存儲上的實現(xiàn);2. 以線性表的各種操作(建立、插入、刪除、遍歷等)的實現(xiàn)為重點;3. 掌握線性表的動態(tài)分配順序存儲結(jié)構的定義和基本操作的實現(xiàn);4. 通過本章實驗幫助學生加深對 C 語言的使用(特別是函數(shù)的參數(shù)調(diào)用、指針類型的應用和鏈表的建立等各種基本操
2、作)。【實驗內(nèi)容】1 線性表順序存儲的基本操作參考程序:/*線性表順序存儲的基本操作*/#include <stdio.h>#define MaxSize 50typedef char ElemType;struct ListElemType listMaxSize;int size;void setnull(struct List *p)p->size=0;int length(struct List *p)return(p->size);int get(struct List *p,int i)if (i>p->size)return(-1);elser
3、eturn(p->listi-1);int locate(struct List *p,ElemType x)int i=0;while (i<p->size && p->listi!=x) i+;if (i=p->size)return(-1);elsereturn(i+1);void insert(struct List *p,ElemType x,int i)int j;if (i<1 && i>p->size+1)printf("位置參數(shù)不正確,不能進行插入操作!n");elsep-&g
4、t;size+;for (j=p->size-1;j>=i;j-) /*結(jié)點向后移動,騰出一個位置*/p->listj=p->listj-1;p->listj=x;void delete(struct List *p,int i)int j;if (i>p->size | i<1)printf("位置參數(shù)不正確,不能進行刪除操作!n");elsefor (j=i-1;j<p->size-1;j+) /*結(jié)點向前移動,覆蓋該刪除的結(jié)點*/p->listj=p->listj+1;p->size-;di
5、splay(struct List *p)int j;if (p->size=0)printf("該線性表為空,不能顯示!n");elseprintf("線性表:");if (p->size=1) /*只有一個結(jié)點的情況*/printf("%c",p->listp->size);else /*有一個以上結(jié)點的情況*/for (j=0;j<p->size-1;j+)printf("%c",p->listj);printf("%c",p->listj)
6、; /*顯示最后一個結(jié)點*/printf("n");main()struct List L;setnull(&L);insert(&L,'a',1);insert(&L,'b',2);insert(&L,'a',1);insert(&L,'c',2);insert(&L,'d',1);insert(&L,'e',2);display(&L);printf("值:%c 位置:%dn",'a&
7、#39;,locate(&L,'a');printf("位置:%d 值:%cn",4,get(&L,4);printf("刪除第 2 個結(jié)點后");delete(&L,2);display(&L);printf("刪除第 2 個結(jié)點后");delete(&L,2);display(&L);printf("刪除第 1 個結(jié)點后");delete(&L,1);display(&L);printf("刪除第 1 個結(jié)點后"
8、);delete(&L,1);display(&L);2 線性表鏈式存儲的基本操作/*線性表鏈式存儲-單鏈表的基本操作*/#include <stdio.h>#include <malloc.h>typedef char ElemType;struct LNodeElemType data;struct LNode *next;setnull(struct LNode *p)*p=NULL;int length(struct LNode *p)int n=0;struct LNode *q=*p;while (q!=NULL)n+;q=q->nex
9、t;return(n);ElemType get(struct LNode *p,int i)int j=1;struct LNode *q=*p;while (j<i && q!=NULL) /*查找第 i 個結(jié)點*/q=q->next;j+;if (q!=NULL) /*找到了第 i 個結(jié)點*/return(q->data);elseprintf("位置參數(shù)不正確!n");int locate(struct LNode *p,ElemType x)int n=0;struct LNode *q=*p;while (q!=NULL &am
10、p;& q->data!=x) /*查找data 域為 x 的第一個結(jié)點*/q=q->next;n+;if (q=NULL) /*未找到 data 域等于 x 的結(jié)點*/return(-1);else /*找到data 域等于 x 的結(jié)點*/return(n+1);void insert(struct LNode *p,ElemType x,int i)int j=1;struct LNode *s,*q;s=(struct LNode *)malloc(sizeof(struct LNode); /*建立要插入的結(jié)點 s*/s->data=x;q=*p;if (i=
11、1) /*插入的結(jié)點作為頭結(jié)點*/s->next=q;*p=s;elsewhile (j<i-1 && q->next!=NULL) /*查找第 i-1 個結(jié)點*/q=q->next;j+;if (j=i-1) /*找到了第 i-1 個結(jié)點,由 q 指向它*/s->next=q->next; /*將結(jié)點 s 插入到 q 結(jié)點之后*/q->next=s;else printf("位置參數(shù)不正確!n");void delete(struct LNode *p,int i)int j=1;struct LNode *q=*
12、p,*t;if (i=1) /*刪除鏈表的頭結(jié)點*/t=q;*p=q->next;elsewhile (j<i-1 && q->next!=NULL) /*查找第 i-1 個結(jié)點*/q=q->next;j+;if (q->next!=NULL && j=i-1) /*找到第 i-1 個結(jié)點,由 q 指向它*/t=q->next; /*t 指向要刪除的結(jié)點*/q->next=t->next; /*將q 之后的結(jié)點刪除*/else printf("位置參數(shù)不正確!n");if (t!=NULL) /
13、*在 t 不為空時釋放該結(jié)點*/free(t);void display(struct LNode *p)struct LNode *q;q=*p;printf("單鏈表顯示:");if (q=NULL) /*鏈表為空時*/printf("鏈表為空!");else if (q->next=NULL) /*鏈表只有一個結(jié)點時*/printf("%cn",q->data);else /*鏈表存在一個以上的結(jié)點時*/while (q->next!=NULL) /*顯示前面的結(jié)點*/printf("%c"
14、,q->data);q=q->next;printf("%c",q->data); /*顯示最后一個結(jié)點*/printf("n");main()struct LNode *head;setnull(&head);insert(&head,'a',1);insert(&head,'b',2);insert(&head,'a',2);insert(&head,'c',4);insert(&head,'d',3);i
15、nsert(&head,'e',1);display(&head);printf("單鏈表長度=%dn",length(&head);printf("位置:%d 值:%cn",3,get(&head,3);printf("值:%c 位置:%dn",'a',locate(&head,'a');printf("刪除第 1 個結(jié)點:");delete(&head,1);display(&head);printf(&qu
16、ot;刪除第 5 個結(jié)點:");delete(&head,5);display(&head);printf("刪除開頭 3 個結(jié)點:");delete(&head,3);delete(&head,2);delete(&head,1);display(&head);3 雙鏈表的基本操作/*雙鏈表的基本操作*/#include <stdio.h>#include <malloc.h>typedef char ElemType;struct DNodeElemType data;struct DNo
17、de *left,*right;setnull(struct DNode *p)*p=NULL;int length(struct DNode *p)int n=0;struct DNode *q=*p;while (q!=NULL)n+;q=q->right;return(n);ElemType get(struct DNode *p,int i)int j=1;struct DNode *q=*p;while (j<i && q!=NULL) /*查找第 i 個結(jié)點*/q=q->right;j+;if (q!=NULL) /*找到了第 i 個結(jié)點*/ret
18、urn(q->data);elseprintf("位置參數(shù)不正確!n");int locate(struct DNode *p,ElemType x)int n=0;struct DNode *q=*p;while (q!=NULL && q->data!=x) /*查找data 域為 x 的第一個結(jié)點*/q=q->right;n+;if (q=NULL) /*未找到 data 域等于 x 的結(jié)點*/return(-1);else /*找到data 域等于 x 的結(jié)點*/return(n+1);void insert(struct DNod
19、e *p,ElemType x,int i)int j=1;struct DNode *s,*q;s=(struct DNode *)malloc(sizeof(struct DNode); /*建立要插入的結(jié)點 s*/s->data=x;s->left=s->right=NULL;q=*p;if (i=1) /*插入的結(jié)點作為表頭結(jié)點*/s->right=q;if (q!=NULL) /*原來的雙鏈表不為空*/q->left=s;*p=s;elsewhile (j<i-1 && q->right!=NULL) /*查找第i-1 個結(jié)點
20、*/q=q->right;j+;if (j=i-1) /*找到了第 i-1 個結(jié)點,由 q 指向它*/if (q->right!=NULL) /*q 不是最后一個結(jié)點*/s->right=q->right; /*將結(jié)點s 插入到q 結(jié)點和之后結(jié)點 q->right 之間*/q->right->left=s;s->left=q;q->right=s;else /*q 是最后一個結(jié)點*/s->left=q; /*將 s 作為表尾結(jié)點*/q->right=s;else printf("位置參數(shù)不正確!n");voi
21、d delete(struct DNode *p,int i)int j=1;struct DNode *q=*p,*t;if (i=1) /*刪除鏈表的頭結(jié)點*/t=q;q=q->right;if (q!=NULL) /*當雙鏈表不只一個結(jié)點時*/q->left=NULL;*p=q;elsewhile (j<i-1 && q->right!=NULL) /*查找第i-1 個結(jié)點*/q=q->right;j+;if (q->right!=NULL && j=i-1) /*找到第 i-1 個結(jié)點,由 q 指向它*/t=q->
22、;right; /*t 指向要刪除的結(jié)點*/if (t->right!=NULL) /*刪除的結(jié)點不是最后結(jié)點*/q->right=t->right; /*將 q 之后的結(jié)點刪除*/q->right->left=q;else /*刪除的結(jié)點是最后結(jié)點*/q->right=NULL;else printf("位置參數(shù)不正確!n");if (t!=NULL) /*在 t 不為空時釋放該結(jié)點*/free(t);void display(struct DNode *p)struct DNode *q;q=*p;printf("雙鏈表顯示
23、:");if (q=NULL) /*鏈表為空時*/printf("鏈表為空!");else if (q->right=NULL) /*鏈表只有一個結(jié)點時*/printf("%cn",q->data);else /*鏈表存在一個以上的結(jié)點時*/while (q->right!=NULL) /*顯示前面的結(jié)點*/printf("%c",q->data);q=q->right;printf("%c",q->data); /*顯示最后一個結(jié)點*/printf("n&q
24、uot;);main()struct DNode *dhead;setnull(&dhead);insert(&dhead,'a',1);insert(&dhead,'b',2);insert(&dhead,'a',2);insert(&dhead,'c',4);insert(&dhead,'d',3);insert(&dhead,'e',1);display(&dhead);printf("雙鏈表長度=%dn",l
25、ength(&dhead);printf("位置:%d 值:%cn",3,get(&dhead,3);printf("值:%c 位置:%dn",'a',locate(&dhead,'a');printf("刪除第 1 個結(jié)點:");delete(&dhead,1);display(&dhead);printf("刪除第 5 個結(jié)點:");delete(&dhead,5);display(&dhead);printf("
26、刪除開頭 3 個結(jié)點:");delete(&dhead,3);delete(&dhead,2);delete(&dhead,1);display(&dhead);4 順序表的應用約瑟夫問題的實現(xiàn)【問題描述】設有 n 個人圍坐在圓桌周圍,現(xiàn)從某個位置 m(1mn)上的人開始報數(shù),報數(shù)到 k的人就站出來。下一個人,即原來的第k+1 個位置上的人,又從1 開始報數(shù),再報數(shù)到 k的人站出來。依此重復下去,直到全部的人都站出來為止。試設計一個程序求出出列序列。這是一個使用循環(huán)鏈表的經(jīng)典問題。因為要不斷地出列,采用鏈表的存儲形式能更好地模擬出列的情況。【數(shù)據(jù)描述】
27、建立一個不帶頭結(jié)點的循環(huán)鏈表,其中的 n 個人用 n 個結(jié)點來表示。Typedef int Elemtype;Typedef struct Cnode Elemtype data;struct Cnode *next;CNode;【C 源程序】#include <stdio.h>#include <stdlib.h>#define NULL 0#define OK 1#define ERROR 0#define OVERFLOW -2typedef int Status;typedef int Elemtype;/*定義數(shù)據(jù)元素類型 */typedef struct C
28、node Elemtype data;struct Cnode *next;CNode;CNode *joseph;/*定義一個全局變量 */Status Create_clist(CNode *clist,int n) CNode *p,*q;int i;clist=NULL;for(i=n;i>=1;i-) p=(CNode *)malloc(sizeof(CNode);if(p=NULL)return OVERFLOW; /*存儲分配失敗 */p->data=i;p->next=clist;clist=p;if(i=n)q=p;/*用 q 指向鏈表的最后一個結(jié)點 */q
29、->next=clist; /*把鏈表的最后一個結(jié)點的鏈域指向鏈表的第一個結(jié)點,構成循環(huán)鏈表 */joseph=clist; /*把創(chuàng)建好的循環(huán)鏈表頭指針賦給全局變量 */return OK; /*end */Status Joseph(CNode *clist,int m,int n,int k) int i;CNode *p,*q;if(m>n) return ERROR;/*起始位置錯 */if(!Create_clist(clist,n)return ERROR; /*循環(huán)鏈表創(chuàng)建失敗 */p=joseph; /*p 指向創(chuàng)建好的循環(huán)鏈表 */for(i=1;i<m;
30、i+)p=p->next; /*p 指向 m 位置的結(jié)點 */while(p) for(i=1;i<k-1;i+)p=p->next; /* 找出第 k 個結(jié)點 */q=p->next;printf("%d ",q->data);/*輸出應出列的結(jié)點 */if(p->next=p)p=NULL; /*刪除最后一個結(jié)點 */else p->next=q->next;p=p->next;free(q); /*while */clist=NULL; /* end */void main( ) int m,n,k,i;CNode
31、 *clist;clist=NULL;/*初始化 clist */printf("n 請輸入圍坐在圓桌周圍的人數(shù) n:");scanf("%d",&n);printf("n 請輸入第一次開始報數(shù)人的位置 m:");scanf("%d",&m);printf("n 你希望報數(shù)到第幾個數(shù)的人出列?");scanf("%d",&k);Create_clist(clist,n);/*創(chuàng)建一個有n 個結(jié)點的循環(huán)鏈表 clist */printf("n
32、出列的順序如下:n");Joseph(clist,m,n,k);getch();/*main */【測試數(shù)據(jù)】1、請輸入圍坐在圓桌周圍的人數(shù) n:7請輸入第一次開始報數(shù)人的位置 m:3你希望報數(shù)到第幾個數(shù)的人出列? 2出列的順序如下:4 6 1 3 7 5 22、請輸入圍坐在圓桌周圍的人數(shù) n:13請輸入第一次開始報數(shù)人的位置 m:5你希望報數(shù)到第幾個數(shù)的人出列? 6出列的順序如下:10 3 9 4 12 7 5 2 6 11 8 1 133、請輸入圍坐在圓桌周圍的人數(shù) n:21請輸入第一次開始報數(shù)人的位置 m:3你希望報數(shù)到第幾個數(shù)的人出列? 9出列的順序如下:11 20 8 18
33、 7 19 10 2 15 9 4 1 21 3 6 14 12 13 5 16 17【實驗總結(jié)】熟練掌握線性表的基本操作在順序存儲和鏈式存儲上的實現(xiàn);以線性表的各種操作(建立、插入、刪除、遍歷等)的實現(xiàn)為重點;掌握線性表的動態(tài)分配順序存儲結(jié)構的定義和基本操作的實現(xiàn);通過本章實驗幫助學生加深對 C 語言的使用(特別是函數(shù)的參數(shù)調(diào)用、指針類型的應用和鏈表的建立等各種基本操作)通過這個實驗讓我體會到做實驗時,一定要親力親為,務必要將每個步驟,每個細節(jié)弄清楚,弄明白,實驗后,還要復習,思考,這樣,你的印象才深刻,記得才牢固。實驗二棧和隊列的應用【實驗目的】1熟練掌握棧和隊列的結(jié)構,以及這兩種數(shù)據(jù)結(jié)構
34、的特點;2能夠在兩種存儲結(jié)構上實現(xiàn)棧的基本運算,特別注意棧滿和??盏呐袛鄺l件及描述方法;3熟練掌握鏈隊列和循環(huán)隊列的基本運算,并特別注意隊列滿和隊列空的判斷條件和描述【實驗內(nèi)容】1順序棧的基本操作/*順序棧的基本操作*/#include <stdio.h>#define MaxSize 100typedef char ElemType;typedef structchar stackMaxSize;int top; stacktype;void initstack(stacktype *S)S->top=-1;void push(stacktype *S,ElemType x
35、)if (S->top=MaxSize) printf("棧上溢出!n");elseS->top+;S->stackS->top=x;void pop(stacktype *S)if (S->top=-1) printf("棧下溢出!n");else S->top-;ElemType gettop(stacktype *S)if (S->top=-1) printf("???n");else return(S->stackS->top);int empty(stacktype *S
36、)if (S->top=-1) return(1);else return(0);void display(stacktype *S)int i;printf("棧中元素:");for (i=S->top;i>=0;i-)printf("%c ",S->stacki);printf("n");main()stacktype *st;printf("建立一空棧n");initstack(st);printf("棧空:%dn",empty(st);printf("依
37、次插入 a,b,c,d 元素n");push(st,'a');push(st,'b');push(st,'c');push(st,'d');display(st);printf("退一次棧n");pop(st);printf("棧頂元素:%cn",gettop(st);printf("退一次棧n");pop(st);display(st);2鏈式棧的基本操作/*鏈式棧的基本操作*/#include <malloc.h>typedef char Ele
38、mType;typedef struct linknodeElemType data;struct linknode *next; linkstack;void initstack(linkstack *S)*S=NULL;void push(linkstack *S,ElemType x)linkstack *q;q=(linkstack *)malloc(sizeof(linkstack);q->data=x;q->next=*S;*S=q;void pop(linkstack *S)linkstack *t;if (*S=NULL) printf("棧下溢出!n&q
39、uot;);elset=*S;*S=t->next;free(t);ElemType gettop(linkstack *S)if (*S=NULL) return(-1);else return(*S)->data);int empty(linkstack *S)if (*S=NULL) return(1);else return(0);void display(linkstack *S)linkstack *q;printf("棧中元素:");q=*S;while (q!=NULL)printf("%c ",q->data);q=q
40、->next;printf("n");main()linkstack *stack;printf("建立一空棧n");initstack(&stack);printf("棧空:%dn",empty(&stack);printf("依次插入 a,b,c,d 元素n");push(&stack,'a');push(&stack,'b');push(&stack,'c');push(&stack,'d')
41、;display(&stack);printf("退一次棧n");pop(&stack);printf("棧頂元素:%cn",gettop(&stack);printf("退一次棧n");pop(&stack);display(&stack);3棧的應用/*棧的應用:求表達式值*/#include <stdio.h>#define MAX 100char expMAX; /*存儲轉(zhuǎn)換成的后綴表達式*/void trans() /*將算術表達式轉(zhuǎn)換成后綴表達式*/char strMAX
42、; /*存儲原算術表達式*/char stackMAX; /*作為棧使用*/char ch;int sum,i,j,t,top=0;/*t 作為 exp 的下標,top 作為 stack 的下標,i 作為 str 的下標*/printf("*n");printf("* 輸入一個求值的表達式,以#結(jié)束。只能包含+,-,*,/運算符和正整數(shù)*n");printf("*n");printf("算術表達式:");i=0; /*獲取用戶輸入的表達式*/doi+;scanf("%c",&stri);
43、 while (stri!='#' && i!=MAX);sum=i; /*記錄輸入表達式總的字符個數(shù)*/t=1;i=1;ch=stri;i+;while (ch!='#')switch(ch)case '(': /*判定為左括號*/top+;stacktop=ch;break;case ')': /*判定為右括號*/while (stacktop!='(')expt=stacktop;top-;t+;top-;break;case '+': /*判定為加減號*/case '
44、-':while (top!=0 && stacktop!='(')expt=stacktop;top-;t+;top+;stacktop=ch;break;case '*': /*判定為'*'或'/'號*/case '/':while (stacktop='*' | stacktop='/')expt=stacktop;top-;t+;top+;stacktop=ch;break;case ' ':break;default:while (c
45、h>='0' && ch<='9') /*判定為數(shù)字*/expt=ch;t+;ch=stri;i+;i-;expt='#'t+;ch=stri;i+;while (top!=0)expt=stacktop;t+;top-;expt='#'printf("nt 原來表達式:");for (j=1;j<sum;j+) printf("%c",strj);printf("nt 后綴表達式:",exp);for (j=1;j<t;j+) p
46、rintf("%c",expj);void compvalue() /*計算后綴表達式的值*/float stackMAX,d; /*作為棧使用*/char ch;int t=1,top=0; /*t 作為 exp 的下標,top 作為 stack 的下標*/ch=expt;t+;while (ch!='#')switch (ch)case '+':stacktop-1=stacktop-1+stacktop;top-;break;case '-':stacktop-1=stacktop-1-stacktop;top-;bre
47、ak;case '*':stacktop-1=stacktop-1*stacktop;top-;break;case '/':if (stacktop!=0)stacktop-1=stacktop-1/stacktop;elseprintf("nt 除零錯誤!n");exit(0);/*異常退出*/top-;break;default:d=0;while (ch>='0' && ch<='9') /*判定為數(shù)字字符*/d=10*d+ch-'0' /*將數(shù)字字符轉(zhuǎn)換成對
48、應的數(shù)值*/ch=expt;t+;top+;stacktop=d;ch=expt;t+;printf("nt 計算結(jié)果:%gn",stacktop);main()trans();compvalue();4 順序隊的基本操作/*順序隊的基本操作*/#include <stdio.h>#define MaxSize 100typedef char ElemType;typedef structElemType queueMaxSize;int front,rear; queuetype;void initqueue(queuetype *Q)Q->front=
49、Q->rear=-1;void enter(queuetype *Q,ElemType x)if (Q->rear=MaxSize) printf("隊列上溢出!n");elseQ->rear+; /*隊尾指針后移*/Q->queueQ->rear=x; /*新元素賦給隊尾單元*/void delete(queuetype *Q)if (Q->front=Q->rear)printf("隊列為空!n");elseQ->front+;ElemType gethead(queuetype *Q)if (Q-&g
50、t;front=Q->rear)printf("隊列為空!n");elsereturn(Q->queueQ->front+1);int empty(queuetype *Q)if (Q->front=Q->rear) return(1); /*為空,則返回 true*/else return(0); /*不為空,則返回 flase*/void display(queuetype *Q)int i;printf("隊列元素:");for (i=Q->front+1;i<=Q->rear;i+)printf(&
51、quot;%c ",Q->queuei);printf("n");main()queuetype *qu;printf("初始化隊列 qun");initqueue(qu);printf("隊列空:%dn",empty(qu);printf("依次入隊 a,b,c,d 元素n");enter(qu,'a');enter(qu,'b');enter(qu,'c');enter(qu,'d');display(qu);printf(&quo
52、t;出隊一次n");delete(qu);printf("隊首元素:%cn",gethead(qu);printf("出隊一次n");delete(qu);display(qu);5循環(huán)隊的基本操作/*循環(huán)隊的基本操作*/#include <stdio.h>#define MaxSize 4typedef char ElemType;typedef structElemType queueMaxSize;int front,rear; queuetype;void initqueue(queuetype *Q)Q->front
53、=Q->rear=-1;void enter(queuetype *Q,ElemType x)if (Q->front=-1 && (Q->rear+1)=MaxSize)printf("隊列上溢出!n"); /*front 處于初始狀態(tài),元素個數(shù)大于 MaxSize 時上溢出*/else if (Q->rear+1) % MaxSize = Q->front)printf("隊列上溢出!n");elseQ->rear=(Q->rear+1) % MaxSize; /*隊尾指針后移*/Q->
54、queueQ->rear=x; /*新元素賦給隊尾單元*/void delete(queuetype *Q)if (Q->front=Q->rear)printf("隊列為空!n");elseQ->front=(Q->front+1) % MaxSize;ElemType gethead(queuetype *Q)if (Q->front=Q->rear)printf("隊列為空!n");elsereturn(Q->queue(Q->front+1) % MaxSize);int empty(queu
55、etype *Q)if (Q->front=Q->rear) return(1); /*為空,則返回 true*/else return(0); /*不為空,則返回 flase*/void display(queuetype *Q)int i=Q->front+1;if (!empty(Q)printf("隊列元素:");while (i % MaxSize)!=Q->rear)printf("%c ",Q->queuei+ % MaxSize);if (Q->rear!=-1) /*不為初始狀態(tài)時顯示 rear 所指
56、元素*/printf("%c",Q->queuei % MaxSize);printf("n");else printf("隊列為空,不能顯示n");main()queuetype *qu;printf("初始化隊列 qun");initqueue(qu);printf("隊列空:%dn",empty(qu);printf("依次入隊 a,b,c,d,e 元素n");enter(qu,'a');enter(qu,'b');enter(qu
57、,'c');enter(qu,'d');enter(qu,'e');display(qu);printf("出隊三次n");delete(qu);delete(qu);delete(qu);display(qu);printf("依次入隊 x,y 元素n");enter(qu,'x');enter(qu,'y');display(qu);printf("出隊三次n");delete(qu);delete(qu);delete(qu);display(qu)
58、;printf("出隊一次n");delete(qu);6鏈式隊的基本操作/*鏈式隊的基本操作*/#include <stdio.h>typedef char ElemType;struct qnodeElemType data;struct qnode *next;typedef struct queuestruct qnode *front;struct qnode *rear; linkqueue;void initqueue(linkqueue *Q)Q=(struct queue *)malloc(sizeof(struct queue);Q->
59、front=Q->rear=NULL;void enter(linkqueue *Q,ElemType x)linkqueue *s;s=(struct queue *)malloc(sizeof(struct queue);s->data=x;s->next=NULL;if (Q->rear=NULL) /*若鏈隊為空,則新結(jié)點是隊首結(jié)點又是隊尾結(jié)點*/Q->front=Q->rear=s;elseQ->rear->next=s; /*將 s 結(jié)點鏈到隊尾,rear 指向它*/Q->rear=s;void delete(linkqueue *Q)linkqueue *t;if (Q->front=NULL)printf("隊列為空!n");else if (Q->front=Q->rear) /*只有一個結(jié)點時*/t=Q->front;Q->front=Q->rear=NULL;elset=Q->front;Q->front=Q->front->next;fr
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 后勤聘用合同范本
- 發(fā)行書分銷合同范本
- 雙方種植土地合同范本
- 供面合同范例
- 委托擔保保證合同范本
- 公司業(yè)務合同范本
- 臺球店員工合同范本
- 保潔修理員合同范例
- 農(nóng)村場地出售合同范本
- 合同范本樣板格式
- 【音樂】繽紛舞曲-青年友誼圓舞曲課件 2023-2024學年人音版初中音樂七年級上冊
- DB-T29-260-2019天津市建筑物移動通信基礎設施建設標準
- 水利工程施工方案(完整版)
- DB11-T 1200-2023 超長大體積混凝土結(jié)構跳倉法技術規(guī)程
- 2024年內(nèi)蒙古化工職業(yè)學院高職單招(英語/數(shù)學/語文)筆試歷年參考題庫含答案解析
- 城市智慧交通管理系統(tǒng)
- 青少年人工智能技術水平測試一級04
- 心肌病中醫(yī)護理查房課件
- 前列腺炎的護理課件
- 外墻防水膠驗報告模板
- 國外藥典介紹
評論
0/150
提交評論