Queue chain Storage-design and implementation-API functions

Source: Internet
Author: User

Queue-Related Basics My blog: Queue sequential Storage-design and implementation-API functions

The queue is also a special linear table and can be used to simulate the chained storage of a queue with linear chain-linked storage.

Main code:

linkqueue.h//Queue chained Storage API declaration #ifndef _linkqueue_h_#define _linkqueue_h_typedef void linkqueue;//Create queue linkqueue* Linkqueue_create ();//Destroy queue void Linkqueue_destroy (linkqueue* queue);//Empty queues void linkqueue_clear (linkqueue*);// into the queue int linkqueue_append (linkqueue* queue, void* item);//Out Queue void* Linkqueue_retrieve (linkqueue* queue);//Get head element void* Linkqueue_header (linkqueue* queue);//Get Queue Length int linkqueue_length (linkqueue* queue); #endif//_my_linkqueue_h_

linkqueue.cpp//Queue chain Storage API implementation//Call chain Storage linear table # include <stdio.h> #include "linkqueue.h" #include "linklist.h" # Include <malloc.h>typedef struct _tag_linkqueue{linklistnode node;void* item;} tlinkqueue;//creating a queue is equivalent to creating a linked list linkqueue* linkqueue_create () {return linklist_create ();} Destroying the queue is equivalent to creating a linked list void Linkqueue_destroy (linkqueue* queue) {linkqueue_clear (queue); Linklist_destroy (queue);} Emptying the queue is equivalent to emptying the list void Linkqueue_clear (linkqueue* queue) {while (Linkqueue_length (queue)) {Linkqueue_retrieve (queue);}} Into the queue is equivalent to inserting elements in the tail of the list int linkqueue_append (linkqueue* queue, void* item) {Tlinkqueue *tmp = null;int ret = 0;tmp = (tlinkqueue *) malloc (sizeof (tlinkqueue)); if (tmp = = NULL) {//allocation failed return-1;} Tmp->item = Item;ret = Linklist_insert (Queue, (Linklistnode *) tmp, Linklist_length (queue)), if (ret) {printf (" function Linkqueue_append err:%d\n ", ret); free (TMP); return ret;} return ret;}  The out queue is equivalent to deleting the element in the list No. 0 position void* linkqueue_retrieve (linkqueue* queue) {Tlinkqueue *tmp = null;void* item = null;tmp = (tlinkqueue *) LinkliSt_delete (queue, 0), if (TMP = = null) {return null;} item = Tmp->item;free (TMP); Do not forget to release the node return item when out of the queue;} Get the team header element equivalent to get the list No. 0 number position element void* Linkqueue_header (linkqueue* queue) {Tlinkqueue *tmp = Null;tmp = (Tlinkqueue *) Linklist_get ( queue, 0); if (TMP = = null) {return null;} return Tmp->item;} Gets the queue Length int linkqueue_length (linkqueue* queue) {return linklist_length (queue);}

main.cpp//Queue chain store API Test program # include <stdio.h> #include "linkqueue.h" const int MAXN = 10;void Play () {int i = 0, A[MAXN ]; Linkqueue *LQ = null;for (i = 0; i < MAXN; ++i) {A[i] = i + 1;} LQ = Linkqueue_create (); Create queue//into queue for (i = 0; i < MAXN; ++i) {linkqueue_append (LQ, &a[i]);} Queue properties printf ("Header:%d\n", * ((int *) Linkqueue_header (LQ)));p rintf ("Length:%d\n", Linkqueue_length (LQ));// Out Queue while (Linkqueue_length (LQ)) {int TMP = * (int *) Linkqueue_retrieve (LQ));p rintf ("%d\n", TMP);} Destroy Queue Linkqueue_destroy (LQ);} int main () {play (); return 0;}

For more information about sequential storage tables, see my other blog. Sequential storage design and implementation of linear tables-API function implementation

Related Engineering Code: Github

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Queue chain Storage-design and implementation-API functions

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.