-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLQueue.cpp
More file actions
87 lines (87 loc) · 1.31 KB
/
LQueue.cpp
File metadata and controls
87 lines (87 loc) · 1.31 KB
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
//linked queue
class LQueue
{
private:
LNode* front;
LNode* rear;
int queueLen;
public:
//构造函数
LQueue();
//析构函数
~LQueue();
//将元素插入队列
void eLQueue(int x);
//从队列中移除元素
int deLQueue();
//重新初始化队列
void clear();
//得到队首元素值
int getFrontValue();
//得到队列长度
int getLQueueLen();
//打印队列中元素
void print();
};
//linked queue
LQueue::LQueue()
{
front = rear = new LNode();
queueLen = 0;
}
LQueue::~LQueue()
{
while (front != nullptr)
{
LNode* temp = front;
front = front->next;
delete temp;
}
}
void LQueue::eLQueue(int x)
{
rear =rear->next= new LNode(x, nullptr);
queueLen++;
}
int LQueue::deLQueue()
{
if (queueLen == 0)
return -1;
int temp = front->next->elment;
LNode* temp1 = front->next;
front->next = front->next->next;
if (rear == temp1)
rear = front;
delete temp1;
queueLen--;
return temp;
}
void LQueue::clear()
{
while (front != nullptr)
{
LNode* temp = front;
front = front->next;
delete temp;
}
LQueue();
}
int LQueue::getFrontValue()
{
if (queueLen == 0)
return -1;
return front->next->elment;
}
int LQueue::getLQueueLen()
{
return queueLen;
}
void LQueue::print()
{
LNode* temp = front->next;
while(temp!=nullptr)
{
printf("%d\n", temp->elment);
temp = temp->next;
}
}