Implementation and operation of the queue (C language description)

Source: Internet
Author: User

//single-linked list implementation for queues//head node: Sentinel function, where data is not stored, which is used to initialize the queue so that the tail of the team head is pointed.//First node : node after head node, storing data#include<stdio.h>#include<malloc.h>#include<stdlib.h>typedefintElementype;//Defining data Types//Defining the node structuretypedefstructNode {elementype Element; //data fields    structNode *Next;} NODE,*Pnode;//defining the queue structure bodytypedefstructQnode {pnode Front, Rear; //queue header, tail pointer} Queue, *Pqueue;//declaring the function bodyvoidInitqueue (Pqueue);//Create a queue functionBOOLIsemptyqueue (Pqueue);//determine if the queue is an empty functionvoidInsertqueue (Pqueue,intVal);//Queue functionvoidDeletequeue (Pqueue,int* val);//out Team functionvoidDestroyqueue (Pqueue);//Destroy queue functionvoidTraversequeue (Pqueue);//traversing queue functionsvoidClearqueue (Pqueue);//Empty queue functionintLengthqueue (Pqueue);//Find the queue Length function//Main functionintMain () {intval =0;//Defining temporary variablesQueue queue;//Create a queue variableInitqueue (&queue);//Call Initialize queue functionIsemptyqueue (&queue);//call to determine if the queue is an empty functionInsertqueue (&queue, -);//Call the Enqueue functionInsertqueue (&queue, $); Insertqueue (&queue, -); Insertqueue (&queue, -); Insertqueue (&queue, -); Insertqueue (&queue, -); Isemptyqueue (&queue); Traversequeue (&queue);//Call Queue traversal functionDeletequeue (&queue, &val);//call out the team functionTraversequeue (&queue); Clearqueue (&queue);//Call empty queue functionIsemptyqueue (&queue); Destroyqueue (&queue);//Call the Destroy queue function    return 0;}//defining the queue initialization functionvoidinitqueue (pqueue queue) {Queue->front = Queue->rear = (pnode)malloc(sizeof(NODE));//dynamic creation of head nodes, so that the team head, the tail points to the node//the head node acts as a sentinel node and does not store data (as distinct from the first node)    if(Queue->front = = NULL) {//determine if memory is allocated successfullyprintf"Create queue, unable to allocate required memory ..."); Exit (-1); } Queue->front->next = NULL;//Initial queue head node pointer pointing to nullprintf"Create queue success ... \ n");}//defines whether the queue is an empty functionBOOLisemptyqueue (pqueue queue) {if(Queue->front = = queue->Rear) {printf ("queue is empty ... \ n"); return true; }    Else {        //printf ("Queue is not empty ... \ n");        return false; }        }//defining the Queued function//insert data from the end of the queue ValvoidInsertqueue (Pqueue queue,intval) {Pnode P= (Pnode)malloc(sizeof(NODE));//Create a new node to hold the inserted element    if(P = =NULL) {printf ("memory allocation failed, unable to insert data%d ...", Val); Exit (-1); } P->element = val;//Put the data you want to insert into the node data fieldP->next = NULL;//The new node pointer is pointing to an emptyQueue->rear->next = P;//Point the node pointer at the end of the previous queue to the newly created nodeQueue->rear = P;//update the tail pointer to point to the last node in the queueprintf"Insert data%d successfully ... \ n", Val);}//define the team function//start the team from the first node of the queue//If the team succeeds, return its value with ValvoidDeletequeue (Pqueue queue,int*val) {    if(Isemptyqueue (queue)) {printf ("queue is empty, unable to get out of the team ... \ n"); Exit (-1); } pnode P= queue->front->next;//Temporary pointers*val = p->element;//save its valueQueue->front->next = p->next;//Update head node    if(queue->rear==P) Queue->rear = queue->Front;  Free(P);//Release Head QueueP = NULL;//prevents the creation of wild pointersprintf"The stack is successful and the stack value is%d\n", *val);}//defining queue Traversal functionsvoidtraversequeue (pqueue queue) {if(Isemptyqueue (queue)) {exit (-1); } pnode P= queue->front->next;//traversal from the first node of the queue (not the head node, note the distinction)printf"The traversal queue results are:");  while(P! =NULL) {printf ("#df", p->Element); P= p->Next; } printf ("\ n");}//define the Destroy function of the queue//Delete the entire queue, including the head nodevoiddestroyqueue (pqueue queue) {//Delete from the start node     while(Queue->front! =NULL) {Queue->rear = queue->front->Next;  Free(queue->Front); Queue->front = queue->Rear; } printf ("Destroy queue success ... \ n");}//define empty queue functionsvoidclearqueue (pqueue queue) {Pnode P= queue->front->next;//Temporary pointersPnode Q = NULL;//Temporary pointersQueue->rear = queue->front;//point the tail pointer to the head nodeQueue->front->next =NULL; //start emptying from the first node     while(P! =NULL) {Q=P; P= p->Next;  Free(Q); } printf ("empty queue succeeded ... \ n");}

Run results

Implementation and operation of the queue (C language description)

Related Article

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.