您的位置:首页 > 新闻 > 会展 > 微信头像logo在线制作_青岛app开发公司前十名_优化公司_搜索引擎优化要考虑哪些方面

微信头像logo在线制作_青岛app开发公司前十名_优化公司_搜索引擎优化要考虑哪些方面

2024/10/6 6:30:55 来源:https://blog.csdn.net/2301_79847249/article/details/142620311  浏览:    关键词:微信头像logo在线制作_青岛app开发公司前十名_优化公司_搜索引擎优化要考虑哪些方面
微信头像logo在线制作_青岛app开发公司前十名_优化公司_搜索引擎优化要考虑哪些方面

假设以带头结点的循环链表表示队列,并且只设一个指针指向队尾元素结点,试编写相应的初始化队列、判空队、入队和出队算法。(队中元素均为整数)

程序功能为:初始化一个空队列,然后接收命令并完成相应操作,命令如下:

ENQUEUE x 将整数x入队。若操作成功则无输出,若操作失败则输出FULL!。

DELQUEUE 出队一个元素。若操作成功则输出该元素,若失败则输EMPTY QUEUE!。

ISEMPTY 判断队列是否为空。若为空则输出 EMPTY,若非空则输出NOT EMPTY。

END 依次输出队列中所有元素,释放结点空间,并结束程序。

函数接口定义:

CirLinkQueue InitQueue();  //初始化队列,返回值为队列的尾指针。
int IsEmptyQueue(CirLinkQueue Q); //队列判空,若为空,则返回1;非空,返回0。
int EnQueue(CirLinkQueue *Q, DataType x);  //  元素x入队,若操作成功,则返回1;操作失败,则返回0。
int DelQueue(CirLinkQueue *Q, DataType *x);  //  出队一个元素,若操作成功,则返回1;操作失败,则返回0。

说明:队列使用仅带尾指针的循环链表表示,数据类型定义如下:

typedef int DataType;
typedef struct node
{      DataType  data;struct node *next; 
}LNode,*CirLinkQueue;

裁判测试程序样例:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>typedef int DataType;
typedef struct node
{      DataType  data;struct node *next; 
}LNode,*CirLinkQueue;//队列的基本操作函数定义
CirLinkQueue InitQueue();  //初始化队列,返回值为队列的尾指针。
int IsEmptyQueue(CirLinkQueue Q); //队列判空,若为空,则返回1;非空,返回0。
int EnQueue(CirLinkQueue *Q, DataType x);  //  元素x入队,若操作成功,则返回1;操作失败,则返回0。
int DelQueue(CirLinkQueue *Q, DataType *x);  //  出队一个元素,若操作成功,则返回1;操作失败,则返回0。
void DestroyQueue(CirLinkQueue Q);int main(void)
{char cmd[20];CirLinkQueue pQueue = InitQueue();DataType x;scanf("%s", cmd);while (strcmp(cmd, "END") != 0){if (strcmp(cmd, "ENQUEUE") == 0){scanf("%d", &x);if (EnQueue(&pQueue, x) == 0)printf("FULL QUEUE!\n");}else if (strcmp(cmd, "DELQUEUE") == 0){if (DelQueue(&pQueue,&x) == 0)printf("EMPTY QUEUE!\n");elseprintf("%d\n",x);}else if (strcmp(cmd, "ISEMPTY") == 0){if (IsEmptyQueue(pQueue) == 0)printf("NOT EMPTY\n");elseprintf("EMPTY\n");}scanf("%s", cmd);}DestroyQueue(pQueue);return 0;
}void DestroyQueue(CirLinkQueue Q)
{LNode *p,*t;p=Q;Q=Q->next;p->next=NULL;p=Q->next;while(p){printf("%d ",p->data);t=p->next;free(p);p=t;}free(Q);
}/* 请在这里填写答案 */

输入样例:

在这里给出一组输入。例如:

ISEMPTY
ENQUEUE 1
ENQUEUE 2
DELQUEUE
DELQUEUE
DELQUEUE
ENQUEUE 3
ISEMPTY
ENQUEUE 4
ENQUEUE 5
DELQUEUE
END

输出样例:

在这里给出相应的输出。例如:

EMPTY
1
2
EMPTY QUEUE!
NOT EMPTY
3
4 5 
以下是实现带尾指针的循环链表队列操作的代码://队列的基本操作函数定义
CirLinkQueue InitQueue() {CirLinkQueue Q = (CirLinkQueue)malloc(sizeof(LNode));if (!Q) {printf("Memory allocation failed!\n");return NULL;}Q->next = Q;return Q;
}int IsEmptyQueue(CirLinkQueue Q) {return Q->next == Q;
}int EnQueue(CirLinkQueue *Q, DataType x) {CirLinkQueue newNode = (CirLinkQueue)malloc(sizeof(LNode));if (!newNode) {return 0;}newNode->data = x;newNode->next = (*Q)->next;(*Q)->next = newNode;*Q = newNode;return 1;
}int DelQueue(CirLinkQueue *Q, DataType *x) {if (IsEmptyQueue(*Q)) {return 0;}CirLinkQueue temp = (*Q)->next->next;*x = temp->data;if (temp == *Q) {*Q = (*Q)->next;(*Q)->next = *Q;} else {(*Q)->next->next = temp->next;}free(temp);return 1;
}

版权声明:

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

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