![]() |
#2
楚川杉2020-10-15 13:37
|



#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
typedef int datatype;
typedef struct node{
datatype data;
struct node *next;
}Qnode;
typedef struct{
Qnode *rear;
Qnode *front;
int length;
}LinkQueue;
int main(int argc, char *argv[]) {
void initQueue(LinkQueue *q);
void enQueue(LinkQueue *q,datatype e);
int outQueue(LinkQueue *q,datatype *e);
LinkQueue a;
datatype x;
int i;
initQueue(&a);
enQueue(&a,1);
enQueue(&a,3);
enQueue(&a,5);
enQueue(&a,6);
printf("出队列:");
for(i=0;i<3;i++){
if(outQueue(&a,&x)==1){
outQueue(&a,&x);
printf("%d ",x);
}
}
return 0;
}
void initQueue(LinkQueue *q){
//没有头节点的链表
// q->front=q->rear=(Qnode *)malloc(sizeof(Qnode));
//q->front->data=NULL;
Qnode *head;
head=(Qnode *)malloc(sizeof(Qnode));
head->next=NULL;
q->front=head;
q->rear=q->front;
q->length=0;
}
void enQueue(LinkQueue *q,datatype e){//链式队列 进队列操作没有上限,可以返回值void
Qnode *s;
s->data=e;
s->next=NULL;
q->rear->next=s;
q->rear=s;
q->length++;
}
int outQueue(LinkQueue *q,datatype *e){
Qnode *s;
if(q->rear==q->front){
printf("队列已空\n");
return 0;
}
else{
s=q->front->next;
*e=s->data;
//如果没有头节点 s=q->front;
q->front->next=s->next;
if(s->next==NULL){//队列只有一个元素的情况
q->rear=q->front;//防止q->rear成为野指针
}
free(s);
q->length--;
return 1;
}
}
只有本站会员才能查看附件,请 登录