c++实现静态队列

一、队列知识点

1.定义:

        队列是一种操作受限的线性表,只能在队尾插入元素,在对头删除元素。

2.特点:

        先进先出

3.术语:

        队头(front)、队尾(rear) 、空队列、队头元素 、队尾元素

3.分类:

        ① 动态队列:由链表实现

        ② 静态队列:由数组实现

4.应用:

        所有和时间有关的操作都有队列的影子

5.伪算法(静态队列):

Ⅰ.出队

        由于动态队列实现相对简单,故不作过多记录。在用数组实现队列时,通常是循环结构,否则在出队之后会引起数组容量变小,如下图所示:

 所以通常将静态数组设置为循环结构,如下图所示:

 每次出队,将出队的值用一个变量临时保存,并将队头(front)指向下一个元素:

        int t = arr[front]

        front = (front+1)%length

变成循环队列最主要的操作就是在指向下一个元素时都进行加一取余,十分巧妙!

Ⅱ.入队

首先讲需要入队的值附在队尾(rear)指向的位置,然后讲队尾后移一位使得队尾始终指向最后一个元素的下一个位置。

arr[rear] = val5;

rear = (rear+1)%length;

队尾(rear)在更新位置时同样使用加一取余的操作实现循环结构。

Ⅲ.判空

        只要队头(front)=队尾(rear)即为空,初始化时就是空的。

Ⅳ.判满

        在循环结构里,空和满其实是一种状态,所以这里要用其他方法。

        两种方式:①多增加一个标识参数

                          ②少用一个元素:如果队尾(rear)后移一个位置就是队头(front),则满足

 if((rear+1)%length == front) return true; 

Ⅴ.遍历

        从队头开始,只要不等于队尾,就加一访问数组元素

二、c++实现静态队列(全部代码在最后):

1.首先构建队列的类,定义属性:数组(arr),队头(front),队尾(rear),容量(cam),以及len(已有元素个数),val(临时保存出队的值)

class queue {
public:
	queue() {
		cout << "请输入静态队列大小:";
		cin >> this->cam;
		this->arr = new int[this->cam];
		this->front = 0;
		this->rear = 0;
		this->len = 0;
	};

	~queue() {
		delete [] this->arr;
	};

	int len; //用于保存元素个数
	int val; //用于临时保存出队的元素

private:
	int* arr;
	int front;
	int rear;
	int cam;
};

2.判空(empty)和判满(full)

	bool empty() {
		if (this->front == this->rear) return true;
		else return false;

	}

	bool full() {
		if ((this->rear + 1) % cam == front) return true;
		else return false;


	}

3.入队

	void push(int val) {
		if (this->full()) {
			cout << "队列已满,入队失败" << endl;
		}
		else {
			this->arr[this->rear] = val;
			this->rear = (this->rear + 1) % cam;
			this->len++;
		}
	}

4.出队

	void pop() {
		if (this->empty()) {
			cout << "队列为空,出队失败" << endl;
		}
		else {
			this->val = this->arr[this->front];
			this->front = (this->front + 1) % cam;
			this->len--;

		}
	}

5.遍历

	void travel() {
		if (this->empty()) {
			cout << "队列为空" << endl;
		}
		else {
			cout << "队列元素为: ";
			for (int i = this->front; i!= this->rear; i = (i + 1) % cam) {
				cout << this->arr[i] << " ";
			}
			cout << endl;
		}
	}

6.测试

void test() {
	queue queue1;
	queue1.push(1);
	queue1.push(2);
	queue1.push(3);
	queue1.push(4);
	queue1.push(5);
	queue1.travel();
	queue1.pop();
	cout << "出队的值为" << queue1.val << endl;
	queue1.travel();

	return;
}

7.效果

 

三、全部代码

#include<iostream>
#include<string>
using namespace std;


class queue {
public:
	queue() {
		cout << "请输入静态队列大小:";
		cin >> this->cam;
		this->arr = new int[this->cam];
		this->front = 0;
		this->rear = 0;
		this->len = 0;
	};

	~queue() {
		delete [] this->arr;
	};

	bool empty() {
		if (this->front == this->rear) return true;
		else return false;

	}

	bool full() {
		if ((this->rear + 1) % cam == front) return true;
		else return false;


	}

	void push(int val) {
		if (this->full()) {
			cout << "队列已满,入队失败" << endl;
		}
		else {
			this->arr[this->rear] = val;
			this->rear = (this->rear + 1) % cam;
			this->len++;
		}
	}

	void pop() {
		if (this->empty()) {
			cout << "队列为空,出队失败" << endl;
		}
		else {
			this->val = this->arr[this->front];
			this->front = (this->front + 1) % cam;
			this->len--;

		}
	}

	void travel() {
		if (this->empty()) {
			cout << "队列为空" << endl;
		}
		else {
			cout << "队列元素为: ";
			for (int i = this->front; i!= this->rear; i = (i + 1) % cam) {
				cout << this->arr[i] << " ";
			}
			cout << endl;
		}
	}

	int len; //用于保存元素个数
	int val; //用于临时保存出队的元素

private:
	int* arr;
	int front;
	int rear;
	int cam;
};



void test() {
	queue queue1;
	queue1.push(1);
	queue1.push(2);
	queue1.push(3);
	queue1.push(4);
	queue1.push(5);
	queue1.travel();
	queue1.pop();
	cout << "出队的值为" << queue1.val << endl;
	queue1.travel();

	return;
}


void main() {
	test();


	return;
}

猜你喜欢

转载自blog.csdn.net/qq_43575504/article/details/129837830
今日推荐