Data Structure: queue chain Storage Structure

Source: Internet
Author: User

The chain storage structure of a queue is actually a single-chain table of a linear table, but it can only be output at the end. We call it a chain queue for short. For operational convenience, we direct the head pointer of the team to the head node of the chain queue, and the tail pointer to the terminal node. When the queue is empty, both front and rear point to the header node.

Example: (changed from "big talk Data Structure")

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# Include <iostream>
Using namespace STD;

Typedef int elemtype;

Typedef struct Node
{
Elemtype data;
Struct node * next;
} Node, * nodeptr;

Typedef struct
{
Nodeptr front;/* head and tail pointer */
Nodeptr rear;
} Linkqueue;
/* Construct an empty queue */
Bool initqueue (linkqueue * LP)
{
Cout <"init queue..." <Endl;
Nodeptr P = (nodeptr) malloc (sizeof (node ));
P-> next = NULL;
Lp-> front = Lp-> rear = P;
Return true;
}
/* Destroy the queue, including the header node */
Bool destroyqueue (linkqueue * LP)
{
Cout <"Destroy queue..." <Endl;
While (LP-> front)
{
Lp-> rear = Lp-> front-> next;
Free (LP-> front );
Lp-> front = Lp-> rear;
}

Return true;
}
/* Empty queue, reserved header node */
Bool clearqueue (linkqueue * LP)
{
Cout <"Clear queue..." <Endl;
Nodeptr P = Lp-> front-> next;
Lp-> front-> next = NULL;
Lp-> rear = Lp-> front;
Nodeptr Q;

While (P)
{
Q = p-> next;
Free (P );
P = Q;
}

Return true;
}

Bool queueempty (linkqueue SCSI)
{
Return SCSI. Front = SCSI. rear;
}

Int queuelength (linkqueue SCSI)
{
Int I = 0;
If (SCSI. Front = NULL)
Return 0;
Nodeptr P = SCSI. Front-> next;
While (P)
{
++ I;
P = p-> next;
}

Return I;
}

Bool gethead (linkqueue SCSI, elemtype * PE)
{
Nodeptr P;
If (SCSI. Front = SCSI. rear)
Return false;
P = SCSI. Front-> next;
* Pe = p-> data;
Cout <"get head item:" <* PE <Endl;
Return true;
}

/* Insert the element ELEM as the new team-end element of the queue */
Bool enqueue (linkqueue * LP, elemtype ELEM)
{
Cout <"enqueue item" <ELEM <Endl;
Nodeptr S = (nodeptr) malloc (sizeof (node ));
S-> DATA = ELEM;
S-> next = NULL;
Lp-> rear-> next = s;
Lp-> rear = s;

Return true;
}
/* Delete the queue Header element and use * PE to return its value */
Bool dequeue (linkqueue * LP, elemtype * PE)
{
If (LP-> front = Lp-> rear)
Return false;
Nodeptr P = Lp-> front-> next;
* Pe = p-> data;
Cout <"dequeue item" <* PE <Endl;
Lp-> front-> next = p-> next;
If (LP-> rear = P)/* If the team header is the team end, delete the rear and point it to the header node */
Lp-> rear = Lp-> front;
Free (P );

Return true;
}
/* Output each element in the queue from the beginning to the end */
Bool queuetraverse (linkqueue SCSI)
{
Cout <"queue traverse..." <Endl;
Nodeptr P = SCSI. Front-> next;
While (P)
{
Cout <p-> data <'';
P = p-> next;
}

Cout <Endl;
Return true;
}

Int main (void)
{
Linkqueue SCSI;
Initqueue (& SCSI );
For (INT I = 0; I <5; I ++)
Enqueue (& SCSI, I );
Queuetraverse (SCSI );
Int result;
Gethead (SCSI, & result );
Dequeue (& SCSI, & result );
Queuetraverse (SCSI );
If (! Queueempty (SCSI ))
Cout <"Queue Length:" <queuelength (SCSI) <Endl;
/* Clearqueue (& SCSI );*/
Destroyqueue (& SCSI );
Cout <"Queue Length:" <queuelength (SCSI) <Endl;

Return 0;
}

Output:


In general, if you can determine the maximum queue value, we recommend that you use a cyclic queue. If you cannot predict the queue length, use a chain queue.

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.