-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkQueue.cpp
141 lines (119 loc) · 3.17 KB
/
LinkQueue.cpp
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
#include "iostream"
using namespace std;
#include "stdlib.h"
#define null 0
typedef struct queueNode{ //A queue node ,include data and 'next poniter'
int data;
queueNode *next;
}QueueNode;
typedef struct queuePointer{ //
QueueNode *front; //front node & rear node , contain data and next node'address
QueueNode *rear;
}Queue;
//==================================create a Link Queue====================
void initQueue(Queue *&L)
{
L=(Queue *)malloc(sizeof(Queue)); //allocate space for the Queue
// L->front->next=null;
// L->rear->next=null; do not do this ,because L->front hasn't initiate,so we can't use L->rear->next;
// At first,front and rear point to null;so the head node &tail node must be assign a node_value.
L->front=null;
L->rear=null;
//front node initiate:
QueueNode *s;
s=(QueueNode *)malloc(sizeof(QueueNode));
s->next = null;
L->front=s;
}
//===============================insert data into the Queue,================
void insertQueue(Queue *&L,int val)
{
QueueNode *s;
s=(QueueNode *)malloc(sizeof(QueueNode));
s->data=val;
s->next=null; //create a new node;
if(L->front->next==null) //the queue is empty
{
L->front->next=s;
L->rear=s; //while queue is empty,assign front node &rear node a node.
}
else{
L->rear->next=s;
L->rear=s;
}
}
//========================display a LinkQueue===============================
//Queue structure:
// |front_node|*| -> ||data1|*| -> |data2|*| -> null
// ^
// |rear_|*|---------------------------'
int displayQueue(Queue *&L)
{
int temp; //save node_data temporary
int len=0; //Queue length
QueueNode *p=L->front->next; //make p point to the first data_node(after front_node)
if(L->front->next==null)
{
cout<<"The Queue is Empty!"<<endl;
return 0;
}
else{
cout<<"Queue data: ";
while(p!=null)
{
temp=p->data;
cout<<temp<<" ";
len++;
p=p->next;
}
cout<<endl<<"Queue length is: "<<len<<endl;
return 1;
}
}
//==========================pop data from the Queue==========================
int popQueue(Queue *&L)
{
QueueNode *p=L->front->next;
int temp;
if(L->front==L->rear)
{
cout<<"The Queue is Empty!"<<endl;
return 0;
}
else{
temp=p->data;
cout<<"Pop: "<<temp<<endl;
L->front->next=p->next;
return temp;
}
}
//==========================free a Queue=====================================
void destroyQueue(Queue *&L)
{
QueueNode *p=L->front->next->next; //the secend data_node;
QueueNode *q=L->front->next;
free(L->front);
free(L->rear);
while(p!=null)
{
free(q);
q=p;
p=p->next;
}
free(q);
free(L);
}
//===========================================================================
int main()
{
Queue *L;
initQueue(L);
for(int i=0;i<10;i++)
{
insertQueue(L,i);
}
displayQueue(L);
//destroyQueue(L);
//displayQueue(L);
return 0;
}