-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueueLink.cpp
100 lines (89 loc) · 1.26 KB
/
QueueLink.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
#include "QueueLink.h"
#include "StackLink.h"
#include <iostream>
using namespace std;
int QueueLink::IsEmpty()
{
return head->data == 0;
}
void QueueLink::ShowData()
{
if (IsEmpty())
cout << "¶ÓÁпÕ";
else
{
LinkNodeP p = head;
while (p->next)
{
p = p->next;
cout << p->data << " ";
}
}
cout << endl;
}
void QueueLink::EnQueue(int data)
{
LinkNodeP p = new LinkNode;
p->data = data;
tail->next = p;
p->next = NULL;
tail = p;
head->data++;
}
int QueueLink::DeQueue()
{
if (IsEmpty())
return 0;
else
{
head->data--;
LinkNodeP p = head->next, temp = p->next;
int result = p->data;
if (p == tail)
tail = head;
delete p;
head->next = temp;
return result;
}
}
int QueueLink::Top()
{
if (IsEmpty())
return 0;
else
{
return head->next->data;
}
}
int QueueLink::Count()
{
return head->data;
}
void QueueLink::ReverseQueueUsingStack()
{
StackLink s1, s2;
while (!IsEmpty())
s1.Push(DeQueue());
s1.ShowData();
/*while (!s1.IsEmpty())
s2.Push(s1.Pop());*/
while (!s1.IsEmpty())
EnQueue(s1.Pop());
}
QueueLink::QueueLink()
{
head = new LinkNode;
head->data = 0;
head->next = NULL;
tail = head;
}
QueueLink::~QueueLink()
{
LinkNodeP p = head, temp;
while (p != NULL)
{
temp = p->next;
delete p;
p = temp;
}
}