搜档网
当前位置:搜档网 › 编写链接队列的基本操作函数

编写链接队列的基本操作函数

(1)编写链接队列的基本操作函数
1.进队函数 EnQueue(LinkQueue *Q,QElemType e)
2.出队函数 ,队空 DeQueue(LinkQueue *Q,QElemType e)
3.输出队列中元素 OutputQueue(LinkQueue *Q)

(2)调用上述函数实现下列操作,操作步骤如下
1.调用进队函数建立一个队列
2.读取队列中的第一个元素
2.从队列中删除元素
4.输出队列中的所有元素

(3)编写环形队列的基本操作函数
1.进队函数 EnQueue(SqQueue *Q,QElemType e)
2.出队函数 ,队空 DeQueue(SqQueue *Q,QElemType e)
3.输出队列中元素 OutputQueue(SqQueue *Q)
(4)调用上述函数实现下列操作,操作步骤如下
1.调用进队函数建立一个队列
2.读取队列中的第一个元素
2.从队列中删除元素
4.输出队列中的所有元素








1.链接队列:
#include
#include
typedef struct node
{int data;
struct node *next;
};
typedef struct
{struct node *front;
struct node *rear;
}LinkQueue;

InitQueue(LinkQueue *Q)
{ Q->front=(struct node *)malloc(sizeof( struct node));
Q->rear=Q->front;
Q->front->next=NULL;
}

EnQueue(LinkQueue *Q,int e)
{struct node *p;
p=(struct node *)malloc(sizeof(struct node ));
p->data=e;
p->next=NULL;
Q->rear->next=p;
Q->rear=p;
}

DeQueue(LinkQueue *Q,int e)
{ struct node *p;
if(Q->front==Q->rear)
return 0;

else{
p=Q->front->next;
Q->front->next=p->next;
if(p->next=NULL)Q->rear=Q->front;
return(p->data);
free(p);
}
}

OutputQueue(LinkQueue *Q)
{ struct node *p;
p=Q->front->next;
while(p!=NULL)
{ printf("%d ",p->data);
p=p->next;
}
}

GetFront(LinkQueue *Q)
{ struct node *p;
p=Q->front->next;
printf("%d",p->data);
}


void main()
{ LinkQueue s;
int i,max,e,item;
InitQueue(&s);
printf("put a max:");
scanf("%d",&max);
printf("shu ru yuan su");
for(i=0;iscanf("%d",&e);
EnQueue(&s,e);}
OutputQueue(&s);
printf("\n");
printf("di yi ge yuan su wei:");
GetFront(&s);
printf("\n");
printf("shu ru shan chu yuan su :");
scanf("%d",&item);
DeQueue(&s,item);
OutputQueue(&s);
printf("\n");
}







2.环形队列:
#define MAXQSIZE 100
#include
#include
typedef struct{
int *base;
int front;
int rear;
}SqQueue;

InitQueue(SqQueue *Q)
{ Q->base=(int *)malloc(MAXQSIZE * sizeof(int));
if(!Q->base)exit(1);
Q->front=Q->rear=0;
}

EnQueue(SqQueue *Q,int e)
{ Q->base[Q->rear]=e;
Q->rear=(Q->rear+1)%MAXQSIZE;
}

DeQueue(SqQueue *Q,int *e)
{ if(Q->front==Q->rear)return 0;
e=Q->base[Q->front];
Q->front=(Q->front+1)%MAXQSIZE;
}

GetFront(SqQueue *Q)
{ if(Q->front==Q->rear)return 0;
else printf("%d",Q->base[(Q->front)%MAXQSIZE]);
}

OutputQueue(SqQueue *Q)
{ int i;
i=Q->front;
if(!(Q->front==Q->rear))
{
while((

i%MAXQSIZE)!=Q->rear)
{ printf("%d ",Q->base[i%MAXQSIZE]);
i++;
}
}

}


void main()
{ SqQueue *s;
int i,max,e,item;
InitQueue(&s);
printf("put a max:");
scanf("%d",&max);
printf("shu ru yuan su :");
for(i=0;iscanf("%d",&e);
EnQueue(&s,e);}
OutputQueue(&s);
printf("\n");
printf("di yi ge yuan su wei :");
GetFront(&s);
printf("\n");
printf("shu ru shan chu yuan su:");
scanf("%d",&item);
DeQueue(&s,item);
OutputQueue(&s);
}

相关主题