forked from CodeWithKartik/BookMyShow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.cpp
121 lines (119 loc) · 3.39 KB
/
LinkedList.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
#include "LinkedList.h"
#include "User.h"
#include "Movie.h"
//BMSLink
template <class DataType> BMSLink<DataType>::BMSLink(DataType *ele) {
link = ele;
next = NULL;
}
template <class DataType> BMSLink<DataType>::BMSLink(const BMSLink &cpy) {
link = new DataType(*(cpy.GetLink()));
next = NULL;
}
template <class DataType> BMSLink<DataType>::~BMSLink() {
delete link;
}
//BMSUsers
template <class DataType> BMSUsers<DataType>::BMSUsers() {
start = NULL;
}
template <class DataType> BMSUsers<DataType>::BMSUsers(const BMSUsers &cpy) {
BMSLink<DataType> *temp = cpy.GetStart();
while (temp != NULL) {
InsertLink(temp);
temp = temp->GetNext();
}
}
template <class DataType> BMSUsers<DataType>::~BMSUsers() {
BMSLink<DataType> *temp = start;
while (start != NULL) {
temp = start->GetNext();
delete start;
start = temp;
}
}
template <class DataType> BMSLink<DataType> *BMSUsers<DataType>::InsertLink(BMSLink<DataType> *link) {
BMSLink<DataType> *temp = start, *prev;
prev = temp;
BMSLink<DataType> *new_link = link;
if (temp == NULL)
start = new_link;
else {
while (temp != NULL) {
prev = temp;
temp = temp->GetNext();
}
prev->SetNext(new_link);
}
return start;
}
template <class DataType> BMSLink<DataType> *BMSUsers<DataType>::DeleteLink(BMSLink<DataType> *link) {
if (start == NULL)
return start;
else {
BMSLink<DataType> *temp = start, *prev = NULL;
while (temp != NULL) {
if (temp == link) {
if (prev == NULL)
start = temp->GetNext();
else {
prev->SetNext(temp->GetNext());
}
delete temp;
break;
}
prev = temp;
temp = temp->GetNext();
}
return start;
}
}
std::ostream& operator<<(std::ostream& os, const BMSLink<User> *lnk) {
os<<lnk->GetLink();
return os;
}
std::ostream& operator<<(std::ostream& os, const BMSLink<Movie> *lnk) {
os<<lnk->GetLink();
return os;
}
std::ostream& operator<<(std::ostream& os, const BMSLink<Cinema> *lnk) {
os<<lnk->GetLink();
return os;
}
std::ostream& operator<<(std::ostream& os, const BMSUsers<User> *usrs) {
BMSLink<User> *temp = usrs->GetStart();
while (temp != NULL) {
os<<"----------User Starts----------\n";
os<<temp;
os<<"----------User Ends------------\n";
temp = temp->GetNext();
}
return os;
}
std::ostream& operator<<(std::ostream& os, const BMSUsers<Movie> *mvs) {
BMSLink<Movie> *temp = mvs->GetStart();
while (temp != NULL) {
os<<"----------Movie Starts----------\n";
os<<temp;
os<<"----------Movie Ends------------\n";
temp = temp->GetNext();
}
return os;
}
std::ostream& operator<<(std::ostream& os, const BMSUsers<Cinema> *cns) {
BMSLink<Cinema> *temp = cns->GetStart();
while (temp != NULL) {
os<<"----------Cinema Starts----------\n";
os<<temp;
os<<"----------Cinema Ends------------\n";
temp = temp->GetNext();
}
return os;
}
//Template types to be used in this project
template class BMSLink<User>;
template class BMSUsers<User>;
template class BMSLink<Movie>;
template class BMSUsers<Movie>;
template class BMSLink<Cinema>;
template class BMSUsers<Cinema>;