The LinkQuNode for Linked queue (Continuous updates) | Data

简介: The code in Data book (5th Edition) from the 105 page to 106 page

The code in Data book (5th Edition) from the 105 page to 106 page

Continuous updates
//Statement DataNode
typedef struct qnode
{
    ElemType data;
    struct qnode *next;
} DataNode;

//Statement LinkQuNode
typedef struct {  
    DataNode *front; //Point to single-list team head junction
    DataNode *rear; //Point to single-list team end knot
} LinkQuNode;

//Initialize the queue
void InitQueue(LinkQuNode *&q) {
    q=(LinkQuNode *)malloc(sizeof(LinkQuNode));
    q->front=q->rear=NULL;
}

//Destroy the queue
void DestroyQueue(LinkQuNode *&q) {
    DataNode *p=q->front, *r; //p point to team head data node
    if  (p!=NULL) { //Free data node footprint
        r=p->next;
        while (r!=NULL) {
            free(p);
            p=r; r=p->next;
        }
    }
    free(p);  free(q); //Release chain junction stakes in space
}

//Determine if the queue is empty
bool QueueEmpty(LinkQuNode *q) {
    return(q->rear==NULL);
}

//Get in the team
void enQueue(LinkQuNode *&q, ElemType e) {
    DataNode *p;
    p=(DataNode *)malloc(sizeof(DataNode));
    p->data=e;
    p->next=NULL;
    if (q->rear==NULL)
        q->front=q->rear=p;
    else {
        q->rear->next=p; //Set resr point to node p
        q->rear=p;
    }
}

//Out of the team
bool deQueue(LinkQuNode *&q, ElemType &e) {
    DataNode *t;
    if (q->rear==NULL) return false; //When queue is null
        t=q->front; //t point to first data node
    if (q->front==q->rear)
        q->front=q->rear=NULL;
    else //When queue have multipe node
        q->front=q->front->next;
    e=t->data;
    free(t);
    return true;
}
如遇侵权,请联系作者删除
目录
相关文章
|
12月前
|
Perl
Pod Performing a deep fetch of the `master` specs repo to improve future perform
Pod Performing a deep fetch of the `master` specs repo to improve future perform
40 0
The SqQueue for Sequential queue (Continuous updates) | Data
The code in Data book (5th Edition) from the 99 page to 100 page
89 0
Leetcode-Easy 141. Linked List Cycle
Leetcode-Easy 141. Linked List Cycle
90 0
Leetcode-Easy 141. Linked List Cycle
Data Structures and Algorithms (English) - 6-4 Reverse Linked List(20 分)
Data Structures and Algorithms (English) - 6-4 Reverse Linked List(20 分)
112 0
PAT (Advanced Level) Practice - 1004 Counting Leaves(30 分)
PAT (Advanced Level) Practice - 1004 Counting Leaves(30 分)
101 0
PAT (Advanced Level) Practice - 1143 Lowest Common Ancestor(30 分)
PAT (Advanced Level) Practice - 1143 Lowest Common Ancestor(30 分)
109 0
PAT (Advanced Level) Practice - 1098 Insertion or Heap Sort(25 分)
PAT (Advanced Level) Practice - 1098 Insertion or Heap Sort(25 分)
95 0
why My Lead OPA test add Lead fails
why My Lead OPA test add Lead fails
123 0
why My Lead OPA test add Lead fails
Identify the logic how BOL node name is categorized into different object type
Identify the logic how BOL node name is categorized into different object type
Identify the logic how BOL node name is categorized into different object type
|
数据建模
1133. Splitting A Linked List (25)
Given a singly linked list, you are supposed to rearrange its elements so that all the negative values appear ...
1407 0