Queue, chain queue, chain storage queue

Source: Internet
Author: User

A chain-based queue is called a linked queue. A queue is a linear table with limited operations, that is, the insertion and deletion of a queue are at both ends of the table. Therefore, two pointers are required to point to these two special locations, namely, the first pointer and the last pointer.

This example uses the chain queue of the leading node. Therefore, the first pointer points to the first node, and the pointer field of the first node points to the first node.

When the queue is empty, the first pointer and the last pointer point to the header node, and the pointer field of the header node points to null.

Code:

/** Main. c ** created on: 2012-10-21 * Author: China */# include <stdio. h> # include <stdlib. h> # include <assert. h>/* the definition of chain queue is as follows */typedef int datatype; typedef struct snode {datatype data; struct snode * Next;} qnode, * linklist; typedef struct {linklist front; // The first node of the Team (that is, the Deletion Point) linklist rear; // The End Node (that is, the insertion point)} linkqueue; int creat_empty_linkqueue (linkqueue * Queue) {linklist head = (linklist) malloc (sizeof (qnode); If (Null = head) Return-2; head-> next = NULL; queue-> front = queue-> rear = head; return 1 ;} int linkqueue_empty (const linkqueue * Queue) {return queue-> front = queue-> rear;} int linkqueue_length (const linkqueue * Queue) {int Len = 0; linklist P = queue-> front; while (P! = Queue-> rear) {P = p-> next; Len ++;} return Len;} int linkqueue_get_top_element (const linkqueue * queue, datatype * E) {If (linkqueue_empty (Queue) {return-1;} * E = queue-> front-> next-> data; return 1 ;} // The Int linkqueue_enter (linkqueue * queue, datatype e) {linklist q = (linklist) malloc (sizeof (qnode); If (null = q) Return-2; q-> DATA = E; q-> next = NULL; queue-> rear-> next = Q; // modify the pointer field of the node at the end of the team, point it to the new team end node queue-> r Ear = Q; // modify the team end pointer so that it points to the new team end node return 1;} // The Int linkqueue_delete (linkqueue * queue, datatype * E) {// method 1, (The deletion feature of the non-conforming queue is only performed on the header, that is, only the first pointer to front is operated.) If (linkqueue_empty (Queue) {return-1; // empty queue} linklist q = queue-> front-> next; // Q points to the first node to be deleted-> front-> next = Q-> next; // point the pointer field of the header node to the deleted next node // if the last node is deleted, You need to point queue-> rear to the linked list header node !!!! If (null = Q-> next) {queue-> rear = queue-> front;} * E = Q-> data; // return the value of the deleted first element free (Q); q = NULL; return 1; // method 2/* linklist P = queue-> front; queue-> front = p-> next; free (p); Return queue-> front-> data; */}/* -------------------------------------------------------- operation objective: first condition: the queue already exists operation result: the queue is cleared function parameter: linkqueue * queue return value: None --------------------------------------------------- --------- */Void linkqueue_clear (linkqueue * Queue) {// method 1/* assert (queue-> front! = NULL); linklist P = queue-> front-> next; while (p) {queue-> front-> next = p-> next; free (P ); P = queue-> front-> next; printf ("========= clear () =========\ N ");} * /// method 2 datatype E; while (! Linkqueue_empty (Queue) {linkqueue_delete (queue, & E) ;}/ * queue operation objective: to destroy the queue (that is, delete all nodes including Header nodes) initial conditions: operation Result of queue already exists: Destroy queue function parameter: linkqueue * queue to be destroyed returned value: No queue */void destroyqueue (linkqueue * Queue) {assert (queue! = NULL); While (queue-> front) {queue-> rear = queue-> front-> next; free (queue-> front ); queue-> front = queue-> rear; printf ("======================= destroy ===============================\ N ") ;}} int main (INT argc, char ** argv) {int I; datatype E, top_e; linkqueue queue; creat_empty_linkqueue (& Queue); If (linkqueue_empty (& Queue )) {printf ("Empty \ n");} else {printf ("not empty \ n");} printf ("link queue length is: % d \ n ", linkqueue_length (& Qu EUE); printf ("START enter element to link queue \ n"); for (I = 0; I <5; I ++) {linkqueue_enter (& queue, I + 1);} If (-1! = Linkqueue_get_top_element (& queue, & top_e) {printf ("the top of link queue element is: % d \ n", top_e);} If (linkqueue_empty (& Queue )) {printf ("Empty \ n");} else {printf ("not empty \ n");} printf ("link queue length is: % d \ n ", linkqueue_length (& Queue); printf ("START Delete element to link queue and the delete element are: \ n"); for (I = 0; I <3; I ++) {linkqueue_delete (& queue, & E); printf ("% d \ t", e) ;} printf ("\ n "); if (linkqueue_empty (& Queue) {printf ("Empty \ n");} else {printf ("not empty \ n");} printf ("link queue length is: % d \ n ", linkqueue_length (& Queue); linkqueue_clear (& Queue); destroyqueue (& Queue); Return 0 ;}

Running result:

emptylink queue length is:0start enter element to link queuethe top of link queue element is:1not emptylink queue length is:5start delete element from link queue and the delete element are:123not emptylink queue length is:2========clear()================clear()========================destroy================

 

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.