linux编译c文件for循环,Linux C 循环队列的实现

1、创建circle_queue.h 文件

#ifndef CIRCLE_QUEUE_H_

#define CIRCLE_QUEUE_H_

#define SUCCESS 1

#define FAILED -1

typedef int QElemType;// 队列的顺序存储结构(可用于循环队列和非循环队列)

#define MAXSIZE 1000 // 最大队列长度(对于循环队列,最大队列长度要减1)

typedef struct circle_queue circle_queue;

struct circle_queue

{

QElemType *base;// 初始化的动态分配存储空间,相当于一个数组头

int front;// 头指针,若队列不空,指向队列头元素,相当于一个下标

int rear; // 尾指针,若队列不空,指向队列尾元素的下一个位置,相当于一个下标

int count; // 元素个数

};

int init_queue(circle_queue *this); //初始化队列

int destroy_queue(circle_queue *this); //销毁队列

int queue_length(circle_queue *this); //获取队列长度

int insert_last(circle_queue *this,QElemType value); //插入队列尾部

int remove_first(circle_queue *this); //从队列头部移出

int is_empty_queue(circle_queue *this); //判断队列是否为空

int get_head(circle_queue *this,QElemType value); //若队列不空,获得队列的头元素

#endif

2、创建对应的

circle_queue.c文件

#include

#include

#include

#include "circle_queue.h"

//构造一个空队列Q,初始化队列

int init_queue(circle_queue *this)

{

//给队头队尾指针分配空间,并置空

this->base=malloc(MAXSIZE*sizeof(QElemType)); //this.base相当于数组头

if(!this->base) // 存储分配失败

exit(0);

this->front=this->rear=0;//下标初始化为0

return SUCCESS;

}

// 销毁队列queue

int destroy_queue(circle_queue *this)

{

if(this->base)

free(this->base);

this->base=NULL;

this->front=this->rear=0;//空队列的标志是队头队尾指针都相同,且为0

return SUCCESS;

}

//获取队列长度

int queue_length(circle_queue *this)

{

return(this->rear-this->front+MAXSIZE)%MAXSIZE;

}

//插入队列尾部,入队

int insert_last(circle_queue *this,QElemType value)

{

if(this->count == MAXSIZE) //队列满

{

return FAILED;

}

this->base[this->rear]=value;

this->rear=(this->rear+1)%MAXSIZE;

this->count++;

return SUCCESS;

}

//从队列头部移出,出队

int remove_first(circle_queue *this)

{

int value;

if (this->count==0) //队列空

{

return FAILED;

}

value=this->base[this->front];

this->front=(this->front+1)%MAXSIZE;

this->count--;

return value;

}

//判断队列是否为空

int is_empty_queue(circle_queue *this)

{

if(this == NULL)

{

return FAILED;

}

if (this->count==0)

{

return FAILED;

}

else

{

return SUCCESS;

}

}

//若队列不空,获得队列的头元素

int get_head(circle_queue *this,QElemType value)

{

if(this->front==this->rear)//队列空

{

return FAILED;

}

value=this->base[this->front];

return value;//获取队头元素值

}3、创建

circle_queue_test.c测试文件

#include

#include

#include

#include "circle_queue.h"

int main(int argc, char *argv[])

{

int i;

int count;

int value;

int empty;

circle_queue *queue=malloc(sizeof(circle_queue));//声明一个循环队列queue;

init_queue(queue);//循环队列queue初始化

empty = is_empty_queue(queue);//队列判空

if(empty > 0)

{

printf("队列不为空!\n");

}

else

{

printf("队列为空!\n");

}

printf("请输入元素的个数:\n");

scanf("%d",&count);

printf("请输入%d个入队元素:\n",count);

for(i=0;i

{

printf("第%d个:",i+1);

scanf("%d",&value);

insert_last(queue,value); //入队

}

empty = is_empty_queue(queue);//队列判空

if(empty > 0)

{

printf("队列不为空!\n");

}

else

{

printf("队列为空!\n");

}

count=queue_length(queue);

printf("队列的长度为:%d\n",count);

printf("队列中的元素为:");

for(i=0;i

{

printf("%d",queue->base[queue->front+i]);

if(i < (count-1))

{

printf("

}

}

printf("\n");

value=get_head(queue,value);

printf("队头元素为:%d\n",value);

printf("队列开始出队:\n");

for(i=0;i

{

printf("出队的元素为:第%d个:%d\n",i+1,remove_first(queue));

}

empty = is_empty_queue(queue);//队列判空

if(empty > 0)

{

printf("队列不为空!\n");

}

else

{

printf("队列为空!\n");

}

if(queue)

{

printf("队列开始销毁...\n");

}

destroy_queue(queue);//销毁循环队列queue

if(!(queue->base))

{

printf("队列销毁成功!\n");

}

return;

}

4、编译

gcc -o test circle_queue_test.c circle_queue.c

5、运行

./test

6、演示

0818b9ca8b590ca3270a3433284dd417.png

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值