The algorithm hasn’t moved, the grain and grass go first (Basic Part 3---Queue)

tags: algorithm

queue

Queue, also known as queue, is a linear table of FIFO, First-In-First-Out. In specific applications, it is usually implemented with linked lists or arrays. The queue only allows insert operations at the back end (called rear) and delete operations at the front end (called front).
The operation of the queue is similar to that of the stack. The only difference is that the queue only allows new data to be added in the backend.
-Source: Wikipedia

3.1 Definition of queue


element follows first-in-first-out (similar to the situation of queuing in supermarkets in life)
The operation of element satisfies: enter the queue from the end of the line, leave the queue from the head of the line

template<typename DataType>class Queue
{
public:
	Queue(int size)
	{
		maxSize = size;
		front = 0;
		rear = 0;
		count = 0;
		elements = new DataType[maxSize];
		//If space allocation fails, exit the program directly
		if(elements==NULL)
			exit(1);
	}
	~Queue()
	{
		delete [] elemenets;
	}
	bool insert(DataType data);
	DataType pop();
	bool isempty();
	bool isfull();
	DataType getF();
	DataType getR();
private:
	int count;
	int maxSize;
	int front;//The top position of the team
	int rear;//The tail position
	DataType *elements;
};

3.2 Basic operation

The basic operations of the queue include entering the team, leaving the team, judged empty, judged full, return to the head element, and return to the end element;

3.2.1 Enrollment operation

template<typename DataType>bool Queue<DataType>::insert(DataType data)
{
	if(count==maxSize)
		return false;
	elements[rear]=data;
	rear=(rear+1)%maxSize;//Circular queue, save space
	count++;
	return true;
}

3.2.2 Dequeue operation

template<typename DataType>DataType Queue<DataType>::pop()
{
	if(count==0)
		exit(1);
	DataType temp=elements[front];
	front=(front+1)%maxSize;
	count--;
	return temp;
}

3.2.3 Empty operation

template<typename DataType>bool Queue<DataType>::isempty()
{
	if(count==0)return true;
	return false;
}

3.2.4 Full operation

template<typename DataType>bool Queue<DataType>::isfull()
{
	if(count==maxSize)return true;
	return false;
}

3.2.5 Return to the top element

template<typename DataType>DataType Queue<DataType>::getF()
{
	if(count==0)
		exit(1);
	return elements[front];
}

3.2.6 Return to the tail element

template<typename DataType>DataType Queue<DataType>::getR()
{
	if(count==0)
		exit(1);
	return elements[rear];
}

Questions

Without considering the complexity of the program, how do you use the stack to implement queue operations?

answer:
The characteristic of the stack is first-in-last-out (FILO)
The queue is characterized by first in first out (FIFO)
The first-in-last-out feature of the stack has been changed to realize the first-in-first-out of elements
Set up two stacks, one is responsible for the enqueue operation, when you want to leave the queue, the elements in the stack of the enqueue operation will be popped out of the stack, and once into the second stack, then the second stack will be queued Popping, that is, after dequeuing, the elements in the second stack are returned to the first stack

Reference book "Interesting Algorithm (C++ Language Implementation)"

Intelligent Recommendation

Grain and Grass First-Android Folding Screen Development Technology Points Extra Chapter: Runtime Change Processing Principles

In the previous article, we mentioned how the Activity handles when the screen size changes. There are two types: Restart the APP to adapt to the screen change; Manually process data to avoid APP rest...

Chapter 2 Preparations for Using Bootstrap-The Soldiers and Horses Haven't Moved, The Food and Grass First Note 1

Overview: Do some preparations for learning Boostrap This chapter involves content: Download and customize Boostrap Bootstrap file structure Boostrap application analysis Develop the first Boostrap ex...

Java basic learning note first part of the Part 3

Note: Return can be used to end methods NexTline cannot be used after NEXTINT, because nextline will skip NextLine after NEXTINT. In order to solve this problem, you can add more next to nextint. Arra...

CUDA thread execution model analysis (2) the army has not moved the grain first ------ GPU revolution

CUDA Thread execution model analysis (2) the army did not move grain and grass first ------GPURevolution Preface: Today may be a relatively unsatisfactory day, from the first call in the morning to th...

3. Data structure and algorithm-stack and queue (part 2) (2021.02.07)

table of Contents 1. The application of stack in bracket matching 2. The application of stack in expression evaluation (part 1) 3. The application of stack in expression evaluation (below) 4. The appl...

More Recommendation

Go language learning - part (3) basic data type

1. The string in the Go language can only use double quotes "," and cannot use single quotes. (Other languages ​​can be) 2. Single quota package called characters, not string Then the charac...

Chapter 3 Circular Queue (Sequential Queue) Basic Algorithm Implementation

Like the bidirectional stack, it looks like this is dynamically allocated, but in fact it is static, and a fixed-size MAXQSIZE space is allocated. Because when the static queue is full, realloc needs ...

Python algorithm (basic)-queue (first in, first out, FIFO)

The queue is constructed as an ordered set of items added at the end of the line and removed from the head of the line. The queue maintains FIFO ordering properties. The queue operation is as follows....

GO algorithm-queue

Quest: advanced first, complexity in and out of time o (1), find O (n) Stack: Later One: Use a queue to implement a stack Please use only two queues to achieve one stack in the first -mounted (LIFO), ...

Translate Deep Learning and the Game of Go (5) Chapter 3: Realize your first Go AI (Part 2)

3.4 Create your first AI: the weakest AI imaginable After implementing the Go board and game state classes, you can build your first Go AI. This robot will be a very weak player, but it will lay the f...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top