博客
关于我
链队列——出入队列
阅读量:297 次
发布时间:2019-03-03

本文共 1077 字,大约阅读时间需要 3 分钟。

队列数据结构实现

队列是一种先进先出的数据结构,可以通过两端操作数据。在本次实现中,我们使用两个结构体分别存储队列的头和尾节点。

结构体定义如下:

```cstruct node { int data; node *next; };

struct Queue

{
node *head; // 表头指针
node *rear; // 尾部指针
};

Queue Q; // 队列对象

初始化队列时,默认将头节点和尾节点初始化为一个空节点:

```cQ.head = new node; Q.rear = new node; Q.head->next = NULL; Q.rear->next = NULL;

实现队列的增操作(get_link函数):

```cvoid get_link(int x, Queue *Q) { node *tail = Q.rear; // 获取当前尾部节点
while (x--)  {      node *q = new node;      scanf("%d", &q->data);      tail->next = q;      q->next = NULL;      tail = q;  }  Q.rear = tail;  // 更新尾部指针

实现队列的删操作(out_link函数):

```cvoid out_link(node *head) { node *q = head->next; while (q) { printf("%d\n", q->data); q = q->next; } head->next = q; // 将原头节点的下一个指针设为空节点

主函数实现:

```cint main() { int x; scanf("%d", &x);
node *head = new node;  Q.head = Q.rear = head;  // 初始化头和尾都指向同一个空节点  get_link(x, &Q);  // 读取并添加x个节点  out_link(Q.head);  // 输出队列中的数据  if (!Q.head->next)  {      printf("队列为空\n");  }  return 0;

}

整个实现通过动态分配节点实现了队列的基本操作,支持插入和删除数据。通过尾部指针的更新确保了队列的高效操作。

转载地址:http://oqsl.baihongyu.com/

你可能感兴趣的文章
org.springframework.beans.factory.BeanDefinitionStoreException
查看>>
org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata
查看>>
org.springframework.boot:spring boot maven plugin丢失---SpringCloud Alibaba_若依微服务框架改造_--工作笔记012
查看>>
SQL-CLR 类型映射 (LINQ to SQL)
查看>>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
查看>>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
查看>>
org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded
查看>>
org.tinygroup.serviceprocessor-服务处理器
查看>>
org/eclipse/jetty/server/Connector : Unsupported major.minor version 52.0
查看>>
org/hibernate/validator/internal/engine
查看>>
Orleans框架------基于Actor模型生成分布式Id
查看>>
SQL-36 创建一个actor_name表,将actor表中的所有first_name以及last_name导入改表。
查看>>
ORM sqlachemy学习
查看>>
Ormlite数据库
查看>>
orm总结
查看>>
ORM框架 和 面向对象编程
查看>>
OS X Yosemite中VMware Fusion实验环境的虚拟机文件位置备忘
查看>>
os.environ 没有设置环境变量
查看>>
os.path.join、dirname、splitext、split、makedirs、getcwd、listdir、sep等的用法
查看>>
os.removexattr 的 Python 文档——‘*‘(星号)参数是什么意思?
查看>>