To represent a queue with a circular single linked list

Source: Internet
Author: User

Suppose a circular single linked list is used to represent the queue, and only one pointer is set to point to the rear node, but without the head pointer, an algorithm is designed to initialize, join, team, and determine whether the queue is empty.
Method One: Use the cycle single linked list without the lead node.
1.1 Team Empty Condition Rear==null
1.2 Join the team, insert the node after the *rear node, and let the rear point to the node.
1.3) out of the team, delete a node after the *rear node
As shown in figure (1): Diagram (1) Not the leading node of the cyclic single linked list, insert node
The code is as follows:

/*-------------One, not the leading node of the cyclic single chain to express the queue------------------------/void Initqu (Qnode *&rear) {
Rear=null;
    }//Queue void Enqu (Qnode *&rear,elemtype x) {Qnode *s;
    s= (Qnode *) malloc (sizeof (Qnode));
    s->data=x;
        if (rear==null)//queue is empty {s->next=s;
    Rear=s;
        } else{//queue is not empty s->next=rear->next;
        rear->next=s;
    Rear=s;
    }//out team int dequ (Qnode *&rear,elemtype &x) {Qnode *q;
    if (Rear==null) return 0;
        else if (Rear->next = = rear) {x=rear->data;
        Free (rear);
    Rear=null;
        } else{q=rear->next;
        x=q->data;
        rear->next=q->next;
    Free (q);
return 1; }//To determine if the queue is empty int quempty (Qnode *rear) {return (rear==null);} 

Method Two: Using the lead node of the circular single linked list
2.1) Team Empty condition Rear->next = = Rear
2.2) Join the team, insert the node after the *rear node and let the rear point to the node.
2.3) out of the team, delete a node after the Rear->next node
As shown in figure (2): Figure (2) Lead node of the circular single linked list, delete node
The code is as follows:

void InitQu1 (Qnode *&rear) {
    rear= (Qnode *) malloc (sizeof (Qnode));
    rear->next=rear;
}

Into the team
void EnQu1 (Qnode *&rear,elemtype x) {
    qnode *s;
    s= (Qnode *) malloc (sizeof (Qnode));
    s->next=null;
    s->data=x;
    s->next=rear->next;
    rear->next=s;
    rear=s;
}

Out Team
int DeQu1 (Qnode *&rear,elemtype &x) {
    qnode *q;
    if (rear->next==rear) return
        0;
    else if (rear->next->next==rear) {
        x=rear->data;
        q=rear->next;
        q->next=q;
        Free (rear);
        rear=q;
        return 1;
    }
    else{
        q=rear->next->next;
        x=q->data;
        rear->next->next=q->next;
        Free (q);
        return 1;
    }

}

int QuEmpty1 (Qnode *rear) {return
    (rear->next==rear);

}

DuiLie.cpp
The complete code is as follows:

#include <stdio.h> #include <malloc.h> typedef int ELEMTYPE;
    typedef struct qnode{//Data node elemtype;
struct Qnode *next;

}qnode;
    typedef struct{//Chain teams define Qnode *front;
Qnode *rear;


}liqueue;

/*-------------One, not the leading node of the cyclic single chain to express the queue---------------/void Initqu (Qnode *&rear) {rear=null;}
    Team void Enqu (Qnode *&rear,elemtype x) {Qnode *s;
    s= (Qnode *) malloc (sizeof (Qnode));
    s->data=x;
        if (rear==null)//queue is empty {s->next=s;
    Rear=s;
        } else{//queue is not empty s->next=rear->next;
        rear->next=s;
    Rear=s;
    }//out team int dequ (Qnode *&rear,elemtype &x) {Qnode *q;
    if (Rear==null) return 0;
        else if (Rear->next = = rear) {x=rear->data;
        Free (rear);
    Rear=null;
        } else{q=rear->next;
        x=q->data;
        rear->next=q->next;
    Free (q);
return 1; }//Determine if the queue is an empty int quempty (Qnode *rear) {return (rear==null);} /*-------------------Two, using the leading node of the circular single linked list to express the queue------------------/void InitQu1 (Qnode *&rear) {rear= (Qnode *) malloc (
    sizeof (Qnode));
rear->next=rear;
    }//Team void EnQu1 (Qnode *&rear,elemtype x) {Qnode *s;
    s= (Qnode *) malloc (sizeof (Qnode));
    s->next=null;
    s->data=x;
    s->next=rear->next;
    rear->next=s;
Rear=s;
    }//out team int DeQu1 (Qnode *&rear,elemtype &x) {Qnode *q;
    if (rear->next==rear) return 0;
        else if (rear->next->next==rear) {x=rear->data;
        q=rear->next;
        q->next=q;
        Free (rear);
        rear=q;
    return 1;
        } else{q=rear->next->next;
        x=q->data;
        rear->next->next=q->next;
        Free (q);
    return 1;

{QuEmpty1 (Qnode *rear)} int {return (rear->next==rear);
    ///No lead node of the cyclic single linked list output void Dispqueuer (Qnode *&rear) {if (rear==null) return; Qnode *p=rear->next;
        do{printf ("%d", p->data);
    p=p->next;

    }while (P!=rear->next);

printf ("\ n");
    }///lead node cycle single linked list output void DispQueueR2 (Qnode *&rear) {if (Rear->next = = rear) return;

    Qnode *q=rear->next->next;
        while (q!=rear) {printf ("%d", q->data);
    q=q->next;
    } if (q==rear) {printf ("%d", q->data);
printf ("\ n");

    } void Main () {Qnode *p;
The cyclic single linked list/Initqu (P) without the lead node;
int i=0;

    for (i=1;i<10;i++)//{//Enqu (P,I*I-1);//}//Dispqueuer (P);
    The cyclic single link table InitQu1 (p) of the lead node;
    int j=0;
    for (j=1;j<10;j++) {EnQu1 (p,j*j-1);


} DispQueueR2 (P); }

The effect is as follows:

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.