急求救关于队列的.感谢解答者。

来源:百度知道 编辑:UC知道 时间:2024/06/10 11:10:41
#include<stdio.h>
#include<malloc.h>
#define error -1
#define ok 1
typedef struct Qnode
{int data;
struct Qnode *next;
}Qnode,*Queueptr;
typedef struct
{Queueptr front;
Queueptr rear;
}Linkqueue;
int creatqueue(Linkqueue *Q,int n)
{
Queueptr p;
Linkqueue q;
int i;
q=*Q;
q.front=q.rear=(Queueptr)malloc(sizeof(Qnode));
if(!q.rear)return error;
for(i=0;i<n;i++)
{
p=(Queueptr)malloc(sizeof(Qnode));
if(!p)return error;
scanf("%d",&p->data);
q.rear->next=p;
q.rear=p;
}
q.rear->next=NULL;
return ok;
}
void printqueue(Linkqueue Q)
{Linkqueue R;
R=Q;
while(R.front!=R.rear)
{R.front=R.front->next;
printf("%d ",R.front->data);
}
printf("\n");
}
void main()
{
Linkqueue Q;
creatqueue(&Q,5);
printqueue(Q)

把此函数修改一下

int creatqueue(Linkqueue *Q,int n)
{
Queueptr p;
// Linkqueue q;
int i;
// q=*Q;
Q->front=Q->rear=(Queueptr)malloc(sizeof(Qnode));
if(!Q->rear)return error;
for(i=0;i<n;i++)
{
p=(Queueptr)malloc(sizeof(Qnode));
if(!p)return error;
scanf("%d",&p->data);
Q->rear->next=p;
Q->rear=p;
}
Q->rear->next=NULL;
return ok;
}