C语言版实现链队列
本文实例为大家分享了C语言实现链队列的具体代码,供大家参考,具体内容如下 源文件部分: 指针没学好的同学很难看懂^_^,有点乱,希望对大家有点帮助。 #include #include #include #include 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) //初始化+清空操作==其实这里的清空是指将头节点后的节点给丢弃掉 { LQnode *p=NULL; p=(LQueue)malloc(sizeof(LQnode)); head->front=p; head->rear=p; p->next=NULL; } int Empty_queue(Deque *head) //判空 { 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) //入队操作 { LQueue p=NULL; p=(LQueue)malloc(sizeof(LQnode)); if(!p) { printf("已无更多的内存单元得到分配!n"); return ; } p->data=e; p->next=NULL; //插入时,队首指针是不需要动的 arrow->rear->next=p; arrow->rear=p; return ; } void Dequeue(Deque *arrow,Elemtype *e) //出队操作 { LQnode *p=NULL; if(Empty_queue(arrow)) { printf("当前链队列为空,无法完成出队操作!!!n"); return ; } p=arrow->front->next; (*e)=p->data; arrow->front->next=p->next; printf("元素%d已退出队列!!!n",*e); if(Lenght_queue(*arrow)==0) return ; //当最后一个元素出列以后,arrow->rear不知道指向了哪里 free(p); return ; } int Queue_top(Deque *arrow) //返回队首元素 { if(Empty_queue(arrow)) { printf("当前链队列为空,队首元素不存在!!!n"); return 0; } printf("当前队首元素是:%dn",arrow->front->next->data); } void Destroy_queue(Deque *arrow) //链队列的销毁 { LQnode *p=NULL; if(Empty_queue(arrow)) { printf("当前链队列为空,无须完成销毁操作!!!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) //修改函数 { int i=0; LQnode *p=NULL; p=arrow->front->next; while(i { p=p->next; } p->data=e; printf("已完成修改操作!n"); } int Insearch_queue(Deque arrow,Elemtype e) //查找函数 { LQnode *p=NULL; int i=1; if(Empty_queue(&arrow)) { printf("当前链队列为空,没有元素可查找!!!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("查找失败,队列内无该元素存在!n"); return 0; } void instruction(Deque head) { int n,m,t,a,b,len1,index; printf("tt1、队列初始化 n"); printf("tt2、新增队列元素n"); printf("tt3、返回队首元素n"); printf("tt4、元素出队列 n"); printf("tt5、查找队列元素n"); printf("tt6、修改队列元素n"); printf("tt7、销毁队列 n"); printf("tt8、队列的长度 n"); printf("tt9、打印队列元素n"); printf("tt10、退出程序 n"); printf("请输入你所需要完成的指令:n"); do{ scanf("%d",&n); if(n<1||n>10) printf("对不起,你输入的指令编号是无效的,请重新输入!!!n"); }while(n<1||n>10); switch(n) { case 1: Init_queue(&head); printf("已完成链队列初始化,请输入你要添加的元素个数!n"); scanf("%d",&n); while(n--) { int x; scanf("%d",&x); Enqueue(&head,x); } printf("完成建队操作!n"); break; case 2: printf("请输入你要添加的元素个数!n"); scanf("%d",x); } printf("增添成功!n"); break; case 3: Queue_top(&head); break; case 4: Dequeue(&head,&t); break; case 5: printf("请输入你所要查找的元素:n"); scanf("%d",&m); index=Insearch_queue(head,m); if(index) printf("你所要查找的元素位于队列的第%d个位置上!!!n",index); break; case 6: printf("请输入你更改的元素队列位置:n"); do{ scanf("%d",&a); if(a<1||a>Lenght_queue(head)) printf("对不起,你所输入的元素位置不在区域内,请重新输入!!!n"); }while(a<1||a>Lenght_queue(head)); printf("请输入修改后的值:n"); scanf("%d",&b); Modify_queue(&head,b); break; case 7: Destroy_queue(&head); break; case 8: len1=Lenght_queue(head); printf("当前链队列的长度为:%dn",len1); break; case 9: Print_queue(head); break; case 10: return; default: instruction(head); break; } instruction(head); } 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |