c语言实现链队列的基本功能

秒速五厘米 2022-05-17 02:26 271阅读 0赞

链队列,实际上是一个带有头指针和尾指针的单链表,头指针指向头节点(不存放数据),尾指针指向队尾节点,虽然用头指针可以确定一个单链表,但是插入操作是在队尾进行,如果没有尾指针,会变得复杂

初始化:

  1. void init(pQueue pq){
  2. pq->front=pq->rear=(pNode)malloc(sizeof(Node));
  3. }

注意:(1).为头指针和尾指针申请内存

入队:

  1. void enqueue(pQueue pq,int x){
  2. pNode pNew=(pNode)malloc(sizeof(Node));
  3. pNew->data=x;
  4. pNew->next=NULL;
  5. pq->rear->next=pNew;
  6. pq->rear=pNew;
  7. }

注意:(1).入队是在队尾进行,所以需要尾指针

出队:

  1. void dequeue(pQueue pq,int *e){
  2. pNode pTemp=(pNode)malloc(sizeof(Node));
  3. pTemp=pq->front->next;
  4. *e=pTemp->data;
  5. pq->front->next=pTemp->next;
  6. free(pTemp);
  7. }

注意:(1).出队是在队头进行

(2).记得free(pTemp),因为一开始申请了一块内存

(3).用e来存储数据,方便在main函数中处理,int *p是定义一个指针p,p是一个地址,所以p=&a(或者一开始就用int *p=&a),然后printf(“%d”,*p),是输出变量a的值,printf(”%d”,&p),是输出一个地址,因为指针p也是一个变量,也有自己的地址

求队列长度:

  1. int QueueLength(pQueue pq){
  2. int count=0;
  3. pNode p=pq->front->next;
  4. while(p!=NULL){
  5. count++;
  6. p=p->next;
  7. }
  8. printf("\n");
  9. return count;
  10. }

注意:(1).while循环要跳出,p要等于null,这就需要入队把pNew->next=NULL

遍历队列:

  1. void show_queue(pQueue pq){
  2. pNode p=pq->front->next;
  3. printf(" 当前元素从头到尾:");
  4. while(p!=NULL){
  5. printf("%d ",p->data);
  6. p=p->next;
  7. }
  8. }

main函数:

  1. void main(){
  2. int count;
  3. int x;
  4. int e;
  5. Queue q;
  6. init(&q);
  7. printf("_____入队10个元素_____");
  8. for(int i=0;i<10;i++){
  9. enqueue(&q,i);
  10. count=QueueLength(&q);
  11. printf("%d入队,当前队列元素个数:%d ",i,count);
  12. show_queue(&q);
  13. }
  14. printf("\n_____出队5个元素_____");
  15. for(int i=0;i<5;i++){
  16. dequeue(&q,&x);
  17. count=QueueLength(&q);
  18. printf("%d出队,当前队列元素个数:%d ",x,count);
  19. show_queue(&q);
  20. }
  21. GetHead(&q,&e);
  22. printf("\n当前队头为:%d\n",e);
  23. system("pause");
  24. }

注意:(1).如果定义的是结构体指针,比如pQueue pq(pq是地址),就应该申请一块内存,也可以用Queue q,然后&q取地址传入函数,这样就不用申请内存了

(2).入队出队可以用for循环提高效率,同时通过传入&x,把x的值传出到main函数,方便调用

(3).xx删除,当前还有xx个元素,从头到尾是xx,这样一目了然

完整代码如下:

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<malloc.h>
  4. typedef struct node{
  5. int data;
  6. node *next;
  7. }Node,*pNode;
  8. typedef struct{
  9. pNode front;
  10. pNode rear;
  11. }Queue,*pQueue;
  12. void init(pQueue pq){
  13. pq->front=pq->rear=(pNode)malloc(sizeof(Node));
  14. //pq->rear->next=NULL;
  15. }
  16. int QueueLength(pQueue pq){
  17. int count=0;
  18. pNode p=pq->front->next;
  19. while(p!=NULL){
  20. count++;
  21. p=p->next;
  22. }
  23. printf("\n");
  24. return count;
  25. }
  26. void enqueue(pQueue pq,int x){
  27. pNode pNew=(pNode)malloc(sizeof(Node));
  28. pNew->data=x;
  29. pNew->next=NULL;
  30. pq->rear->next=pNew;
  31. pq->rear=pNew;
  32. }
  33. void dequeue(pQueue pq,int *e){
  34. pNode pTemp=(pNode)malloc(sizeof(Node));
  35. pTemp=pq->front->next;
  36. *e=pTemp->data;
  37. pq->front->next=pTemp->next;
  38. free(pTemp);
  39. }
  40. void show_queue(pQueue pq){
  41. pNode p=pq->front->next;
  42. printf(" 当前元素从头到尾:");
  43. while(p!=NULL){
  44. printf("%d ",p->data);
  45. p=p->next;
  46. }
  47. }
  48. void GetHead(pQueue pq,int *e){
  49. *e=pq->front->next->data;
  50. }
  51. void main(){
  52. int count;
  53. int x;
  54. int e;
  55. Queue q;
  56. init(&q);
  57. printf("_____入队10个元素_____");
  58. for(int i=0;i<10;i++){
  59. enqueue(&q,i);
  60. count=QueueLength(&q);
  61. printf("%d入队,当前队列元素个数:%d ",i,count);
  62. show_queue(&q);
  63. }
  64. printf("\n_____出队5个元素_____");
  65. for(int i=0;i<5;i++){
  66. dequeue(&q,&x);
  67. count=QueueLength(&q);
  68. printf("%d出队,当前队列元素个数:%d ",x,count);
  69. show_queue(&q);
  70. }
  71. GetHead(&q,&e);
  72. printf("\n当前队头为:%d\n",e);
  73. system("pause");
  74. }

发表评论

表情:
评论列表 (有 0 条评论,271人围观)

还没有评论,来说两句吧...

相关阅读

    相关 c++实现基本操作

    队列是一种常见的数据结构,它的特点是先进先出,也就是说,先加入队列的元素先被取出,而后加入的元素则在其后面等待。 在C++中,队列可以通过STL库中的queue类来实现。下

    相关 c语言实现基本功能

    链队列,实际上是一个带有头指针和尾指针的单链表,头指针指向头节点(不存放数据),尾指针指向队尾节点,虽然用头指针可以确定一个单链表,但是插入操作是在队尾进行,如果没有尾指针,会