C语言中的循环队列与栈、队列之间的转换实现

引言

在数据结构的学习中,栈(Stack)和队列(Queue)是两个非常重要的概念。它们分别遵循着后进先出(LIFO)和先进先出(FIFO)的原则。在某些情况下,我们可能需要通过栈来模拟队列,或者通过队列来模拟栈的行为。本文将详细介绍这两种数据结构,并提供相应的C语言实现代码和图解。

一、栈(Stack)

栈是一种后进先出(LIFO)的数据结构,只允许在一端进行操作,这一端被称为栈顶(top)。栈的基本操作包括入栈(push)和出栈(pop)。

C语言实现栈

// 支持动态增长的栈
typedef int STDataType;
typedef struct Stack
{
	STDataType* _a;
	int _top;		// 栈顶
	int _capacity;  // 容量 
}Stack;

// 初始化栈 
void StackInit(Stack* ps)
{
	assert(ps);
	ps->_a = NULL;
	//top指向栈顶数据的下一个位置
	ps->_top = 0;
	//top指向栈顶数据
	//ps->_top = -1;
	ps->_capacity = 0;
}
// 入栈 
void StackPush(Stack* ps, STDataType data)
{
	assert(ps);
	if (ps->_top == ps->_capacity)
	{
		int newcapacity = ps->_capacity == 0 ? 4 : ps->_capacity * 2;
		STDataType* tmp = (STDataType*)realloc(ps->_a, newcapacity * (sizeof(STDataType)));
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		ps->_a = tmp;
		ps->_capacity = newcapacity;
	}
	ps->_a[ps->_top] = data;
	ps->_top++;
}
// 出栈 
void StackPop(Stack* ps)
{
	assert(ps);
	assert(ps->_top > 0);
	ps->_top--;
}
// 获取栈顶元素 
STDataType StackTop(Stack* ps)
{
	assert(ps);
	assert(ps->_top > 0);
	return ps->_a[ps->_top - 1];
}
// 获取栈中有效元素个数 
int StackSize(Stack* ps)
{
	assert(ps);
	return ps->_top;
}
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 
int StackEmpty(Stack* ps)
{
	assert(ps);
	assert(ps);
	return ps->_top == 0;
}
// 销毁栈 
void StackDestroy(Stack* ps)
{
	assert(ps);
	free(ps->_a);
	ps->_a = NULL;
	ps->_top = ps->_capacity = 0;
}

栈的图解

(可以想象一个竖直的容器,新元素从顶部加入,也是从顶部取出。)

二、队列(Queue)

队列是一种先进先出(FIFO)的数据结构,只允许在两端进行操作,一端为队头(front),另一端为队尾(rear)。队列的基本操作包括入队(enqueue)和出队(dequeue)。C语言实现队列

#include <stdio.h>  
#include <stdlib.h>  
#define MAX_SIZE 100  
  
typedef struct 
{  
    int data[MAX_SIZE];  
    int front, rear;  
} Queue;  
  
void initQueue(Queue *q) 
{  
    q->front = q->rear = -1;  
}  
  
int isFull(Queue *q) 
{  
    return (q->rear + 1) % MAX_SIZE == q->front;  
}  
  
int isEmpty(Queue *q) 
{  
    return q->front == -1;  
}  
  
void enqueue(Queue *q, int value) 
{  
    if (!isFull(q)) 
    {  
        q->data[++q->rear] = value;  
    } 
    else 
    {  
        printf("Queue is full!\n");  
    }  
}  
  
int dequeue(Queue *q) 
{  
    if (!isEmpty(q)) 
    {  
        int value = q->data[q->front++];  
        if (q->front > q->rear) q->front = q->rear = -1; // 处理队列为空的情况  
        return value;  
    } else 
    {  
        printf("Queue is empty!\n");  
        return -1;  
    }  
}  
  
// ... 其他队列操作函数 ...

队列的图解

(可以想象一个水平的容器,新元素从尾部加入,从头部取出。)

三、循环队列

循环队列是对普通队列的一种改进,通过取模运算实现队首和队尾的循环,从而更高效地利用存储空间。相当于队列头尾相接,同时容量固定.

(代码与上面的队列实现类似,主要区别在于isFullisEmpty的判断,以及frontrear的更新方式。)

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>

typedef struct
{
    int* a;
    int head; //指向头
    int tail; //指向尾下一个
    int k; //容量
} MyCircularQueue;


MyCircularQueue* myCircularQueueCreate(int k)
{
    MyCircularQueue* obj = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
    if (obj == NULL)
    {
        return;
    }
    
    //多开一个解决假溢出问题
    obj->a = (int*)malloc(sizeof(int)*(k + 1));
    obj->head = 0;
    obj->tail = 0;
    obj->k = k;
    return obj;
}

bool myCircularQueueIsEmpty(MyCircularQueue* obj) 
{
    return obj->head == obj->tail;
}

bool myCircularQueueIsFull(MyCircularQueue* obj) 
{
    return (obj->tail + 1) % (obj->k + 1) == obj->head;
}

bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) 
{
    if (myCircularQueueIsFull(obj))
    {
        return false;
    }
    obj->a[obj->tail] = value;
    obj->tail++;
    obj->tail %= (obj->k + 1);
    return true;
}

bool myCircularQueueDeQueue(MyCircularQueue* obj) 
{
    if (myCircularQueueIsEmpty(obj))
    {
        return false;
    }
    ++obj->head;
    obj->head %= (obj->k + 1);
    return true;
}

int myCircularQueueFront(MyCircularQueue* obj) 
{
    if (myCircularQueueIsEmpty(obj))
    {
        return -1;
    }
    else
    {
        return obj->a[obj->head];
    }
}

int myCircularQueueRear(MyCircularQueue* obj) 
{
    if (myCircularQueueIsEmpty(obj))
    {
        return -1;
    }
    else
    {
        return obj->a[(obj->tail + obj->k) % (obj->k + 1)];
    }
}

void myCircularQueueFree(MyCircularQueue* obj) 
{
    free(obj->a);
    free(obj);
}
四、栈实现队列

虽然栈是后进先出的数据结构,但我们可以通过两个栈(一个作为输入栈,一个作为输出栈)来模拟队列的先进先出特性。

代码实现

(主要涉及两个栈的push和pop操作,以及如何在适当的时候交换两个栈的角色。)

// 支持动态增长的栈
typedef int STDataType;
typedef struct Stack
{
	STDataType* _a;
	int _top;		// 栈顶
	int _capacity;  // 容量 
}Stack;
// 初始化栈 
void StackInit(Stack* ps)
{
	assert(ps);
	ps->_a = NULL;
	//top指向栈顶数据的下一个位置
	ps->_top = 0;
	//top指向栈顶数据
	//ps->_top = -1;
	ps->_capacity = 0;
}
// 入栈 
void StackPush(Stack* ps, STDataType data)
{
	assert(ps);
	if (ps->_top == ps->_capacity)
	{
		int newcapacity = ps->_capacity == 0 ? 4 : ps->_capacity * 2;
		STDataType* tmp = (STDataType*)realloc(ps->_a, newcapacity * (sizeof(STDataType)));
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		ps->_a = tmp;
		ps->_capacity = newcapacity;
	}
	ps->_a[ps->_top] = data;
	ps->_top++;
}
// 出栈 
void StackPop(Stack* ps)
{
	assert(ps);
	assert(ps->_top > 0);
	ps->_top--;
}
// 获取栈顶元素 
STDataType StackTop(Stack* ps)
{
	assert(ps);
	assert(ps->_top > 0);
	return ps->_a[ps->_top - 1];
}
// 获取栈中有效元素个数 
int StackSize(Stack* ps)
{
	assert(ps);
	return ps->_top;
}
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 
int StackEmpty(Stack* ps)
{
	assert(ps);
	assert(ps);
	return ps->_top == 0;
}
// 销毁栈 
void StackDestroy(Stack* ps)
{
	assert(ps);
	free(ps->_a);
	ps->_a = NULL;
	ps->_top = ps->_capacity = 0;
}

//leetcode

typedef struct 
{
	Stack pushst;
	Stack popst;
} MyQueue;


MyQueue* myQueueCreate() 
{
	MyQueue* obj = (MyQueue*)malloc(sizeof(MyQueue)); 
	if (obj == NULL)
	{
		return;
	}
	StackInit(&(obj->popst));
	StackInit(&(obj->pushst));
	return obj;
}

void myQueuePush(MyQueue* obj, int x) 
{
	StackPush(&(obj->pushst), x);
}

int myQueuePeek(MyQueue* obj) 
{
	if (StackEmpty(&(obj->popst)))
	{
		//倒数据
		while (!StackEmpty(&(obj->pushst)))
		{
			int top = StackTop(&(obj->pushst));
			StackPush(&(obj->popst), top);
			StackPop(&(obj->pushst));
		}
	}
	return StackTop(&(obj->popst));
}

int myQueuePop(MyQueue* obj) 
{
	int front = myQueuePeek(obj);
	StackPop(&(obj->popst));
	return front;
}

bool myQueueEmpty(MyQueue* obj) 
{
	return StackEmpty(&(obj->popst)) && StackEmpty(&(obj->pushst));
}

void myQueueFree(MyQueue* obj) 
{
	StackDestroy(&(obj->popst));
	StackDestroy(&(obj->pushst));
	free(obj);
}
五、队列实现栈

队列是先进先出的数据结构,但通过两个队列(或者一个队列和一个辅助栈)也可以模拟栈的后进先出特性。

代码实现

typedef char QDataType;
typedef struct QueueNode
{
	struct QueueNode* next;
	QDataType val;
}QNode;

typedef struct Queue
{
	QNode* phead;
	QNode* ptail;
	int size;
}Queue;
void QueueInit(Queue* pq)
{
	assert(pq);
	pq->phead = NULL;
	pq->ptail = NULL;
	pq->size = 0;
}

void QueueDestroy(Queue* pq)
{
	assert(pq);

	QNode* cur = pq->phead;
	while (cur)
	{
		QNode* next = cur->next;
		free(cur);

		cur = next;
	}

	pq->phead = pq->ptail = NULL;
	pq->size = 0;
}

// 队尾插入
void QueuePush(Queue* pq, QDataType x)
{
	assert(pq);

	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		return;
	}

	newnode->next = NULL;
	newnode->val = x;

	if (pq->ptail == NULL)
	{
		pq->phead = pq->ptail = newnode;
	}
	else
	{
		pq->ptail->next = newnode;
		pq->ptail = newnode;
	}

	pq->size++;
}

// 队头删除
void QueuePop(Queue* pq)
{
	assert(pq);
	assert(pq->size != 0);

	/*QNode* next = pq->phead->next;
	free(pq->phead);
	pq->phead = next;

	if (pq->phead == NULL)
		pq->ptail = NULL;*/

		// 一个节点
	if (pq->phead->next == NULL)
	{
		free(pq->phead);
		pq->phead = pq->ptail = NULL;
	}
	else // 多个节点
	{
		QNode* next = pq->phead->next;
		free(pq->phead);
		pq->phead = next;
	}

	pq->size--;
}

QDataType QueueFront(Queue* pq)
{
	assert(pq);
	assert(pq->phead);

	return pq->phead->val;
}

QDataType QueueBack(Queue* pq)
{
	assert(pq);
	assert(pq->ptail);

	return pq->ptail->val;
}


int QueueSize(Queue* pq)
{
	assert(pq);

	return pq->size;
}

bool QueueEmpty(Queue* pq)
{
	assert(pq);

	return pq->size == 0;
}
typedef struct
{
	Queue q1;
	Queue q2;
} MyStack;
//leetcode
MyStack* myStackCreate()
{
	MyStack* pst = (MyStack*)malloc(sizeof(MyStack));
	QueueInit(&(pst->q1));
	QueueInit(&(pst->q2));
	return pst;
}

void myStackPush(MyStack* obj, int x)
{
	if (!QueueEmpty(&(obj->q1)))
	{
		QueuePush(&(obj->q1), x);
	}
	else
	{
		QueuePush(&(obj->q2), x);
	}
}

int myStackPop(MyStack* obj)
{
	//假设法
	Queue* empty = &(obj->q1);
	Queue* nonempty = &(obj->q2);
	if (!QueueEmpty(&(obj->q1)))
	{
		nonempty = &(obj->q1);
		empty = &(obj->q2);
	}
	//不为空前size-1导走,删除最后一个就是栈顶数据
	while (QueueSize(nonempty) > 1)
	{
		QueuePush(empty, QueueFront(nonempty));
		QueuePop(nonempty);
	}
	int top = QueueFront(nonempty);
	QueuePop(nonempty);
	return top;
}

int myStackTop(MyStack* obj)
{
	if (!QueueEmpty(&(obj->q1)))
	{
		return QueueBack(&(obj->q1));
	}
	else
	{
		return QueueBack(&(obj->q2));
	}
}

bool myStackEmpty(MyStack* obj)
{
	return QueueEmpty(&(obj->q1)) && QueueEmpty(&(obj->q2));
}

void myStackFree(MyStack* obj)
{
	QueueDestroy(&(obj->q1));
	QueueDestroy(&(obj->q2));
	free(obj);
}

分享到这里,欢迎翻阅我的文章.

  • 11
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
多级反馈队列调度算法(Multilevel Feedback Queue Scheduling Algorithm)是一种常见的进程调度算法。实现多级反馈队列调度算法的思路是:将进程分成若干级别,每个级别使用不同的时间片大小,优先级较高的进程使用较小的时间片,优先级较低的进程使用较大的时间片,同时,每个级别内部使用先进先出(FIFO队列进行调度。 在C语言实现多级反馈队列调度算法,需要定义进程结构体、就绪队列、运行队列等数据结构,同时,使用循环和条件语句实现进程调度的逻辑。具体实现过程包括以下步骤: 1. 定义进程结构体:包括进程名称、进程状态、优先级等属性。 2. 初始化多个就绪队列:根据设定的进程级别,初始化多个就绪队列。可以使用数组或链表等数据结构来存储就绪队列。 3. 将进程加入就绪队列:在进程创建或者进程状态转换时,将进程加入对应的就绪队列。 4. 实现调度函数:根据多级反馈队列调度算法的逻辑,实现调度函数,即选择优先级最高或等级最高的进程进行调度,如果进程未完成,则将该进程插入到合适的下一级就绪队列,继续等待调度。 5. 实现时间片轮转:在进程使用时间片用完或者进程等待时间过长时,使用时间片轮转(Round-Robin)算法,将该进程插入到下一个就绪队列,等待调度。 6. 实现进程挂起和恢复:在某些特定情况下,需要将进程挂起或恢复,例如I/O操作等。这个可以通过修改进程状态和队列进程位置来实现。 总体来说,C语言实现多级反馈队列调度算法需要灵活运用数据结构和逻辑控制,正确处理进程状态和就绪队列,使得系统的进程调度可靠高效。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小志biubiu

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

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

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

打赏作者

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

抵扣说明:

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

余额充值