Linear structure queue
The structure of computer input-output buffer is the application of queue. Queue is another kind of restricted linear table, which allows only one end of the table to be deleted, while the other end of the table to be deleted. This structure is called queue or queue. The end that allows insertion is called the end of the queue, the end that allows deletion is called the head of the queue, and the insertion of the queue. The operation is called queuing, the deletion of the queue is called out of the queue or out of the queue, and the empty queue is called when there are no elements in the queue. The queue is the FIFO table.
The basic operation is as follows:
(1) Queue initialization
(2) Entry operation
(3) Out-of-line operation
(4) Reading Team Head Elements
(5) Judging team air operations
1. Sequential queue:
#include<stdio.h>
#include<malloc.h>
#define MAXSIZE 100
#define TRUE 1
#define FALSE 0
typedef int dataType;
typedef int boolean;
typedef struct SeQueue {
dataType data[MAXSIZE];
int rear,front;
}SeQueue;
void initQueue(SeQueue **s); //Initialization queue
boolean isEmpty(int rear); //Judging Team Air
boolean isFull(int rear); //Judging Team Full
void inQueue(SeQueue *s, dataType data); //Entry operation
dataType outQueue(SeQueue *s); //Out-of-line operation
dataType getQueue(SeQueue *s); //Get the team leader operation
dataType getQueue(SeQueue *s) {
int index = s->front + 1;
if(isEmpty(s->rear)) {
printf("To be null.\n");
return;
}
return s->data[index];
}
dataType outQueue(SeQueue *s) {
if(isEmpty(s->rear)) {
printf("The queue was empty and could not get out of the queue..\n");
return;
}
printf("out : %d\n\n", s->front);
return s->data[++s->front];
}
void inQueue(SeQueue *s, dataType data) {
if(isFull(s->rear)) {
printf("The queue is full and unable to join.\n");
return;
}
s->data[++s->rear] = data;
}
boolean isFull(int rear) {
return MAXSIZE <= rear ? TRUE : FALSE;
}
boolean isEmpty(int rear) {
return rear <= -1 ? TRUE : FALSE;
}
void initQueue(SeQueue **s) {
if(*s != NULL) {
printf("Has been initialized.\n");
return;
}
(*s) = (SeQueue *)malloc(sizeof(SeQueue));
(*s)->front = -1;
(*s)->rear = -1;
}
void main(void) {
SeQueue *s = NULL;
initQueue(&s);
inQueue(s, 5);
inQueue(s, 6);
inQueue(s, 7);
printf("\n%d\n", outQueue(s));
printf("\n%d\n", outQueue(s));
printf("\n%d\n", getQueue(s));
printf("%d %d", s->front, s->rear);
}
There is a false overflow in the sequential queue, which is caused by the operation of "the end of the queue, the head of the queue".
2. Cyclic queue
One way to solve this problem is to use data[0... MAXSIZE-1] is regarded as a cyclic structure with head-to-tail connection, that is to say, the last unit is followed by the first unit. In this way, the data area looks like a loop, which is called circular queue.
The construction method of cyclic table can make use of mathematical modular operation to add the end pointer of the queue to 1 and modify it as follows:
sq->rear = (sq->rear + 1) % MAXSIZE
Add 1 to the pointer of the team leader at the time of leaving the team, and modify it to read as follows:
sq->front = (sq->front + 1) % MAXSIZE
Conditions for team fulfilment:
(sq->rear + 1)% MAXSIZE == sq->front
Code correlation:
#include<stdio.h>
#include<malloc.h>
#define MAXSIZE 100
#define TRUE 1
#define FALSE 0
typedef int boolean;
typedef int dataType;
typedef struct CSeQueue {
dataType data[MAXSIZE];
int front, rear;
}CSeQueue;
void initSeQueue(CSeQueue **cs);
boolean inSeQueue(CSeQueue *cs, dataType data);
dataType outSeQueue(CSeQueue *cs);
void destorySeQueue(CSeQueue **cs);
void destorySeQueue(CSeQueue **cs) {
if(*cs == NULL) {
return;
}
free(*cs);
}
dataType outSeQueue(CSeQueue *cs) {
if(cs->front == cs->rear) {
printf("The queue was empty.\n");
return FALSE;
}
cs->front = (cs->front + 1) % MAXSIZE;
return cs->data[cs->front];
}
boolean inSeQueue(CSeQueue *cs, dataType data) {
if((cs->rear + 1) % MAXSIZE == cs->front) {
printf("The queue is full.\n");
return FALSE;
}
cs->rear = (cs->rear + 1) % MAXSIZE;
cs->data[cs->rear] = data;
}
void initSeQueue(CSeQueue **cs) {
if(*cs != NULL) {
printf("The queue has been initialized.\n");
return;
}
(*cs) = (CSeQueue*)malloc(sizeof(CSeQueue));
(*cs)->front = (*cs)->rear = MAXSIZE-1; //Change the value of front, rear to the subscript of the direct precursor of the first unit
}
void main(void) {
CSeQueue *cs = NULL;
initSeQueue(&cs);
inSeQueue(cs, 5);
inSeQueue(cs, 6);
inSeQueue(cs, 7);
inSeQueue(cs, 8);
outSeQueue(cs);
outSeQueue(cs);
destorySeQueue(&cs);
}
Code address: http://download.csdn.net/detail/dear_mr/9815785