【第1关:基于循环链表的队列的基本操作】【编程题实训-队列】【头歌】【bjfu246】

任务描述

本关任务:用带头结点的循环链表表示队列,并且只设一个指针指向队尾元素结点(不设头指针)。实现该队列的入队出队以及判断队列是否为空操作。

编程要求

输入
多组数据,每组数据有两行。第一行为两个整数n和m,n表示入队序列A的长度(n个数依次连续入队,中间没有出队的情况),m表示出队序列B的元素数量(m个数依次连续出队,中间没有入队的情况)。第二行为序列A(空格分隔的n个整数)。当n和m都等于0时,输入结束。

输出
对应每组数据输出一行。每行包括m+1个整数,前m个数代表出队序列B的各个整数,最后一个整数表示队列是否为空,队列为空输出0,不为空输出1。整数之间用空格分隔。

测试说明
平台会对你编写的代码进行测试:

测试输入:
5 3
1 3 5 3 6
4 4
-1 2 3 4
0 0

预期输出:
1 3 5 1
-1 2 3 4 0

C++代码:

head.h
详细见注释

#include<iostream>
using namespace std;
typedef struct QNode
{//队列的链式存储结构
    int data;
    struct QNode* next;
}QNode, * QueuePtr;
typedef struct
{
    QueuePtr rear;    //只设一个队尾指针
}LinkQueue;
int EmptyQueue(LinkQueue Q)
{//判断队列是否为空,空返回1,否则返回0
//队列只有一个头结点,即当头结点的指针域指向自己时,队列为空
  return(Q.rear->next->next==Q.rear->next)?1:0;
}
void EnQueue(LinkQueue& Q, int e)
{//入队,插入元素e为Q的新的队尾元素
       QNode *p=new QNode;
        p->data=e;
        p->next=Q.rear->next;
        Q.rear->next=p;
        Q.rear=p;
}
void DeQueue(LinkQueue& Q)
{//出队,输出Q的队头元素值,后将其删除
   QNode *p=Q.rear->next->next;
        printf("%d ",p->data);
        if(p==Q.rear)
        {
            Q.rear=Q.rear->next;
            Q.rear->next=p->next;
        }
        else Q.rear->next->next=p->next;
        delete p;
}

主函数文件不可编辑:

#include "head.h"
int main()
{
	int n,m;
	while(cin>>n>>m)
	{
		if(n==0&&m==0) break;
		LinkQueue Q;        //初始化一个带头结点的循环链表
		Q.rear=new QNode;
        Q.rear->next=Q.rear;
		while(n--)
		{
			int e;cin>>e;
			EnQueue(Q,e);
		}
		while(m--)
			DeQueue(Q);
		if(EmptyQueue(Q))
			cout<<"0"<<endl;
		else
			cout<<"1"<<endl;
	}
	return 0;
}

  • 8
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
单链表的插入操作可以分为两种情况: 1. 在链表头部插入新节点 2. 在链表中间或尾部插入新节点 以下是两种情况的代码实现: 1. 在链表头部插入新节点 ```c #include <stdio.h> #include <stdlib.h> struct ListNode { int val; struct ListNode *next; }; struct ListNode* insertNodeAtBeginning(struct ListNode* head, int val) { struct ListNode* new_node = (struct ListNode*)malloc(sizeof(struct ListNode)); new_node->val = val; new_node->next = head; head = new_node; return head; } int main() { struct ListNode* head = NULL; head = insertNodeAtBeginning(head, 1); head = insertNodeAtBeginning(head, 2); head = insertNodeAtBeginning(head, 3); printf("The linked list is: "); while(head != NULL) { printf("%d ", head->val); head = head->next; } return 0; } ``` 2. 在链表中间或尾部插入新节点 ```c #include <stdio.h> #include <stdlib.h> struct ListNode { int val; struct ListNode *next; }; struct ListNode* insertNodeAtPosition(struct ListNode* head, int val, int position) { struct ListNode* new_node = (struct ListNode*)malloc(sizeof(struct ListNode)); new_node->val = val; new_node->next = NULL; if (position == 1) { new_node->next = head; head = new_node; return head; } struct ListNode* temp = head; for (int i = 1; i < position-1; i++) { temp = temp->next; } new_node->next = temp->next; temp->next = new_node; return head; } int main() { struct ListNode* head = NULL; head = insertNodeAtPosition(head, 1, 1); head = insertNodeAtPosition(head, 2, 2); head = insertNodeAtPosition(head, 3, 3); head = insertNodeAtPosition(head, 4, 2); printf("The linked list is: "); while(head != NULL) { printf("%d ", head->val); head = head->next; } return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汤米尼克

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值