如何用C语言编写链式队列代码?

2026-05-08 15:563阅读0评论SEO资源
  • 内容介绍
  • 相关推荐

本文共计572个文字,预计阅读时间需要3分钟。

如何用C语言编写链式队列代码?

记录一种C语言实现的链表序列代码,仅供参考,内容如下:

c#include #include

// 定义链表节点结构体typedef struct Node { int data; struct Node *next;} Node;

// 创建新节点Node* createNode(int data) { Node *newNode=(Node*)malloc(sizeof(Node)); if (newNode==NULL) { printf(内存分配失败\n); exit(1); } newNode->data=data; newNode->next=NULL; return newNode;}

// 向链表尾部添加节点void appendNode(Node **head, int data) { Node *newNode=createNode(data); if (*head==NULL) { *head=newNode; } else { Node *current=*head; while (current->next !=NULL) { current=current->next; } current->next=newNode; }}

// 打印链表void printList(Node *head) { Node *current=head; while (current !=NULL) { printf(%d , current->data); current=current->next; } printf(\n);}

如何用C语言编写链式队列代码?

// 释放链表内存void freeList(Node *head) { Node *current=head; while (current !=NULL) { Node *temp=current; current=current->next; free(temp); }}

int main() { Node *head=NULL;

// 添加节点 appendNode(&head, 1); appendNode(&head, 2); appendNode(&head, 3); appendNode(&head, 4);

// 打印链表 printList(head);

// 释放链表内存 freeList(head);

return 0;}

记录一下C语言实现的链队列代码,供大家参考,具体内容如下

#include<stdio.h> #include<stdlib.h> #include<stdbool.h> typedef int ElemType; //链队列的结点定义 typedef struct node{ ElemType val; struct node* next; }QueueNode; //链队列的定义,包含队头指针和队尾指针 typedef struct queue { QueueNode* front; QueueNode* rear; }LinkedQueue; //初始化队列 LinkedQueue* initQueue() { LinkedQueue* queue = (LinkedQueue*)malloc(sizeof(LinkedQueue)); queue->front = (QueueNode*)malloc(sizeof(QueueNode)); queue->front->next = NULL; queue->rear = queue->front; } //元素入队 void enQueue(LinkedQueue* queue, ElemType elem) { QueueNode* node = (QueueNode*)malloc(sizeof(QueueNode)); node->val = elem; node->next = NULL; queue->rear->next = node; queue->rear = node; } //队列是否为空 bool isQueueEmpty(LinkedQueue* queue) { return queue->front == queue->rear; } //元素出队 ElemType deQueue(LinkedQueue* queue) { if(!isQueueEmpty(queue)) { QueueNode* p = queue->front; queue->front = p->next; ElemType e = queue->front->val; free(p); return e; } return NULL; } int main() { LinkedQueue* queue = initQueue(); int i; for(i = 0; i < 20; i++) { enQueue(queue, i); } while(!isQueueEmpty(queue)) { printf("deQueue: %d\n", deQueue(queue)); } return 0; }

需要注意的是:

  • 初始化队列时,队头和队尾都指向同一个结点(头结点,不存储数据);
  • 判断队列是否为空,即判断队头指针和队尾指针是否相同;
  • 队头元素是当前front指针的next结点中的值

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

本文共计572个文字,预计阅读时间需要3分钟。

如何用C语言编写链式队列代码?

记录一种C语言实现的链表序列代码,仅供参考,内容如下:

c#include #include

// 定义链表节点结构体typedef struct Node { int data; struct Node *next;} Node;

// 创建新节点Node* createNode(int data) { Node *newNode=(Node*)malloc(sizeof(Node)); if (newNode==NULL) { printf(内存分配失败\n); exit(1); } newNode->data=data; newNode->next=NULL; return newNode;}

// 向链表尾部添加节点void appendNode(Node **head, int data) { Node *newNode=createNode(data); if (*head==NULL) { *head=newNode; } else { Node *current=*head; while (current->next !=NULL) { current=current->next; } current->next=newNode; }}

// 打印链表void printList(Node *head) { Node *current=head; while (current !=NULL) { printf(%d , current->data); current=current->next; } printf(\n);}

如何用C语言编写链式队列代码?

// 释放链表内存void freeList(Node *head) { Node *current=head; while (current !=NULL) { Node *temp=current; current=current->next; free(temp); }}

int main() { Node *head=NULL;

// 添加节点 appendNode(&head, 1); appendNode(&head, 2); appendNode(&head, 3); appendNode(&head, 4);

// 打印链表 printList(head);

// 释放链表内存 freeList(head);

return 0;}

记录一下C语言实现的链队列代码,供大家参考,具体内容如下

#include<stdio.h> #include<stdlib.h> #include<stdbool.h> typedef int ElemType; //链队列的结点定义 typedef struct node{ ElemType val; struct node* next; }QueueNode; //链队列的定义,包含队头指针和队尾指针 typedef struct queue { QueueNode* front; QueueNode* rear; }LinkedQueue; //初始化队列 LinkedQueue* initQueue() { LinkedQueue* queue = (LinkedQueue*)malloc(sizeof(LinkedQueue)); queue->front = (QueueNode*)malloc(sizeof(QueueNode)); queue->front->next = NULL; queue->rear = queue->front; } //元素入队 void enQueue(LinkedQueue* queue, ElemType elem) { QueueNode* node = (QueueNode*)malloc(sizeof(QueueNode)); node->val = elem; node->next = NULL; queue->rear->next = node; queue->rear = node; } //队列是否为空 bool isQueueEmpty(LinkedQueue* queue) { return queue->front == queue->rear; } //元素出队 ElemType deQueue(LinkedQueue* queue) { if(!isQueueEmpty(queue)) { QueueNode* p = queue->front; queue->front = p->next; ElemType e = queue->front->val; free(p); return e; } return NULL; } int main() { LinkedQueue* queue = initQueue(); int i; for(i = 0; i < 20; i++) { enQueue(queue, i); } while(!isQueueEmpty(queue)) { printf("deQueue: %d\n", deQueue(queue)); } return 0; }

需要注意的是:

  • 初始化队列时,队头和队尾都指向同一个结点(头结点,不存储数据);
  • 判断队列是否为空,即判断队头指针和队尾指针是否相同;
  • 队头元素是当前front指针的next结点中的值

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。