Definition and implementation of queues (C language Implementation)

Source: Internet
Author: User

As a child. When we do morning exercises or military training, are lined up in a row, symbolizing. If you are late, you can only stand on the last one. At the exit. Is the first one to go first. This is the embryonic form of the queue.

Definition of a queue

A queue is a special kind of linear table

Queues operate only on both ends of a linear table

Team Head (Front) : Remove one end of the data element

End of Team (Rear) : Insert one end of a data element

Different queues are intended to operate in the middle!

A queue is essentially a special operation form of a linear table that is deleted at the head. Get, join at the tail.

Basically similar to the stack, change the hot not dressing. Detailed to be able to participate in

implementation and operation of the stack (C language Implementation)

As with stacks, queues have the same linear and chained structure. Respectively for example the following:

======================================================================================================

Linear form of the queue:

The linear form of the queue is essentially a sequential linear table, where the same code is reused, the code on the sequential linear table is not affixed, and the implementation and operation of the sequential linear table can be read in detail (C language Implementation)

`#ifndef _seqqueue_h_#define _seqqueue_h_typedef void seqqueue; seqqueue* seqqueue_create (int capacity); void Seqqueue_destroy (seqqueue* queue); void Seqqueue_clear (seqqueue* queue); int Seqqueue_append (seqqueue* queue, void* item); void* Seqqueue_retrieve (seqqueue* queue); void* Seqqueue_header ( seqqueue* queue); int seqqueue_length (seqqueue* queue); int seqqueue_capacity (seqqueue* queue); #endif`

Source file:

`Linear queue. CPP: Defines the entry point of the console application.#include "stdafx.h" #include "SeqList.h" #include "SeqQueue.h" #include <stdlib.h>seqqueue* seqqueue_create ( int capacity) {return seqlist_create (capacity);} void Seqqueue_destroy (seqqueue* queue) {Seqlist_destroy (queue);} void Seqqueue_clear (seqqueue* queue) {seqlist_clear (queue);} Insert int seqqueue_append (seqqueue* queue, void* item) at the tail {return Seqlist_insert (queue, item, Seqlist_length (queue));} Delete Head void* seqqueue_retrieve (seqqueue* queue) {return Seqlist_delete (queue, 0);} void* seqqueue_header (seqqueue* queue) {return Seqlist_get (queue, 0);} int seqqueue_length (seqqueue* queue) {return seqlist_length (queue);} int seqqueue_capacity (seqqueue* queue) {return seqlist_capacity (queue);}    int _tmain (int argc, _tchar* argv[]) {seqqueue* queue = seqqueue_create (20);    int a[10] = {0};        int i = 0;                for (i=0; i<10; i++) {A[i] = i + 1;    Seqqueue_append (queue, A + i); } printf ("Header:%d\n", * (int*) Seqqueue_headeR (queue));    printf ("Length:%d\n", Seqqueue_length (queue));        printf ("Capacity:%d\n", seqqueue_capacity (queue));    while (Seqqueue_length (queue) > 0) {printf ("Retrieve:%d\n", * (int*) Seqqueue_retrieve (queue)); } seqqueue_destroy (queue); System ("pause"); return 0;}`

Execution Result:

`Header:1length:10capacity:20retrieve:1retrieve:2retrieve:3retrieve:4retrieve:5retrieve:6retrieve:7retrieve:8re Trieve:9retrieve:10 Please press the random key to continue ...`

======================================================================================

Chained implementations of queues:

Same. The chain implementation of the queue is essentially a chain-like linear table, please refer to the code of the linked list: Implementation and operation of the linked list (C language Implementation)

`#ifndef _linkqueue_h_#define _linkqueue_h_typedef void linkqueue; linkqueue* linkqueue_create (); void Linkqueue_destroy (linkqueue* queue); void Linkqueue_clear (linkqueue* queue); int Linkqueue_append (linkqueue* queue, void* item); void* Linkqueue_retrieve (linkqueue* queue); void* Linkqueue_header ( linkqueue* queue); int linkqueue_length (linkqueue* queue); #endif`

Source file:

`Chained queue. CPP: Defines the entry point of the console application. #include "stdafx.h" #include "LinkList.h" #include "LinkQueue.h" #include <malloc.h> #include <stdlib.h>    int _tmain (int argc, _tchar* argv[]) {linkqueue* queue = linkqueue_create ();    int a[10] = {0};        int i = 0;                for (i=0; i<10; i++) {A[i] = i + 1;    Linkqueue_append (queue, A + i);    } printf ("Header:%d\n", * (int*) Linkqueue_header (queue));        printf ("Length:%d\n", Linkqueue_length (queue));    while (Linkqueue_length (queue) > 0) {printf ("Retrieve:%d\n", * (int*) Linkqueue_retrieve (queue));    } linkqueue_destroy (queue); System ("pause"); return 0;}    typedef struct _tag_linkqueuenode{Linklistnode header; void* item;} Tlinkqueuenode; linkqueue* linkqueue_create () {return linklist_create ();}    void Linkqueue_destroy (linkqueue* queue) {linkqueue_clear (queue); Linklist_destroy (queue);} void Linkqueue_clear (linkqueue* queue)//O (n) {while (Linkqueue_length (queue) > 0)    {Linkqueue_retrieve (queue); }}//adds int linkqueue_append (linkqueue* queue, void* item) at the tail {tlinkqueuenode* node = (tlinkqueuenode*) malloc (sizeof (TLi    Nkqueuenode));        int ret = (item! = NULL) && (node! = NULL);                if (ret) {Node->item = Item;    ret = Linklist_insert (queue, (linklistnode*) node, linklist_length (queue));    } if (!ret) {Free (node); } return ret;}    Delete Head void* linkqueue_retrieve (linkqueue* queue) {tlinkqueuenode* node = (tlinkqueuenode*) linklist_delete (queue, 0);        void* ret = NULL;                if (node! = NULL) {ret = node->item;    Free (node); } return ret;}    void* linkqueue_header (linkqueue* queue) {tlinkqueuenode* node = (tlinkqueuenode*) linklist_get (queue, 0);        void* ret = NULL;    if (node! = NULL) {ret = node->item; } return ret;} int linkqueue_length (linkqueue* queue) {return linklist_length (queue);}`

Execution Result:

`Header:1length:10retrieve:1retrieve:2retrieve:3retrieve:4retrieve:5retrieve:6retrieve:7retrieve:8retrieve:9ret Rieve:10 Please press the random key to continue ...`

If there is an error. Hope to point out.

Definition and implementation of queues (C language Implementation)

Related Keywords:

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.