数据结构-------线性关系------队列

队列:(queue) 又称FIFO,first input first output
只允许在两端进行操作,尾部插入,头部删除的一个线性表。
队列的作用:
 缓存, 匹配输入和输出两种不用的速度.
1.顺序存储 --  小复杂 
使用普通数组实现,发现删除的时候,大量的搬移数据.
我们使用变通的方法: 环形队列
2.链式存储-- 就是使用链表完成FIFO.
判空:phead->next=NULL;
入列:从尾部不断的插入(追加)数据		
		ph->next=p;  ptail=ptail->next;

       

 

出列:

q=phead->next;    m=q->next; phead->next=m;   

下面是他的使用


		
#include<stdio.h>
#include<stdlib.h>
#include<string.h>


struct node {
	int val;
	struct node *next;
};


struct node head;

struct node *phead=&head;
struct node *ptail=&head;

void list_queue_init(struct node *phead)
{
	phead->next=NULL;
}

void list_queue_push(int val)
{
	struct node *p;

	p=malloc(sizeof(*p));
	p->val=val; p->next=NULL;
	
	ptail->next=p;
	ptail=ptail->next;
}

int list_queue_is_empty(struct node *phead)
{
	return phead->next == NULL ?1:0;
}

int list_queue_pop(struct node *phead,int *val)
{
	
	if( list_queue_is_empty(phead)==1   ){
		return -1;
	}

	
	struct node *p=phead->next;
	struct node *m=p->next;
	if(p->next == NULL){ 
		ptail=phead;
	}
	phead->next=m;
	*val=p->val;
	free(p) ;

	return 0;
}

int main()
{

	struct node *p;

	//init  
	list_queue_init(phead);

	//push queue 
	for(int i=1;i<=10;i++){
		list_queue_push(i);
	}

	int val;
	//pop 
	for(int i=0;i<12;i++){
		int ret = list_queue_pop(phead,&val);
		if(ret <0){
			printf("sorry nothing to pop\n");
			break;
		}

		printf("%d ",val);
	}

	printf("\n");

	return 0;
}

                       

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值