C语言实现数据结构链队列

实验内容

1.初始化链队列

2.判断队列是否为空

3.元素x进队列

4.出队列

5.取队头元素

6.取队尾元素

7.求队列元素个数

8.打印队列元素

#include<stdio.h>
#include<malloc.h>
typedef int dataType ;
struct node{
	dataType data;
	struct node * next;
};
typedef struct {
	struct node *front ,*rear;
}Linkqueue;
//初始化链队列
void Initqueue(Linkqueue *q){
  	q->front = (node*)malloc(sizeof(node));
  	q->rear = q->front ; 
} 
//判断是否为空
int empty(Linkqueue *q){
	return q->front == q->rear;
} 
//元素x进队列
void push(Linkqueue *q,dataType x){
	node *t = (node*)malloc(sizeof(node));
	t->data = x;
	t->next = NULL;
	q->rear->next = t;
	q->rear = t;
} 
//出队列
void pop(Linkqueue *q){
	if(empty(q)) exit(1);
	node *p = q->front->next;
	q->front->next = p->next;
	free(p);
} 
//取对头元素值
dataType front(Linkqueue *q){
	return q->front->next->data;
} 
//取队尾元素值
dataType rear(Linkqueue *q){
	return q->rear->data;
} 
//求队列元素个数
int size(Linkqueue *q){
	node *p = q->front->next;
	int k = 0;
	while(p){
		k++;
		p = p->next;
	} 
	return k;
} 
//打印队列元素 
void Print(Linkqueue *Q)
{
	node *p=Q->front;
	for(Q->front;Q->front->next!=NULL;Q->front=Q->front->next)
		printf("%d\n",Q->front->next->data);
	Q->front=p;
}
int main()
{
	Linkqueue queue,*q = &queue;
	Initqueue(q);
	push(q,80);
	push(q,90);
	push(q,100);
	//pop(q);
	Print(q);
	printf("对头元素为:%d\n",front(q));
	printf("对尾元素为:%d\n",rear(q));
	printf("队列元素有:%d个\n ",size(q));	
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值