国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C > 正文

C語(yǔ)言版實(shí)現(xiàn)鏈隊(duì)列

2020-01-26 13:40:06
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例為大家分享了C語(yǔ)言實(shí)現(xiàn)鏈隊(duì)列的具體代碼,供大家參考,具體內(nèi)容如下

源文件部分:  指針沒(méi)學(xué)好的同學(xué)很難看懂^_^,有點(diǎn)亂,希望對(duì)大家有點(diǎn)幫助。

#include<stdio.h>#include<malloc.h>#include<stdlib.h>#include<string.h>typedef int Elemtype;#include"LQueue.h"int main(){ Deque head; instruction(head); return 0;}頭文件部分:typedef struct Queue{ Elemtype data; struct Queue *next;}LQnode,*LQueue; typedef struct{ LQnode *front; LQnode *rear;}Deque; void Init_queue(Deque *head)  //初始化+清空操作==其實(shí)這里的清空是指將頭節(jié)點(diǎn)后的節(jié)點(diǎn)給丟棄掉 { LQnode *p=NULL; p=(LQueue)malloc(sizeof(LQnode)); head->front=p; head->rear=p;  p->next=NULL;} int Empty_queue(Deque *head)      //判空{(diào) if(head->front->next==head->rear->next) return 1; return 0;} int Lenght_queue(Deque arrow){ LQnode *p=NULL; int len=0; p=arrow.front->next; while(p) { len++; p=p->next; } return len;} void Enqueue(Deque *arrow,Elemtype e)    //入隊(duì)操作{ LQueue p=NULL; p=(LQueue)malloc(sizeof(LQnode)); if(!p) { printf("已無(wú)更多的內(nèi)存單元得到分配!/n"); return ; } p->data=e; p->next=NULL;         //插入時(shí),隊(duì)首指針是不需要?jiǎng)拥? arrow->rear->next=p; arrow->rear=p;  return ;} void Dequeue(Deque *arrow,Elemtype *e)    //出隊(duì)操作{ LQnode *p=NULL; if(Empty_queue(arrow)) { printf("當(dāng)前鏈隊(duì)列為空,無(wú)法完成出隊(duì)操作!!!/n"); return ; } p=arrow->front->next; (*e)=p->data; arrow->front->next=p->next; printf("元素%d已退出隊(duì)列!!!/n",*e); if(Lenght_queue(*arrow)==0) return ;            //當(dāng)最后一個(gè)元素出列以后,arrow->rear不知道指向了哪里    free(p); return ;} int Queue_top(Deque *arrow)  //返回隊(duì)首元素 { if(Empty_queue(arrow)) { printf("當(dāng)前鏈隊(duì)列為空,隊(duì)首元素不存在!!!/n"); return 0; } printf("當(dāng)前隊(duì)首元素是:%d/n",arrow->front->next->data);} void Destroy_queue(Deque *arrow)  //鏈隊(duì)列的銷毀{ LQnode *p=NULL; if(Empty_queue(arrow)) { printf("當(dāng)前鏈隊(duì)列為空,無(wú)須完成銷毀操作!!!/n"); return ; } while(arrow->front->next) { p=arrow->front->next; arrow->front->next=p->next; if(Lenght_queue(*arrow)==0)  break;    free(p); } printf("銷毀成功!/n"); return ;} void Print_queue(Deque arrow){ LQnode *p=NULL; p=arrow.front->next; while(p) { printf("%d ",p->data); p=p->next; } printf("/n");} void Modify_queue(Deque *arrow,Elemtype index,Elemtype e)  //修改函數(shù){ int i=0; LQnode *p=NULL; p=arrow->front->next; while(i<index-1) { p=p->next; } p->data=e; printf("已完成修改操作!/n");} int Insearch_queue(Deque arrow,Elemtype e)      //查找函數(shù){ LQnode *p=NULL; int i=1; if(Empty_queue(&arrow)) { printf("當(dāng)前鏈隊(duì)列為空,沒(méi)有元素可查找!!!/n"); return 0; } p=arrow.front->next; while(p!=NULL) { if(e==p->data) {  return i;  break; } i++; p=p->next; } if(p==NULL) printf("查找失敗,隊(duì)列內(nèi)無(wú)該元素存在!/n"); return 0;} void instruction(Deque head){ int n,m,t,a,b,len1,index; printf("/t/t1、隊(duì)列初始化 /n"); printf("/t/t2、新增隊(duì)列元素/n"); printf("/t/t3、返回隊(duì)首元素/n"); printf("/t/t4、元素出隊(duì)列 /n"); printf("/t/t5、查找隊(duì)列元素/n"); printf("/t/t6、修改隊(duì)列元素/n"); printf("/t/t7、銷毀隊(duì)列  /n"); printf("/t/t8、隊(duì)列的長(zhǎng)度 /n"); printf("/t/t9、打印隊(duì)列元素/n"); printf("/t/t10、退出程序  /n"); printf("請(qǐng)輸入你所需要完成的指令:/n"); do{ scanf("%d",&n); if(n<1||n>10)  printf("對(duì)不起,你輸入的指令編號(hào)是無(wú)效的,請(qǐng)重新輸入!!!/n"); }while(n<1||n>10); switch(n) { case 1:  Init_queue(&head);  printf("已完成鏈隊(duì)列初始化,請(qǐng)輸入你要添加的元素個(gè)數(shù)!/n");  scanf("%d",&n);  while(n--)  {  int x;  scanf("%d",&x);  Enqueue(&head,x);  }  printf("完成建隊(duì)操作!/n");  break; case 2:  printf("請(qǐng)輸入你要添加的元素個(gè)數(shù)!/n");  scanf("%d",&n);  while(n--)  {  int x;  scanf("%d",&x);  Enqueue(&head,x);  }  printf("增添成功!/n");  break; case 3:  Queue_top(&head);  break; case 4:  Dequeue(&head,&t);  break; case 5:  printf("請(qǐng)輸入你所要查找的元素:/n");  scanf("%d",&m);  index=Insearch_queue(head,m);  if(index)  printf("你所要查找的元素位于隊(duì)列的第%d個(gè)位置上!!!/n",index);  break; case 6:  printf("請(qǐng)輸入你更改的元素隊(duì)列位置:/n");  do{  scanf("%d",&a);  if(a<1||a>Lenght_queue(head))   printf("對(duì)不起,你所輸入的元素位置不在區(qū)域內(nèi),請(qǐng)重新輸入!!!/n");  }while(a<1||a>Lenght_queue(head));  printf("請(qǐng)輸入修改后的值:/n");  scanf("%d",&b);  Modify_queue(&head,a,b);  break; case 7:  Destroy_queue(&head);  break; case 8:  len1=Lenght_queue(head);  printf("當(dāng)前鏈隊(duì)列的長(zhǎng)度為:%d/n",len1);  break; case 9:  Print_queue(head);  break; case 10:  return; default:  instruction(head);  break; } instruction(head);}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 定安县| 凤台县| 承德县| 绩溪县| 巴东县| 佳木斯市| 廉江市| 吐鲁番市| 象山县| 茂名市| 洛隆县| 安龙县| 正镶白旗| 通榆县| 曲阜市| 吕梁市| 临泉县| 汉沽区| 弥渡县| 中宁县| 措勤县| 嫩江县| 青阳县| 富锦市| 墨脱县| 兰溪市| 梁山县| 汕尾市| 通辽市| 和龙市| 龙游县| 南乐县| 公主岭市| 万年县| 锡林郭勒盟| 什邡市| 阿克陶县| 奈曼旗| 白城市| 陆河县| 株洲县|