循环队列操作

本文详细介绍了如何在C语言中使用结构体实现一个循环队列,包括创建队列、插入元素以及遍历队列的功能。通过`createqueue`、`Insertqueue`和`showqueue`函数展示了队列的核心操作。
摘要由CSDN通过智能技术生成

主函数:

#include <stdio.h>                        
#include "./03queue.h"                    
int main(int argc, const char *argv[])    
{                                         
    queue* shoudizhi = createqueue();     
                                          
    Insertqueue(shoudizhi,1);             
    Insertqueue(shoudizhi,2);             
    Insertqueue(shoudizhi,3);             
    Insertqueue(shoudizhi,4);             
    Insertqueue(shoudizhi,5);             
    Insertqueue(shoudizhi,6);             
    showqueue(shoudizhi);                 
    return 0;                             
}                                         
                                          
                                          

头文件:

#ifndef __QUEUE_H__
#define __QUEUE_H__
#define N 10
typedef struct elem
{
    int data[N];
    int front;
    int rear;
}queue;


queue* createqueue();
void Insertqueue(queue* head,int num);

void showqueue(queue* head);
                                      








#endif

函数文件:

在这里插入代码片#include <stdio.h>
#include <stdlib.h>
#include "./03queue.h"

/*
 * function:    创建循环队列
 * @param [ in] 
 * @param [out] 
 * @return      
 */

queue* createqueue()
{
	queue* head = (queue*)malloc(sizeof(queue));
	if(NULL == head)
	{
		printf("栈区申请失败,循环队列创建失败\n");
		return NULL;
	}
	head->front = head->rear = 0;
	return head;
}

/*
 * function:    插入循环队列
 * @param [ in] 
 * @param [out] 
 * @return      
 */

void Insertqueue(queue* temp,int num)
{

	temp->data[temp->rear] = num;
	temp->rear = (temp->rear+1)%N;
}

/*
 * function:    循环队列遍历
 * @param [ in] 
 * @param [out] 
 * @return      
 */

void showqueue(queue* head)
{
	int i = head->front;
	printf("----------");
	for(i = head->front;i!=head->rear;i=(i+1)%N)
	{
		printf("%d\n",head->data[i]);
	}

}




头疼写不了













  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值