Linux C 数据结构—队列

Linux C 数据结构—队列

一.队列特点

先进先出的线性表。

允许在两端进行插入和删除的线性表。

一端负责插入数据(入队。使用 rear 队尾指针),另一端负责删除数据(出队。使用 front 队头指针)

二.队列示例代码

#include <stdio.h>
#include <stdlib.h>
#define MAX  6

typedef struct 
{
	int data[MAX];     //顺序队列。 
	int front;         //队头指针。(出队时使用) 
	int rear;          //队尾指针。(入队时使用)
}sequeue_t;

sequeue_t *create_empty_queue()
{
	sequeue_t *sq = (sequeue_t *)malloc(sizeof(sequeue_t ));
	sq->front = 0;
	sq->rear = 0;
	return sq;
}

int empty_queue(sequeue_t *s)   //两指针指同一位置,称为对空。
{
	return (s->front == s->rear) ; 
}

int full_queue(sequeue_t *s)	//为了区分 队空 与 队满,规定满队个数,要比数组个数少1. 
{
	return ((s->rear+1)%MAX == s->front); 
}

int enter_queue(sequeue_t *s,int value)   //入队  
{
	if(full_queue(s))
	{
		printf("full\n");
		return -1;
	}
	s->data[s->rear++] = value ;  //值入队。 
	s->rear  = s->rear % MAX;     //队尾指针 ++;
	return 0;
}
int out_queue(sequeue_t *s) 
{
	if(empty_queue(s))
	{
		printf("empty_queue\n");
		return -1;
	}
	int x = s->data[s->front++];  //值出队。 
	s->front = s->front % MAX;    //队头指针 ++;
	return x;
}

//测试函数。 
int main()
{
	sequeue_t *h = create_empty_queue();
	enter_queue(h,1);
	enter_queue(h,2);
	enter_queue(h,3);
	enter_queue(h,4);
	enter_queue(h,5);
	enter_queue(h,6);

	printf("%d\n",out_queue(h));    //1
	printf("%d\n",out_queue(h));    //2

	enter_queue(h,6);
	enter_queue(h,7);

	printf("%d\n",out_queue(h));    //3
	printf("%d\n",out_queue(h));    //4
	printf("%d\n",out_queue(h));    //5
	printf("%d\n",out_queue(h));    //6
	printf("%d\n",out_queue(h));    //7
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值