Queue-Data structure C + + implementation

Source: Internet
Author: User

Reference Link: Data structure adventure-Queue Chapter

Data structure is too important, do not learn is not able to develop software.

Start from scratch and learn the data structure.

Model of the queue queue

Imagine the real life of the queue, queued first in first out, not allowed to queue, team head out first, the end of the team into.

How to implement a queue programmatically

Ring queue, array implementation, static, pre-determined queue capacity, artificial redundancy, recycling array resources.

Normal queue, linked list implementation, dynamic, a bit wasteful, because insert delete only in the queue tail.

Basic elements of a queue

To manipulate a queue, you must have an array (pointer), queue length, capacity, and then manipulate the queue with the queue header and the tail of the queue.

What does the queue header and queue tail mean?

Basic operations for queues

Create queue, destroy queue, empty queue, queue empty, queue full, queue Length; element queued, element out, traversal queue

Implementation of basic operations

Create queue: constructor implementation, function parameters are queue capacity, this step needs to complete the task: to form an array entity, that is, allocating memory, some of the basic elements of the queue initialization.

Destroy queue: destructor implementation, free memory, pointer empty.

Empty queue: Pointer not moving, basic element initialization

Queue empty: Judging by length

Queue full: Judging by length and capacity

Queue Length: Direct return

Elements Queued : To determine whether to insert, can insert, update the queue basic information (tricks)

element out: to determine if something can be deleted, update the queue basic information

Traverse Queue : Loop, from start to finish, traverse (output)

Most of the operations are for the basic elements of the queue and will take advantage of and change it.

Writing a large data structure is difficult, it is easy to have some small errors, if you can not control these small errors, and so the program is big, it is very difficult to find the wrong and debugging.

C + + Implementation steps
    • First, go through the above process and be aware of it.
    • Write out the interface of the queue (it is recommended to write in a separate document so that it can be consulted at any time), not the specific implementation;
    • Implementation, core functions, create, INSERT, delete, traverse display, Debug success
    • Complement other core features

Code to write well, is really too difficult, first of all to the right name, followed by the size of the write can not be mistaken, in the use of a one-to-one correspondence.

The return value of the function, the function name, the parameter, and so on, are not allowed to have errors. (It's hard to write it all at once and slowly fix it when implemented)

First hand knocks the code experience: the queue of code is really very small, interface more than 20 lines, to achieve more than 80 lines, testing more than 20 lines, it is not much, but it is still difficult to write, and before knocking on a single example of the book, the feeling is not difficult; too many queues, basic 5 elements, too many operations, 9 basic I'm not too familiar with the syntax of C + +, some lag, too much, too miscellaneous, and the name is too long to dazzle me.

Standard C + + code
#pragmaOnce//MyQueue.h//Ring Queue C + + implementationclassmyqueue{ Public: Myqueue (intqueuecapacity);//Initqueue (&q) Create queue    Virtual~myqueue ();//Destroyqueue (&q) Destroy queue    voidClearqueue ();//clearqueue (&q) empty queue    BOOLQueueempty ()Const;//queueempty (&q) empty queue    intQueuelength ()Const;//queuelength (Q) Queue Length    BOOLQueuefull ()Const; BOOLEnQueue (intElement);//EnQueue (&q, Element) new element in the queue    BOOLDeQueue (int&element);//DeQueue (&q, &element) first element out of the team    voidQueuetraverse ();//Queuetraverse (Q, visit ()) Traversal queuePrivate:    int*m_pqueue;//Array of array of queues    intM_iqueuelen;//number of queue elements    intm_iqueuecapacity;//Queue Array Capacity    intM_ihead; intM_itail;};
//MyQueue.cpp#include"MyQueue.h"#include<iostream>using namespacestd; Myqueue::myqueue (intqueuecapacity) {m_iqueuecapacity=queuecapacity; M_ihead=0; M_itail=0; M_iqueuelen=0; M_pqueue=New int[m_iqueuecapacity];} Myqueue::~Myqueue () {Delete[]m_pqueue; M_pqueue=nullptr;}voidMyqueue::clearqueue () {M_ihead=0; M_itail=0; M_iqueuelen=0;}BOOLMyqueue::queueempty ()Const{    if(M_iqueuelen = =0)        return true; Else        return false;}BOOLMyqueue::queuefull ()Const{    if(M_iqueuelen = =m_iqueuecapacity)return true; Else        return false;}intMyqueue::queuelength ()Const{    returnM_iqueuelen;}BOOLMyqueue::enqueue (intElement) {    if(Queuefull ())return false; Else{M_pqueue[m_itail]=element; M_itail++; M_itail= m_itail%m_iqueuecapacity; M_iqueuelen++; return true; }}BOOLMyqueue::D Equeue (int&Element) {    if(Queueempty ())return false; Else{element=M_pqueue[m_ihead]; M_ihead++; M_ihead= m_ihead%m_iqueuecapacity; M_iqueuelen--; return true; }}voidMyqueue::queuetraverse () { for(inti = M_ihead; I < M_iqueuelen + M_ihead; i++) {cout<< M_pqueue[i%m_iqueuecapacity] <<Endl; }}
//Demo.cpp#include <iostream>#include"MyQueue.h"using namespacestd;intMain () {Myqueue*p =NewMyqueue (4); P->enqueue (1); P->enqueue (0); P->enqueue ( One); P-Queuetraverse (); P-Clearqueue (); cout<<"After clear:"<<Endl; P-Queuetraverse (); if(p->queueempty ()) cout<<"Empty Queue"<<Endl; Elsecout<<"Not Empty"<<Endl; P->enqueue (5); P->enqueue ( the); P->enqueue ( -); cout<< p->queuelength () <<Endl; inttmp; P-DeQueue (TMP); cout<<"tmp is"<< tmp <<Endl; cout<< p->queuelength () <<Endl; return 0;}

Queue-Data structure C + + implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.