队列的链式存储的一种实现方法就是简化版的线性链表
#include <stdio.h>
#include <stdlib.h>
typedef struct node//结点结构
{
int data;
struct node *next;
}node,*linklist;
typedef struct//队列的链表结构
{
linklist front,rear;
}linkqueue;
void create_queue(linklist *l)//队列的建立
{
(*l)=(node*)malloc(sizeof(node));
linklist p,r;
(*l)->next=NULL;
int i,j,x;
r=(*l);
printf("please input:");
scanf("%d",&j);
for(i=0;i<j;i++)
{
scanf("%d",&x);
p=(node*)malloc(sizeof(node));
p->data=x;
r->next=p;
r=p;
}
r->next=NULL;
}
void print_queue(linklist *l)//队列的打印输出
{
linklist p;
p=(*l)->next;
while(p)
{
printf("%d",p->data);
p=p->next;
}
}
void en_queue(linklist *l)//入队操作,在队尾加入一个元素
{
linklist p,r;
p=(*l)->next;
int x;
scanf("%d",&x);
while(p->next)
{
p=p->next;
}
r=(node*)malloc(sizeof(node));
r->data=x;
p->next=r;
r->next=NULL;
}
void de_queue(linklist *l)//出队操作,解决第一个元素
{
linklist p;
p=(*l)->next;
(*l)->next=p->next;
free(p);
}
int main()
{
linklist a;
create_queue(&a);
en_queue(&a);
de_queue(&a);
print_queue(&a);
return 0;
}