first, queue and loop queue
1. Queues(1) A queue is a linear table that allows an insert operation at one end only, while a delete operation at the other end . A queue is a linear table, called FIFO, that is an FIFO (Fiirst in First out). One end of the allowed insert is called the tail of the queue, and the one end that allows deletion is called the team header. from the definition of the queue, the queue operation, in fact, is to append an element at the end of the team, do not need to move any elements, so the time complexity of O (1). The deletion of a queue, unlike the stack, is that the queue element's dequeue is in the team header, where the small label is 0, to remove an element, you need to move all the elements of the queue, so the event complexity is O (n). (2) front/rear pointer: In order to avoid when there is only one element, the team tail and the team head coincident make the processing become troublesome, all introduce two pointers, thefront pointer points to the team head element, the rear pointer points to the next position of the team tail element , This way, when front equals rear, the queue is empty (an empty queue) instead of a queue with one element left.
2. Abstract data types for queuesADT QueueDataThe same linear table. Elements have the same type, and adjacent elements have precursors and successors. OperationInitqueue (*Q): Initialize operation, establish an empty queue Q.Destoryqueue (*q): If queue Q is present, destroy it. Clearqueue (*Q): Clears queue QGetHead (q,*e): If queue Q exists and is not empty, return the team header element of queue Q with E. EnQueue (*q,e): If queue Q exists and is not empty, inserting a new element e into queue q is called the tail element. DeQueue (*q,*e): Delete Queue Q Squadron header element and return its value with Equeuelength (q): Returns the number of elements in the queue QEndadt
3. Loop queue
(1) Definition: The sequential storage structure that ends up in a queue is called a circular queue to resolve a false overflow problem.
(2) Team full condition :As a general rule, when front=rear, the queue may be empty and the queue can be full. So we assume that the queue is empty when front==rear, and that the array has a free space when the queue is full. Because rear may be larger than front, it can be smaller than front. We define that when the queue satisfies the condition "(rear+1)%queuesize==front", we think the queue is full (Queuesize is the maximum storage capacity of the queue).
(3) Queue Length formulaWhen Rear>front, the length of the queue is Rear-front, and when Rear<front, the length of the queue is (Queuesize-front) + (0+rear), where font, rear, The queuesize are subscript for an array. The derived calculation queue formula:(rear-front+queuesize)%queuesize
(4) sequential storage structure for cyclic queuestypedef int QELEMTYPEtypedef struct{Qelemtype data[maxsize];int front; Team Head Pointerint rear; Tail pointer, if the queue is not empty, point to the next position of the tail element of the team}sqqueue;(5) related operation of cyclic queueA. Initializing a loop queue QStatus initqueue (sqqueue *q){q->front=0;q->rear=0;return OK;}B. Calculating the length of a circular queue/ * Returns the number of elements in Q, which is the current length of the queue * /int queuelength (sqqueue Q){return (q.rear-q.front+maxsize)%maxsize;}C. Circular queue insert Operationimplementation: If the queue is not full, insert element e as the new team tail elementStatus EnQueue (sqqueue *q,qelemtype e){if ((q->rear+1)%maxsize = = Q->front)//Judgment stack full ((rear+1)%queuesize==front)return ERROR;q->data[q->rear]=e;q->rear= (q->rear+1)%maxsize; The rear pointer moves backward one bit, and if you go to the end of the array headreturn OK;}D. Circular queue deletion operationsimplementation: If the queue is not empty, then delete the Q Squadron head element. Return its value with EStatus EnQueue (sqqueue *q,qelemtype *e){if (q->rear==q->front)return ERROR; Queue is empty condition: Rear=front*e=q->data[q->front]; Assign a team header element to Eq->front= (q->front+1)%maxsize; The front pointer moves backward one position, and if you go to the end of the array headreturn OK;}Summary: The sequential storage, if not the loop queue, the algorithm's time performance is not high, but the loop queue is also faced with the problem of an array of possible overflow, so we have the next section of the chain-based storage structure.
second, the chain storage structure of the queue
1. Chain Queue: The chain storage structure of a queue, in fact, is a single linked list of linear tables, but it can only be the end of the head out , also known as the chain queue. For ease of operation, the team head pointer points to the head node of the chain queue and the tail pointer points to the terminal node . The queue is empty when the team head pointer front and the tail pointer rear all point to the head node.
2. Chain Queue structuretypedef int QELEMTYPE/ * Node structure * /typedef struct QNODE{Qelemtype data; Data fieldsstruct Qnode *next; Pointer field}qnode,*queueptr;/ * List structure of the queue * /typedef struct{ queueptr front,rear; Team head, Team tail hands}linkqueue;3. Queued operation of the chain queuealgorithm: Essentially inserts a node at the end of a chain, and the tail pointer points to a new nodeA.Open a space for the new node s and determine if the storage allocation is successful;B. Save the inserted element to the data field of the new node s and initialize the pointer field of S;c.assigning the new node s of the owning element e to the successor of the original queue node;d.set the current s to the tail node of the team, rear pointing s
implementation: Insert element e As Q's new team tail element
Status EnQueue (Linkqueue *q,qelemtype e) { queueptr s= (queueptr) malloc (sizeof (Qnode)); Open a Space if (!s) //Storage allocation failure exit (OVERFLOW) for the new node s; s->data=e; Stores element E in the data field of the new node S s->next=null;//initializes the pointer field of the new node q->rear->next=s; Assigning the new node s with Element E to the successor q->rear=s; of the original queue node Set the current s as the tail node of the team, rear pointing to S}
4. Delete operations for chain queuesalgorithm: The essence is the successor node of the first node out of the team, the successor of the head node is changed to the node behind him. If the list has only one element left in addition to the head node, the rear point will be pointed to the head node. A. Define aqueueptr node p, used to temporarily delete the node q->front-next;B. Assign a value to E to delete the data field of the node;c. The successor node that will delete the node (P->next) assigns a value to the successor of the head node (Q->front->next)d. DeterminationIf the team head is the end of the team, then delete the rear point to the head node, and finally release the node p to delete.
implementation: If the queue is not empty, delete the team head element of Q, return its value with E, and return OK, otherwise return error
Status EnQueue (Linkqueue *q,qelemtype e) { queueptr p; if (q->front==q->rear) return ERROR; Queue is empty p=q->front->next; The successor of the head node pointed to by the team head pointer (the node to be deleted) is staged to P *e=p->data; . Assign the data field data to delete the node to e q->front->next=p->next;. Assign the successor P->next of the tuple head node to the successor if of the Team head node (q->rear==p) //If the team head is the end of the team, then the rear points to the head node after deletion q-rear=q->front; Free (p); return OK;}
three, cyclic queue and chain queue performance analysis1.The complexity of the basic operation time of cyclic queue and chain queue is O (1);2. Circular queue in advance to apply for a good space, the use of the period is not released; chain queue, no need to request space in advance but there will be some event overhead for each application and release node;3. The loop queue must have a fixed length, there may be a waste of space; The chain queue requires a pointer field that generates some overhead, so it is recommended to use a circular queue if the maximum queue length can be determined.
09. Cyclic Queue and Chain queue