【数据结构】队列

文章目录

操作

LinkQueue* InitQueue()
{
	LinkQueue* s = (LinkQueue*)malloc(sizeof(LinkQueue));
	s->front = s->rear = NULL;
	return s;
}

void DestroyLinkQueue(LinkQueue* s)
{
	qQueue* pre = s->front;
	qQueue* p = s;
	if (pre)
	{
		p = pre->next;
		while (p)
		{
			free(pre);
			pre = p;
			p = p->next;
		}
		free(pre);
	}
	free(s);
}


bool  LinkQueueEmpty(LinkQueue* s)
{
	return (s->front == NULL);
}

void EnLinkQueue(LinkQueue* s,ElemType e)
{
	qQueue* t = (qQueue*)malloc(sizeof(qQueue));
	t->data = e;
	if (s->front = NULL)
		s->front = s->rear = t;
	else
	{
		s->rear->next = t;
		s->rear = t;
		t->next = NULL;
	}
}

void DeLinkQueue(LinkQueue* s,ElemType * e)
{
	if (s->front == NULL)
		return false;
	qQueue* t = s->front ;
	if (t->next = NULL)
		s->front = s->rear = NULL;
	else
		s->front = t->next;
	*e = t->data;
	free(t);
	return true;
}


//只带尾指针的循环队列
qQueue* InitRearQueue()
{
	qQueue* s = NULL;
	return s;
}

void EnRearQueue(qQueue** s,ElemType e)
{	
		qQueue* t = (qQueue*)malloc(sizeof(qQueue));
		t->data = e;
		if (!s)
		{
			t->next = t;			
			*s = t;
		}
		t->next = (*s)->next;
		(*s)->next = t;
		(*s) = t;			
}

bool	RearQueue(qQueue* s, ElemType* e)
{
	if (s == NULL)
		return false;
	qQueue* t = s->next;
	if (s->next = s)
	{
		*e = s->next;
		free(s);
		s = NULL;
	}
	else
	{
		*e = t->data;
		s->next = t->next;
		free(t);
	}
	return true;
}

bool RearQueueEmpty(qQueue* s)
{
	return (s == NULL);
}

应用

//设有n个人站成一排,从左向右的编号分别为1~n,现在从左往右报数“1,2,1,2…”
//数到“1”的人出列,数到“2”的立即站到队伍的最右端。
//报数过程反复进行,直到n个人都出列为止。
//要求给出他们的出列顺序
//例如,当n=8时初始序列为:12345678
//则出列顺序为:13572648

void QueueNumber(int n)
{
	int e = 0;
	LinkQueue* s = InitLinkQueue();
	for (int i =1; i <= n; i++)
		EnLinkQueue(s, i);	
	while (!LinkQueueEmpty(s))
	{
		DeLinkQueue(s, &e);
		printf("%d\t", e);
		if (!LinkQueueEmpty(s) )
		{
			DeLinkQueue(s, &e);
			EnLinkQueue(s, e);
		}
	}
	printf("\n");
	
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值