OJ brushing record: the storage structure and operation of the queue

Sequence storage structure and operation of the queueQuestion number: 460

Question requirements:
Please define a sequential queue, which can perform operations such as "entry", "dequeue", "empty queue", "get the first element of the queue" and so on. The above operations can be performed by entering some commands on the keyboard. In this question, the elements of the queue are letters, and the maximum number of elements in the queue is 100.
Enter description

Enter each command, and their corresponding format is as follows:

Enqueue: E a, a represents the elements enqueued, where E and the elements are separated by spaces.

Empty the queue: C

Get the head element: when the input command is D, output the element value of the deque;

When the input command is G, output the value of the first element of the current team;

If there is no element to dequeue or desirable,

Output description

When the input command is D, output the value of the dequeued element;

When the input command is G, output the value of the first element of the current team;

If there is no element to be dequeued or desirable, please output None;

Input sample

E a
G
C
E b
D
D
Q

Sample output

a
b
None

Problem-solving idea: It
is necessary to take the modulus of the moved array subscript (head_ or rear_) during each operation to realize the loop of the beginning and end of the array to prevent false overflow.

Customs clearance code:

#include <iostream>

#define MaxSize 100

using namespace std;

class Queue {
    
    
	public:
		Queue();
		
	public:
		void Push(char ch);
		char Pop();
		void Clear();
		char getHead();
		
	private:
		char arr_[MaxSize];
		int head_;
		int rear_;
};

Queue::Queue() {
    
    
	head_ = 0;
	rear_ = 0;
}

void Queue::Push(char ch) {
    
    
	if ((rear_ + 1) % MaxSize == head_) throw "上溢";
	
	arr_[rear_] = ch;
	rear_ = (rear_ + 1) % MaxSize;
}

char Queue::Pop() {
    
    
	if (rear_ == head_) throw "None";
	
	char ch = arr_[head_]; 
	
	head_ = (head_ + 1) % MaxSize;
	
	return ch;
}

void Queue::Clear() {
    
    
	head_ = 0;
	rear_ = 0;
}

char Queue::getHead() {
    
    
	if (rear_ == head_) throw "None";
	
	return arr_[head_];
}

int main() {
    
    
	Queue q;	
	char val, key;

	while (cin >> key) {
    
    
		if (key == 'Q') break;
		
		try {
    
    
			switch (key) {
    
    
				case 'E':
					cin >> val;
					q.Push(val);
					break;

				case 'C':
					q.Clear();
					break;

				case 'G':
					cout << q.getHead() << endl;
					break;

				case 'D':
					cout << q.Pop() << endl;
					break;
			}
		} catch (const char* str) {
    
    
			cout << str << endl;
		}
	}
	
	return 0;
}

The chained storage structure and operation of the queueQuestion number: 115

Subject requirements:
Same as above.

Problem-solving ideas:
water.

Customs clearance code:

#include <iostream>

using namespace std;

struct Node {
    
    
	char _val;
	Node* _next;
	Node(int val):_val(val), _next(NULL) {
    
    }
};

class Queue {
    
    
	public:
		Queue();
		~Queue();

	public:
		void Push(char ch);
		char Pop();
		void Clear();
		char getHead();

	private:
		Node* head_;
		Node* rear_;
};

Queue::Queue() {
    
    
	head_ = new Node(-1);
	rear_ = head_;
}

Queue::~Queue() {
    
    
	Node* afterHead = NULL;

	for (Node* p = head_; p != NULL; p = afterHead) {
    
    
		afterHead = p->_next;
		delete p;
	}
}

void Queue::Push(char ch) {
    
    
	Node* node = new Node(ch);

	rear_->_next = node;
	rear_ = node;
}

char Queue::Pop() {
    
    
	if (head_->_next == NULL) throw "None";
	
	Node* popedNode = head_->_next;

	char res = popedNode->_val;
	head_->_next = popedNode->_next;

	delete popedNode;

	return res;
}

void Queue::Clear() {
    
    
	Node* afterHead = NULL;

	for (Node* p = head_->_next; p != NULL; p = afterHead) {
    
    
		afterHead = p->_next;
		delete p;
	}
	
	head_->_next = NULL;
	rear_ = head_;
}

char Queue::getHead() {
    
    
	if (head_->_next == NULL) throw "None";
	
	return head_->_next->_val;
}

int main() {
    
    
	Queue q;
	char val, key;

	while (cin >> key) {
    
    
		if (key == 'Q') break;

		try {
    
    
			switch (key) {
    
    
				case 'E':
					cin >> val;
					q.Push(val);
					break;

				case 'C':
					q.Clear();
					break;

				case 'G':
					cout << q.getHead() << endl;
					break;

				case 'D':
					cout << q.Pop() << endl;
					break;
			}
		} catch (const char* str) {
    
    
			cout << str << endl;
		}
	}

	return 0;
}

complete.

Guess you like

Origin blog.csdn.net/weixin_45711556/article/details/108813038