2-25练习题

6.假设循环队列用数组实现,其定义如下:

#define SEQLEN 32

int seqn [ SEQLEN ];

/*用于存放队列数据的数组*/

int head;/*数组下标索引,指向队列头部,若队列不空,指向队列头元素*/

int tail;/*数组下标索引,指向队列尾部,若队列不空,指向队列空元素*/

队列示例:

循环队列中保存了1、2,3三个数据的状态。

seqn

head=1

tail=4

(1)假如队列未满,现有变量 data 需要入队,请写出表达式;

seqn[tail] = data;

tail = (tail + 1) % SEQLEN;

(2)假如队列未空,现在需要从队列取一个元素并赋值给变量data,请写出表达式;

data = seqn[head];

head = (head + 1) % SEQLEN;

(3)请写出队列为空的判断条件:

head == tail

(4)请写出队列满的判断条件:

(head + 1) % SEQLEN == tail

(5)请写出清空队列的表达式:

head = 0;

tail = 0;

(6)请写出计算队列中元素个数的表达式:

(head <= tail) ? (tail - head) : (SEQLEN - head + tail)

(7)队列最多可以存放几个元素:

循环队列最多可以存放的元素个数为 SEQLEN - 1,因为需要浪费一个位置来区分队列为空和队列为满的状态。

所以最多可以存放31个元素。

6, 补下面队列代码

struct list_head {

struct list_head *next, *prev;

};

/**

* used to add new element into list.

* @new: specify the new element.

* @prev: specify the previous element.

* @next: specify the next element.

* 插入 到 prev 和 next 中间

*/

static inline void _list_add(struct list_head * new,

struct list_head * prev,

struct list_head *next)

}

new=(struct list_head*)malloc(sizeof(struct list_head));

if(NULL = new)

{

printf("创建新节点失败\n");

return;

}

new->next = NULL;

new->prev = NULL;

new->prev=new->prev->next;

new->prev->next = new;

/**

* used to maintain link status when delete an element.

*@prev: specify the previous element.

*@next: specify the next element.

* 删除 prev 和 next 中间那个

*/

static inline void __list_del(struct list_head *prev, struct list_head *next)

{

struct list_head* p;

p=prev->next;

prev->next = prev->next->next;

free(p);

p=NULL;

}

/**

* used to add element into the tall of list.

* @new: specify the new element.

* @head: specify the head element.

* 插入到队尾

* /

static inline void list_add_tail(struct list_head *new, struct list_head *head)

{

new=(struct list_head*)malloc(sizeof(struct list_head));

if(NULL = new)

{

printf("申请新节点失败\n");

return;

}

p=head;

while(p->next!=NULL)

{

p=p->next;

}

p->next = new->next;

p->next = new;

new->prev = p;

new->next=NULL;

}

/**

* used to remove element.

* @entry: specify the element which want to be deleted.

* 删掉

*/

static inline void list_del(struct list_head *entry)

{

struct list_head* p;

if(entry->next!=NULL)

{

entry->prev->next = entry->next;

entry->next->prev = entry->prev;

free(entry);

entry=NULL;

}

else

{

entry->prev->next = NULL;

free(entry);

entry=NULL;

}

}

/**

* used to check list is empty or not.

* @head: specify the head element.

* 判断是否为空

*/

static inline int list_empty(const struct list_head *head)

{

return head->next == head->prev;

}

相关推荐
codebetter4 小时前
Windows子系统WSL Ubuntu24.04 Docker pull镜像失败
windows·docker
kyle~5 小时前
Windows---动态链接库Dynamic Link Library(.dll)
运维·windows·操作系统·运维开发·开发部署
中游鱼7 小时前
如何序列化和反序列化动态 XmlElement ?
windows·microsoft·c#
我命由我1234512 小时前
Kotlin 数据容器 - List(List 概述、创建 List、List 核心特性、List 元素访问、List 遍历)
java·开发语言·jvm·windows·java-ee·kotlin·list
mCell14 小时前
从删库到跑路?这50个Linux命令能保你职业生涯
linux·windows·macos
dualven_in_csdn15 小时前
electron 使用记录
windows
zz96022616 小时前
Windows Server存储池,虚拟磁盘在系统启动后不自动连接需要手动连接
windows
吳所畏惧1 天前
NVM踩坑实录:配置了npm的阿里云cdn之后,下载nodejs老版本(如:12.18.4)时,报404异常,下载失败的问题解决
前端·windows·阿里云·npm·node.js·batch命令
leese2331 天前
FreeMarker模板引擎
windows
love530love1 天前
命令行创建 UV 环境及本地化实战演示—— 基于《Python 多版本与开发环境治理架构设计》的最佳实践
开发语言·人工智能·windows·python·conda·uv