There are two types of linear tables with limited access locations: Stack and queue. There are two ways to implement Queues: sequential and chained.
Cyclic queues are implemented in order. Two variables, front and rear, are used to record the header and tail of the queue. The implementation code is as follows:
Class cqueueimpl {<br/> Public: <br/> cqueueimpl (INT size = 10); <br/> ~ Cqueueimpl (); <br/> bool isempty (); <br/> bool isfull (); <br/> void enqueue (INT value ); <br/> int dequeue (); <br/> PRIVATE: <br/> int m_irear; <br/> int m_ifront; <br/> int * m_pelement; <br/> int m_imaxsize; <br/> };Cqueueimpl: cqueueimpl (INT size) {<br/> m_pelement = new int [size]; <br/> m_imaxsize = size; <br/> m_irear = 0; <br/> m_ifront = 0; <br/>}< br/> cqueueimpl ::~ Cqueueimpl () {<br/> Delete m_pelement; <br/>}< br/> bool cqueueimpl: isempty () {<br/> return m_irear = m_ifront; <br/>}< br/> bool cqueueimpl: isfull () {<br/> return (m_irear + 1) % m_imaxsize = m_ifront; <br/>}< br/> void cqueueimpl: enqueue (INT value) {<br/> assert (! Isfull (); <br/> m_irear = (m_irear + 1) % m_imaxsize; <br/> m_pelement [m_irear] = value; <br/>}< br/> int cqueueimpl: dequeue () {<br/> assert (! Isempty (); <br/> m_ifront = (m_ifront + 1) % m_imaxsize; <br/> return m_pelement [m_ifront]; <br/>}
In the implementation of cyclic queues, it is important to note that the queue is null and full. The storage location is an array of maxsize size. If all units are used to store data, the full and empty values are, the rear values are equal to the front, and a variable is required to determine whether it is full or empty. The general processing method is to empty a unit. When rear = front, it indicates that the queue is empty; When (Rear + 1) % maxsize = front, it indicates that the queue is full. For tail inserts, each insert operation is the next position of the rear. For the header to fetch data, each insert operation is the next position of the front.
The queue chain implementation is similar to the stack implementation. The Code is as follows:
Class cqueuelinkimpl; <br/> class cqueuenode {<br/> friend class cqueuelinkimpl; <br/> PRIVATE: <br/> cqueuenode * m_plink; <br/> int m_ivalue; <br/> cqueuenode (INT ivalue, cqueuenode * pnode) {m_ivalue = ivalue; m_plink = pnode;} <br/> ~ Cqueuenode () {}< br/>}; <br/> class cqueuelinkimpl {<br/> Public: <br/> cqueuelinkimpl (); <br/> ~ Cqueuelinkimpl (); <br/> bool isempty (); <br/> void enqueue (INT value); <br/> int dequeue (); <br/> PRIVATE: <br/> cqueuenode * m_prear; <br/> cqueuenode * m_pfront; <br/> };Cqueuelinkimpl: cqueuelinkimpl () {<br/> m_prear = NULL; <br/> m_pfront = NULL; <br/>}< br/> cqueuelinkimpl ::~ Cqueuelinkimpl () {<br/> cqueuenode * pnode; <br/> while (m_pfront! = NULL) {<br/> pnode = m_pfront; <br/> m_pfront = m_pfront-> m_plink; <br/> Delete pnode; <br/>}< br/> bool cqueuelinkimpl: isempty () {<br/> return m_pfront = NULL; <br/>}< br/> void cqueuelinkimpl: enqueue (INT value) {<br/> cqueuenode * pnode = new cqueuenode (value, null ); <br/> If (m_pfront = NULL) {<br/> m_pfront = pnode; <br/> m_prear = pnode; <br/>}else {<br/> m_prear-> m_plink = pnode; <br/> m_prear = Pnode; <br/>}< br/> int cqueuelinkimpl: dequeue () {<br/> assert (! Isempty (); <br/> cqueuenode * pnode = m_pfront; <br/> int value = pnode-> m_ivalue; <br/> m_pfront = pnode-> m_plink; <br/> Delete pnode; <br/> return value; <br/>}
In the queue chain implementation, pay attention to the following points:
1. In the chain implementation, pfront and prear pointers to cqueuenode are used to record the header and tail of the queue.
2. initialize front when inserting the first node;
3. Whether the queue is empty is determined by pfront.
4. When a queue resource is released, it is determined and started by pfront.
5. prear is useful only when the node is inserted to the end.
There are many queue applications. For example, you need to get a number for the bank to handle the business; for example, call 10086, and wait when the business is busy. For example, there are 10086 seats in shenzhouxing, dynamic zone, and global customer service. Our customer service representatives provide high-quality services to global customers, and our customers with low quality services will be inspired. When there are too many calls, the access services of global customers are prioritized. When the customer service in the dynamic zone is busy and the global customer service is idle, we need to adopt the guidance method. This requires the weight calculated by priority and various factors. And can be taken from the queue based on the weight.
When a base station is busy, Priority Queues are also involved to ensure that global access to customers is given priority.