-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLQueueImplementation.cpp
More file actions
92 lines (83 loc) · 1.53 KB
/
Copy pathLQueueImplementation.cpp
File metadata and controls
92 lines (83 loc) · 1.53 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
88
89
90
91
92
#include <iostream>
using namespace std;
template <typename T>
struct node{
T data;
node* next;
};
template <typename T>
class LQueue
{
node<T> *front, *back;
void copy(LQueue<T> const& q)
{
back=NULL;
if(!q.empty()){
node<T>* p = q.front;
while(p){
push(p->data);
p=p->next;
}
}
}
void clean(){
while(!empty())
pop();
}
public:
LQueue():front(NULL),back(NULL){}
LQueue(LQueue const& q) : front (NULL), back(NULL){
copy(q);
}
LQueue& operator=(LQueue const& q)
{
if(this != q)
{
clean();
copy(q);
}
return *this;
}
~LQueue(){
clean();
}
bool empty() const{
return back==NULL;
}
void push(T const& x){
node<T>* p = new node<T>;
p->data= x;
p->next=NULL;
if(!empty()){
back->next = p;
}else
front = p;
back = p;
}
T pop(){
if(empty()){
cerr<<"opit za izkliuchvane ot prazna opashka! \n";
return T();
}
node<T>* p = front;
T x = p->data;
if(p == back){
front = NULL;
back = NULL;
}else
front = p->next;
delete p;
return x;
}
T head() const{
if(empty()){
cerr<<"opit za poglejdane v prazna opashka! \n";
return T();
}
return front->data;
}
};
int main()
{
return 0;
}