实验内容

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));
}


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