数据结构重学日记(十三)队列的操作
2019-01-16 本文已影响0人
南瓜方糖
写了这么多代码给我只算了8个字?
顺序队列
#define MaxSize 5
typedef int ElemType;
typedef struct {
ElemType data[MaxSize];
int front, rear;
} SqQueue;
void queue_init(SqQueue *SQ) {
SQ->front = SQ->rear = 0;
}
bool en_queue(SqQueue *SQ, ElemType x) {
if ((SQ->rear + 1) % MaxSize == SQ->front) return false; // 队满
SQ->data[SQ->rear] = x;
SQ->rear = (SQ->rear + 1) % MaxSize;
return true;
}
bool de_queue(SqQueue *SQ, ElemType x) {
if (SQ->rear == SQ->front) return false; // 队空
x = SQ->data[SQ->front];
SQ->front = (SQ->front + 1) % MaxSize;
return true;
}
void print_queue(SqQueue *SQ) {
for (int i = SQ->front; i < SQ->rear; i++) {
printf("%3d",SQ->data[i]);
}
printf("\r\n\r\n");
}
链式队列
//
// Created by wu on 19-1-14.
//
#include <stdbool.h>
typedef int ElemType;
typedef struct LinkNode{ // 链式队列结点 强制类型转换
ElemType data;
struct LinkNode * next;
}LinkNode;
typedef struct { // 链式队列
LinkNode * front, * rear; //队首队尾指针
}LinkQueue;
void link_queue_init(LinkQueue * LQ){
LQ->rear = LQ->front = (LinkNode *)malloc(sizeof(LinkNode));
LQ->front->next = NULL;
}
void en_link_queue(LinkQueue * LQ, ElemType x){
LinkNode * s = (LinkNode *)malloc(sizeof(LinkNode));
s->data = x;
s->next = NULL;
LQ->rear->next = s;
LQ->rear = s;
}
int de_link_queue(LinkQueue * LQ, ElemType x){
if(LQ->front == LQ->rear) return 0; // 空队
LinkNode *p = LQ->front->next;
x = p->data;
LQ->front->next = p->next;
if(LQ->rear == p) LQ->rear = LQ->front;
free(p);
return x;
}
void print_link_queue(LinkQueue *LQ){
if(LQ->front == LQ->rear){
printf("空");
return;
}
LinkNode * s = LQ->front;
while(s != LQ->rear){
s = s->next;
printf("%3d",s->data);
}
printf("\r\n\r\n");
}