【自考】数据结构第三章，队列，期末不挂科指南，第4篇

队列

队列基本概念

data:一维数组，存储队列中的数据元素
font：指向队列首元素的前一个单元
rear：指向实际的队列尾元素单元

SQ.rear = SQ.rear+1;
SQ.data[SQ.rear] = x;


SQ.front = SQ.front+1


[En]

So in order to solve this false overflow problem, smart developers have come up with a new solution to create a ring.

循环队列

SQ.rear = (SQ.rear+1) % maxsize ;
SQ.data[SQ.rear] = x;


SQ.front = (SQ.front+1)%maxsize;


顺序队列的C语言实现

[En]

After a meal of operation, it is still relatively simple, in short, do not write chain storage, the order is still relatively simple

#include
#include

//循环队列最大数据元素个数
const int maxsize = 8;

//循环队列的结构体
typedef struct cycqueue{
int *data;
int front,rear;
} CycQue;

//队列初始化
void init(CycQue *CQ){
CQ->data = (int *)malloc(maxsize*sizeof(int));
CQ->front = 0;
CQ->rear = 0;
}

//判断队列是否为空
int empty(CycQue *CQ){
if(CQ->rear==CQ->front) return 1;
else return 0;
}

//入队列
int EnQueue(CycQue *CQ,int x){
if((CQ->rear+1)%maxsize==CQ->front){
printf("队列满");
return 0;
}
else{
CQ->rear =(CQ->rear+1) % maxsize;
CQ->data[CQ->rear] = x;
return 1;
}

}

//出队列
int OutQueue(CycQue *CQ){
if(empty(CQ)){
printf("队列为空");
return 0;
}
else{
CQ->front = (CQ->front+1) % maxsize;
return 1;

}

}

int main()
{
CycQue CQ;
init(&CQ);

EnQueue(&CQ,2);
EnQueue(&CQ,4);
printf("%d",CQ.rear);
OutQueue(&CQ);
printf("%d",CQ.front);
return 0;
}



链式队列的C语言实现

[En]

In fact, after the chain queue has previous experience, it will not be too difficult to write, so let’s write a core part of the code.

#include
#include
int *data;
} LkQueNode;

typedef struct LkQueue{
LkQueNode *front,*rear;
} LkQue;

void init(LkQue *LQ){
LkQueNode *temp;
temp = (LkQueNode *)malloc(sizeof(LkQueNode)); //生成队列的头结点
LQ->front = temp;    //队列头指针指向队列头结点
LQ->rear = temp;     //队列尾指针指向队列尾结点
(LQ->front)->next = NULL;

}



//入队列
void EnQueue(LkQue *LQ,int x){

LkQueNode *temp;
temp = (LkQueNode *)malloc(sizeof(LkQueNode));
temp->data = x;
temp-next = NULL;
(LQ->rear)->next = temp;
LQ->rear = temp;
}


自考要点

Original: https://www.cnblogs.com/happymeng/p/shujujiegou_4.html
Author: 梦想橡皮擦
Title: 【自考】数据结构第三章，队列，期末不挂科指南，第4篇

(0)