zhuaizhai 2024-03-29 21:03 采纳率: 90.5%
浏览 5
已结题

C++实现链队列,按要求完成下列操作

实现链队列的基本操作函数,以实现判断队列是否为满,是否为空、求队列元素个数,进队和出队等功能。编写辆队列的基本操作函数,
void InitQueue(LinkQueue &Q)://构造一个空队列Q
void DestroyQueue(LinkQueue &Q): // 销毁队列Q,Q不再存在 void ClearQueue(LinkQueue &QK: //将Q清为空队列
int QueueEmpty(LinkQueue Q): //若队列Q为空队列,则返回TRUE;否则返回FALSE int QueueLength(LinkQueue Q); //返回Q的元素个数,即队列的长度
int GetHead(LinkQueue QQElemType &e); // 若队列不空,则用e返回Q的队头元素,并返回OK:否则返回ERROR int EnQueue(LinkQueue &Q.QElemType e): // 插入元素e为Q的新的队尾元素
int DeQueue(LinkQueue &Q.QElemType &e); //若队列不空,则删除Q的队头元素,用e返回其值,并返回OK:否则返回ERROR void QueueTraverse(LinkQueue Qvoid(*vi)(QElemType)); //从队头到队尾依次对队列

输入说明:
第一行输入初始待插入的队列元素个数(要求初始个数不少于2个)第一行输入初始待插入的队列元素值第二行输入第2次插入的队列元素输出说明:
第一行输出初始队列中的元素个数第二行输出目前队列中的元素第三行输出删除的队头元素
第四行输出第2次删除的队头元素
第五行输出插入第2次数据后的队列长度第六行输出插入第2次数据后的队列元素第七行输出当前的队头元素
输入样例1:
10 20 30 40 5060
输出样例1:队列长度为:5
现在队列中元素:10 2030 40 50删除的元素是10删除的元素是20队列长度为:4
现在队列中元素:30 40 50 60现在队头元素为:30

  • 写回答

2条回答 默认 最新

  • 码农阿豪@新空间 优质创作者: Java、后端开发技术领域 2024-03-29 21:04
    关注
    让阿豪来帮你解答,本回答参考chatgpt3.5编写提供,如果还有疑问可以评论或留言
    Case: Input:(输入的队列中有10、20、30、40、50这五个元素,2为待插入的元素) 5 10 20 30 40 50 2 Output:(由于在队首删除元素,所以先输出删除的队首元素为10,再执行一次后为20) queue length is:5 elements in current queue:10 20 30 40 50 the front element has been deleted, it was 10 the front element has been deleted, it was 20 queue length is:4 now there are elements in queue: 30 40 50 2 the front element is now: 30 Solution: 链式队列的定义: 链式队列和普通的队列的定义很像,只不过把数组替换成了链表。 这个问题要求实现链式队列的基本操作,如进队,出队,求队列长度,清队等。 定义链式队列的结构体 QueueNode,结构体中定义了数据域 Data,并定义了一个结构体节点指针类型的队列指针 Rear,头指针 Front: typedef int QElemType; typedef struct QueueNode {
    QElemType Data;
    struct QueueNode *Next;
    

    }QueueNode; typedef struct LinkQueue { QueueNode * Front; QueueNode * Rear; }LinkQueue; 首先是判断队列为空的操作: int QueueEmpty(LinkQueue Q) { return (Q.Front == Q.Rear); } 再是求队列长度: int QueueLength(LinkQueue Q) { QueueNode *p = Q.Front; int num = 0; while(p != Q.Rear) { num++; p = p-> Next; } return num; } 然后是进队: int EnQueue(LinkQueue &Q, QElemType e) { QueueNode *pn = new QueueNode; pn->Data = e; pn->Next = NULL; Q.Rear->Next = pn; Q.Rear = pn; return 1; } 最后是出队: int DeQueue(LinkQueue &Q, QElemType & e) { if(QueueEmpty(Q)) { e = -1; return 0; } QueueNode *pn = Q.Front->Next; e = pn->Data; Q.Front->Next = pn->Next; if(Q.Rear == pn) Q.Rear = Q.Front; delete pn; return 1; } 由于出队的操作需要用到上一个队列节点,所以我们在队列顶加了一个空节点,0号位置没有节点。 需要注意的是,当出队列时,如果刚刚好删除了队尾,那么就要将 Rear 指针向前移一位,这样做的目的是避免节点的仍然存在造成的错误。 回到操作函数上,构造空队列函数需要申请一个空的链表: void InitQueue(LinkQueue &Q) { QueueNode *pn = new QueueNode; pn->Next = NULL; Q.Front = Q.Rear = pn; } 清空操作函数需要不断的出队: void ClearQueue(LinkQueue &Q) { int tmp; while(QueueLength(Q)) DeQueue(Q,tmp); } 判断链式队列是否满的操作在这里并不需要,因为链式队列的长度是可以动态的不断增长的。 最后是输出队列中的元素,其中 Traverse 需要一个函数指针作为参数: void Traverse(LinkQueue Q,void(*vi)(QElemType)) { QueueNode *p = Q.Front->Next; while(p != NULL) { vi(p->Data); p = p->Next; } } void f(QElemType d) { cout<<d<<" "; } 在Main函数中,先构造一个新队列: LinkQueue Q; InitQueue(Q); 再插入元素: while(cin>>n) EnQueue(Q,n); 这里要循环读入,因为我们是用while循环读入和插入数字的。 接下来可以输出了: printf("queue length is:%d\n",QueueLength(Q)); printf("elements in current queue: "); 然后是最重要的出队操作,在主函数中调用 DeQueue,由于 DeQueue 返回值为 0 时,说明队列已空,所以要做出判断: int res = DeQueue(Q,tmp); if(res == 0) printf("no elements.\n"); else printf("\nthe front element has been deleted, it was %d\n", tmp); 现在队列中有4个元素,再做一次出队操作: res= DeQueue(Q,tmp); if(res == 0) printf("no elements.\n"); else printf("\nthe front element has been deleted, it was%d\n", tmp); 再插入一个2: EnQueue(Q,2); 队列的长度以及队列中元素的个数: printf("queue length is:%d\n",QueueLength(Q)); printf("now there are elements in queue: "); Traverse(Q,f); printf("\n"); 最后输出队列头: GetHead(Q,tmp); printf("now the front element is:%d\n",tmp); 代码:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月6日
  • 已采纳回答 3月29日
  • 创建了问题 3月29日