队列
#include <iostream>
using std::cout;
using std::endl;
template <typename Type> class Queue {
private:
Type *data;
int head, tail, length;
public:
Queue(int length_input) {
data = new Type[length_input];
length = length_input;
head = 0;
tail = -1;
}
~Queue() {
delete[] data;
}
bool push(Type element) {
if (tail + 1 >= length) {
return false;
}
tail++;
data[tail] = element;
return true;
}
void output() {
for (int i = head; i <= tail; i++) {
cout << data[i] << " ";
}
cout << endl;
}
// 请在下面实现队首元素输出方法 front
Type front(){
return data[head];
}
// 请在下面实现删除队首元素方法 pop
void pop(){
head++;
}
// 请在下面实现判断队列是否为空的方法 empty
bool empty(){
return head > tail;
}
};
int main() {
Queue<int> queue(100);
for (int i = 1; i <= 10; i++) {
queue.push(i);
}
queue.output();
if (!queue.empty()){
cout << queue.front() << endl;
queue.pop();
}
queue.output();
return 0;
}
循环队列
由于假溢出问题,循环队列会更好。循环队列不能再使用tail<head或者其他tail与head的相对位置关系来判断队列溢出,否则空队列也会被判为溢出。这里使用队列长度计数变量count来判断队列溢出。
#include <iostream>
using std::cout;
using std::endl;
template <typename Type> class Queue {
private:
Type *data;
int head, tail, length, count;
public:
Queue(int length_input) {
data = new Type[length_input];
length = length_input;
head = 0;
tail = -1;
count = 0;
}
~Queue() {
delete[] data;
}
bool push(Type element) {
if (count >= length) {
return false;
}
tail = (tail + 1) % length;
data[tail] = element;
count++;
return true;
}
void output() {
int i = head;
do {
cout << data[i] << " ";
i = (i + 1) % length;
} while(i != (tail + 1) % length);
cout << endl;
}
Type front() {
return data[head];
}
void pop() {
head = (head + 1) % length;
count --;
}
bool empty() {
return count == 0;
}
};
int main() {
Queue<int> queue(100);
for (int i = 1; i <= 10; i++) {
queue.push(i);
}
queue.output();
if (!queue.empty()) {
cout << queue.front() << endl;
queue.pop();
}
queue.output();
return 0;
}
NEW!!
普通队列
//由于循环队列很难判定队列满,若是front=rear或front>rear,则和队空情况一致
//因此引入计数变量,计算已存入队列的元素个数
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct Queue{
int *data;
int head, tail, length;
}Queue;
Queue *init(int n){
Queue *q = (Queue *)malloc(sizeof(Queue));
q->data = (int *)malloc(sizeof(int) * n);
q->length = n;
q->head = q->tail = 0;//用法很独特!
return q;
}
int front(Queue *q){
return q->data[q->head];
}
int tail(Queue *q){
return q->data[q->tail];
}
void clear(Queue *q){
if (q == NULL) return;
free(q->data);
free(q);
return;
}
//tail指向待插入元素的位置,即当前元素的后一个位置
//判断队空
int empty(Queue *q){
return q->head == q->tail;
}
//入队
int push(Queue *q, int val){
if (q == NULL) return 0;
if (q->tail == q->length){
return 0;//队满
}
q->data[q->tail++] = val;
return 1;
}
//出队
int pop(Queue *q){
if (q == NULL) return 0;
if (empty(q)) return 0;
q->head ++;
return 1;
}
//打印元素
void output(Queue *q){
printf("Queue : [");
for (int i = q->head; i < q->tail; i++){
printf("%d ", q->data[i]);
}
printf("]\n");
return;
}
int main(){
srand(time(0));
#define max_op 20
Queue *q = init(max_op);
for (int i = 0; i < max_op; i++){
int val = rand() % 100;
push(q, val);
output(q);
}
#undef max_op
return 0;
}
循环队列
//由于循环队列很难判定队列满,若是front=rear或front>rear,则和队空情况一致
//因此引入计数变量,计算已存入队列的元素个数
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct Queue{
int *data;
int head, tail, length, cnt;
}Queue;
Queue *init(int n){
Queue *q = (Queue *)malloc(sizeof(Queue));
q->data = (int *)malloc(sizeof(int) * n);
q->length = n;
q->head = q->tail = q->cnt = 0;//用法很独特!
return q;
}
int front(Queue *q){
return q->data[q->head];
}
int tail(Queue *q){
return q->data[q->tail];
}
void clear(Queue *q){
if (q == NULL) return;
free(q->data);
free(q);
return;
}
//tail指向待插入元素的位置,即当前元素的后一个位置
//判断队空
int empty(Queue *q){
//return q->head == q->tail;
return q->cnt == 0;
}
//入队
int push(Queue *q, int val){
if (q == NULL) return 0;
//if (q->tail == q->length){
if (q->cnt == q->length) return 0;//队满
//expand
q->data[q->tail++] = val;
if (q->tail == q->length) q->tail = 0;
q->cnt ++;
return 1;
}
//出队
int pop(Queue *q){
if (q == NULL) return 0;
if (empty(q)) return 0;
q->head ++;
if (q->head == q->length) q->head = 0;
q->cnt --;
return 1;
}
//int expand(Queue *q){}
//打印元素
void output(Queue *q){
printf("Queue : [");
for (int i = q->head, j = 0; j < q->cnt; i++, j++){
printf("%d ", q->data[i % q->length]);
}
printf("]\n");
return;
}
int main(){
srand(time(0));
#define max_op 20
Queue *q = init(max_op);
for (int i = 0; i < max_op; i++){
int val = rand() % 100;
push(q, val);
output(q);
}
#undef max_op
return 0;
}