(链式队列实现)PTA:7-1 有趣的队列 (20 分)

7-1 有趣的队列 (20 分)
本题重新定义队列出队的操作:队首出队的数字重新在队尾入队。

例:队列中有1 2 3三个数字,现要求队首出队,则1从队首出队,同时1从队尾入队,队列变成2 3 1。

入队的顺序为1,2,3,4…n,同时给一个二进制字符串,1代表出队操作,0代表入队操作。

输入格式:
在第一行有两个数字n,m(n<=100,n<m),其中n为入队的数字个数,m代表操作数

接下来m行,每行一个数字,1或者0,代表不同的操作

输出格式:
输出操作后队列的每个数字,数字间以空格分隔,最后一个数字后没有空格

输入样例:
5 8
0
0
1
0
1
0
1
0
输出样例:
3 2 4 1 5

#include<stdbool.h>
#include<stdio.h>
#include<assert.h>
#include<malloc.h>
typedef int Data;
typedef struct _Node
{
	Data data;
	struct _Node* next;
}Node;
typedef struct _LinkQueue
{
	int size;
	Node* front;
	Node* tail;
}LinkQueue;
//创建队列
LinkQueue* createLinkQueue();
//入队
void push(LinkQueue* que, Data val);
//出队
void pop(LinkQueue* que);
//获取队头元素
Data front(LinkQueue* que);
//判空
bool empty(LinkQueue* que);

LinkQueue* createLinkQueue()
{
    LinkQueue* que = calloc(1, sizeof(LinkQueue));
    assert(que != NULL);
    que->front = calloc(1, sizeof(Node));
    assert(que->front != NULL);
    que->tail = que->front;
    que->size = 0;
    return que;
}
static Node* createNode(Data val)
{
    Node* newNode = calloc(1, sizeof(Node));
    if (newNode == NULL)
    {
        return NULL;
    }
    newNode->data = val;
    return newNode;
}
void push(LinkQueue* que, Data val)
{
    Node* newNode = createNode(val);
    if (newNode == NULL)
    {
        printf("Nueerrier");
    }
    que->tail->next = newNode;
    que->tail = newNode;
    que->size++;
}
void pop(LinkQueue* que)
{
    if (empty(que))
    {
        return;

    }
    Node* delNode = que->front->next;
    que->front->next = delNode->next;
    free(delNode);
    que->size--;

}

Data front(LinkQueue* que)
{
    assert(!empty(que));
    return que->front->next->data;
}

bool empty(LinkQueue* que)
{
    return que->size == 0;
}
int main()
{
    int n, m, ch, re=1;
    LinkQueue* que = createLinkQueue();
    scanf("%d%d", &n,&m);

    for (int i = 0; i < m; i++)
    {
        scanf("%d", &ch);
        switch (ch)
        {
        case 0:
        {
            push(que, re++);
            break;
        }
        case 1:
        {      
            push(que, front(que));
            pop(que);
            break;
        }

        }

    }
    int s=1;
    while (!empty(que))
    {
        printf("%d", front(que));
        if(s<n)
        {
            printf(" ");
            s++;
        }
     
        pop(que);
    }
    printf("\n");
    return 0;
}
队列(Queue)是一种先进先出(First In First Out,FIFO)的数据结构,在C语言中可以使用数组或链表来实现。这里简单讲解一下基于数组的队列实现以及基本操作。 **数组队列实现:** ```c typedef struct Queue { int capacity; // 队列容量 int front, rear; // 首(front)和队尾(rear) int* elements; // 存储数据的数组 } Queue; // 初始化队列 void initQueue(Queue* queue, int capacity) { queue->capacity = capacity; queue->front = -1; queue->rear = -1; queue->elements = (int*)malloc(capacity * sizeof(int)); } // 入队(enqueue) void enqueue(Queue* queue, int data) { if (queue->rear == (queue->front + queue->capacity - 1) % queue->capacity) { // 判断满 printf("Queue is full.\n"); return; } queue->rear = (queue->rear + 1) % queue->capacity; // 避免数组越界 queue->elements[queue->rear] = data; } // 出队(dequeue) int dequeue(Queue* queue) { if (queue->front == queue->rear) { // 判断空 printf("Queue is empty.\n"); return -1; } int data = queue->elements[queue->front]; queue->front = (queue->front + 1) % queue->capacity; return data; } ``` **基本操作:** 1. `initQueue()`:初始化一个新的队列配内存空间。 2. `enqueue(data)`:将元素添加到队尾3. `dequeue()`:从头移除并返回第一个元素,如果队列为空则返回-1。 **相关问题--:** 1. 如何检查队列是否为空或已满? 2. 如果需要删除队列,应该如何操作3. C语言链式队列实现有何不同?
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值