#include<stdio.h>
#include<stdlib.h>
typedef struct LQueue
{
int data;
struct LQueue * next;
}LQueue;
void Create_LQueue(LQueue * p)
{
int x;
LQueue * s;
printf("请输入入队的元素(输入888结束):");
scanf_s("%d", &x);
if(x!=888)
{
while(x!=888)
{
s=(LQueue *)malloc(sizeof(LQueue));
s->data=x;
s->next=NULL;
p->next=s;
p=s; //记着别漏了
scanf_s("%d", &x);
}
}
else
p->next=NULL;
}
void En_LQueue(LQueue * p, int x)
{
LQueue * s;
s=(LQueue *)malloc(sizeof(LQueue));
s->data=x;
s->next=NULL;
while(p->next!=NULL)
{
p=p->next;
}
p->next=s;
p=s;
}
void De_LQueue(LQueue * p, int * x)
{
LQueue * s;
s=p->next;
*x=s->data;
p->next=s->next;
free(s);
printf("元素%d出队\n", *x);
}
void Printf(LQueue * p)
{
LQueue * s;
s=p->next;
if(s)
{
printf("队列的内容为:");
while(s)
{
printf("%d ", s->data);
s=s->next;
}
printf("\n");
}
else
printf("队列为空,无法打印!\n");
}
int Length(LQueue * p)
{
LQueue * s;
int len=0;
s=p->next;
if(s)
{
while(s)
{
len++;
s=s->next;
}
return len;
}
else
return 0;
}
int main()
{
LQueue p;
int x=999;
int t; int i;
Create_LQueue(&p);
Printf(&p);
printf("队列长度:%d\n", Length(&p));
printf("将999入队:\n");
En_LQueue(&p, x);
Printf(&p);
printf("元素全部出队:\n");
while((&p)->next) //p不是指针,通过<&p>和循环遍历队列
{
De_LQueue(&p, &t);
}
Printf(&p);
printf("队列长度:%d\n", Length(&p));
}