Queue of data structures

Source: Internet
Author: User

Queue is also an operation constrained linear table, its operation limit is different from the stack, there are restrictions on both ends, the insertion can only be done at one end of the table (only not in), and the deletion can only be done at the other end of the table (only out), allowing the deletion of one end called the tail (rear), allowing the end of the Front)

, the operation principle of the queue is FIFO, so the queue is also known as the FIFO table (first

There are also six basic operations for the queue:

Empty team: Initqueue (Q)

Empty team: Clearqueue (Q)

Team Empty: Queueempty (Q)

Queue: EnQueue (q,x)

Team: DeQueue (Q)

Destroy queue: Destroyqueue (Q)

Take the team head element: GetHead (Q), different with the out team, the team head element is still retained.

Take the tail element: Getback (Q)

Traverse the entire queue: Queuetraverse (Q)

Queues also have both sequential and chained storage structures, which are called sequential queues, which are chain teams.

#include <stdlib.h>#include<stdio.h>#include<iostream>using namespaceStd;typedefintElemtype;typedefstructqnode{elemtype data; structQnode *Next;} Qnode,*Queueptr;typedefstruct{queueptr front; QUEUEPTR Rear;} Linkqueue;voidInitqueue (Linkqueue *p) {Q->front=q->rear= (queueptr) malloc (sizeof(Qnode)); if(! (Q->front)) Exit0); Q->front->next=NULL;}voidDestroyqueue (Linkqueue *2) {     while(q->front) {Q->rear=q->front->Next; Free (Q-front); Q->front=q->Rear; }}voidClearqueue (Linkqueue *Q)    {queueptr p; P=q->front->Next;  while(p) {Q->front->next=p->Next;    Free (p); } Q->rear=q->Front;}intqueueempty (Linkqueue Q) {if(q.front==q.rear)return 1; Else      return 0;}intqueuelength (Linkqueue Q) {queueptr p; intn=0; P=Q.front;  while(p!=q.rear) {n++; P=p->Next; }    returnN;} Elemtype GetHead (Linkqueue Q) {if(q.front!=q.rear)returnQ.front->next->data;} Elemtype Getback (Linkqueue Q) {if(q.front!=q.rear)returnQ.rear->data;}voidEnQueue (Linkqueue *Q, Elemtype e)    {queueptr p; P= (queueptr) malloc (sizeof(Qnode)); if(!p) Exit (0); P->data=e; P->next=NULL; Q->rear->next=p; Q->rear=p;}voidDeQueue (Linkqueue *q, Elemtype *e)    {queueptr p; if(q->front!=q->rear) {P=q->front->Next; *e=p->data; Q->front->next=p->Next; if(q->rear==p) Q->rear=q->Front;    Free (p); }}voidQueuetraverse (Linkqueue Q) {queueptr p; printf ("\nqueue:"); P=q.front->Next;  while(p) {printf ("%d\t",p->data); P=p->Next; }}intMain () {Linkqueue Q; inti;    Elemtype x; Initqueue (&p);  for(i=2; i<=5; i++) EnQueue (&q,i); printf ("len:%d\n", Queuelength (Q)); cout<<getback (Q) <<Endl; cout<<gethead (Q) <<Endl;//While (! Queueempty (Q))//    {//DeQueue (&q,&x);//printf ("%d", x);//    }}

Application of Queues
"For example1"Bank queue
"For example2"Simulates the printer buffer.
When the host outputs data to the printer, there is an issue where the host speed does not match the printer's print speed. The host will then stop to wait for the printer. Obviously, this will reduce the efficiency of the host's use. To this end people envision a way: To set up a print data buffer for the printer, when the host needs to print data, the data is written to this buffer, write full after the host to do other things, and the printer from the buffer in accordance with the FIFO principle of reading data and printing, In this way, the correctness of the printed data is ensured, and the efficiency of the use of the host is improved. This shows that the printer buffer is actually a queue structure.
"For example3】CpuCtss
In a computer system with multiple terminals, multiple users are required to use theCpuRun their own applications, which are presented to the operating system using their respective terminalsCpuRequest, the operating system usually according to each request in the order of time, they are queued, each time the CPU is assigned to the current team first request user, the user's application is put into operation, when the program is finished running or used up the specified time slice, the operating system will then CPU assign a new team to the first request user, so that each user's request can be met, and the CPU will work properly.

Queue of data structures

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.