#include<stdio.h>
#include<stdlib.h>
typedef struct QNode
{ int data;
struct QNode *next;
}LQNode;
typedef struct
{ LQNode *front;
LQNode *rear;
}LQueue;
void Init(LQueue *Q)
{ Q->front=NULL;
Q->rear=NULL;
}
int Enqueue(LQueue *Q,int x)//入队
{ LQNode *p;
p=(LQNode *)malloc(sizeof(LQNode));
p->data=x;
p->next=NULL;
if(Q->rear!=NULL) Q->rear->next=p;
Q->rear=p;
if(Q->front==NULL) Q->front=p;
return 1;
}
int Disqueue(LQueue *Q,int *x)//出队
{ LQNode *p;
if(Q->front==NULL)
{ printf("队列已空无数据元素出队列!\n");
return 0;
}
else{
*x=Q->front->data;
p=Q->front;
Q->front=Q->front->next;
if(Q->front==NULL) { Q->rear=NULL;}
free(p);
return 1;
}
}
int main()
{ LQueue Q;
int n,i;
int x,y;
Init(&Q);
printf("请输入队列的规模:");
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("请输入第%d个的值:",i+1);
scanf("%d",&x);
Enqueue(&Q,x);
}
Disqueue(&Q,&y);
Disqueue(&Q,&y);
printf("%d\n",y);
return 0;
}
链队列的操作
最新推荐文章于 2021-11-22 12:25:41 发布