Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money
Queue order
- Using the one-dimensional array base[M]
- Team logo: Front = rear
- Join: base[rear++] = x
- X = base[front++]
Existing problems
If front ≠ 0 rear = M, false overflow
Solution — loop queues
- Implementation: the use of modular operations
- Base [rear] = x; rear = (rear+1) % M;
- X = base[front]; front = (front + 1) % M;
C++ code implementation
#include<iostream>
#include<stdlib.h>
using namespace std;
#define OK 1
#define ERROR -1
#define OVERFLOW -2
typedef int Status;
typedef int QElemType;
#define MAXSIZE 100 // Maximum length
/*------------ static allocation ------------*/
//typedef struct {
// QElemType elem[MAXSIZE];
// QElemType* rear; // end of queue pointer
// QElemType* front; // queue head pointer
// int length; / / the length
//}SqQueue;
/*------------ dynamic allocation ------------*/
typedef struct {
QElemType* elem; // Dynamically allocating storage space initializes
int rear; / / the tail pointer
int front; / / the head pointer
}SqQueue;
// Construct an empty queue
Status InitSqQueue(SqQueue& Q) {
Q.elem = new QElemType[MAXSIZE];
if(! Q.elem)exit(OVERFLOW);
Q.front = Q.rear = 0;
return OK;
}
// Queue length
int QueueLength(SqQueue Q) {
return(Q.rear - Q.front) % MAXSIZE;
}
// Check whether the queue is empty
bool IsSqQueueEmpty(SqQueue Q) {
return Q.rear == Q.front;
}
// Check whether the queue is full
bool IsSqQueueFull(SqQueue Q) {
return (Q.rear + 1) % MAXSIZE == Q.front;
}
/ / team
Status PushSqQueue(SqQueue& Q, QElemType e) {
if (IsSqQueueFull(Q)) return ERROR;
Q.elem[Q.rear] = e;
Q.rear = (Q.rear + 1) % MAXSIZE;
return OK;
}
/ / out of the team
Status PopSqQueue(SqQueue& Q, QElemType &e) {
if (IsSqQueueEmpty(Q)) return ERROR;
e = Q.elem[Q.front];
Q.front = (Q.front + 1) % MAXSIZE;
return OK;
}
// Create a queue
void CreatSqQueue(SqQueue& Q, int m) {
QElemType e;
for (int i = 1; i <= m; i++) {
cout << "Please enter no" << i << The value of each element:;
cin >> e;
PushSqQueue(Q, e); }}// Output queue
void OutPut(SqQueue Q) {
int i;
i = Q.front;
while(i ! = Q.rear) { cout << Q.elem[i] <<"";
i = (i + 1) % MAXSIZE;
}
cout << endl;
}
int main(a)
{
// Test the code
SqQueue Q;
QElemType e;
int m;
InitSqQueue(Q);
cout << "Please enter the length of queue:";
cin >> m;
CreatSqQueue(Q, m);
OutPut(Q);
cout << "The queue length is:" << QueueLength(Q) << endl;
cout << "Please enter team element:";
cin >> e;
PushSqQueue(Q, e);
cout << "Queue elements are:";
OutPut(Q);
cout << "The queue length is:" << QueueLength(Q) << endl;
PopSqQueue(Q, e);
cout << "The team exit elements are:" << e << endl;
cout << "Queue elements are:";
OutPut(Q);
cout << "The queue length is:" << QueueLength(Q) << endl;
return 0;
}
Copy the code
Please enter the length of the queue: 3 Please enter the value of the first element: 1 Please enter the value of the second element: 2 Please enter the value of the third element: 3 1 2 3 The length of the queue is: 3 Please enter the queue element: 4 The queue element is: 1 2 3 4 The length of the queue element is: 4 The queue element is: 1 2 3 4 The length of the queue element is: 4 The queue element is: 1 The queue elements are: 2 3 4 The length of the queue is: 3Copy the code
Finally, welcome to pay attention to my personal wechat public account “Little Ape Ruochen”, get more IT technology, dry goods knowledge, hot news