当前位置: 首页> 文旅> 美景 > 假设以带头结点的循环链表表示队列,并且只设一个指针指向队尾元素结点(注意不设头指针),试编写相应的置空队列、判断队列是否为空、入队和出队等算法。

假设以带头结点的循环链表表示队列,并且只设一个指针指向队尾元素结点(注意不设头指针),试编写相应的置空队列、判断队列是否为空、入队和出队等算法。

时间:2025/7/9 4:09:05来源:https://blog.csdn.net/2305_78057683/article/details/141643246 浏览次数:0次
typedef int Datatype;
typedef struct queue
{Datatype data;struct queue* next;
}queue;
//队列
typedef struct
{queue* rear;
}LinkQueue;
//初始化
void queueinit(LinkQueue* ps)
{ps->rear = (queue*)malloc(sizeof(queue));if (ps->rear == NULL){perror("error");exit(1);}ps->rear->next = ps->rear;//初始化循环链表
}
//判空
int Empty(LinkQueue* ps)
{return ps->rear == ps->rear->next;//头尾相等,为空
}
//入队
int push(LinkQueue* ps, int e)
{queue* newnode = (queue*)malloc(sizeof(queue));if (newnode == NULL){perror("error:");exit(1);}newnode->data = e;newnode->next = ps->rear->next;//新节点指向原队首ps->rear->next = newnode;if (ps->rear == ps->rear->next){ps->rear = newnode;}
}
int pop(LinkQueue* ps)
{if ((Empty(&ps))){printf("栈为空,无输出内容\n");return -1;}//出队是头出,queue* temp = ps->rear->next;//临时节点指向队首int ret = temp->data;ps->rear->next = temp->next;//尾节点直接指向原队首的下一个节点if (ps->rear->next == ps->rear){ps->rear->next = ps->rear;}free(temp);return ret;
}int main()
{LinkQueue ps;queueinit(&ps);push(&ps, 1);push(&ps, 2);printf("出队情况\n");printf("%d\n", pop(&ps));printf("%d\n", pop(&ps));int ret = Empty(&ps);if (ret == 1){printf("队列元素已全出队\n");}return 0;
}

关键字:假设以带头结点的循环链表表示队列,并且只设一个指针指向队尾元素结点(注意不设头指针),试编写相应的置空队列、判断队列是否为空、入队和出队等算法。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: