-
Notifications
You must be signed in to change notification settings - Fork 0
/
OOP - rec11.cpp
184 lines (155 loc) · 4.49 KB
/
OOP - rec11.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
filename: rec11.cpp
made by Yohann Abittan
netid: yaa243
*/
#include <cstdlib> // To allow NULL if no other includes
#include <iostream>
using namespace std;
class LList;
struct Node{
int data;
Node * next;
Node(int newData = 0, Node* newNext = nullptr): data(newData), next(newNext){}
};
void push_front(int newData, Node*& head){head = new Node(newData, head);}
void push_back(int newData, Node*& head){
Node* temp = head;
while (temp->next != nullptr)
temp = temp->next;
temp->next = new Node(newData);
}
void spliceLList( Node*& splicePoint, Node* otherLList ){
Node* temp = splicePoint->next;
splicePoint->next = otherLList;
while( otherLList->next != nullptr ){
otherLList = otherLList->next;
}
otherLList->next = temp;
}
Node* searchNode( int data, Node* llist ){
Node* temp = llist;
while( temp != nullptr ){
if( temp->data == data )
return temp;
temp = temp->next;
}
return nullptr;
}
ostream& operator<<(ostream& os, Node* rhs){
if( rhs == nullptr )
os << "No Matching Linked List" << endl;
else{
Node* temp = rhs;
while( temp != nullptr ){
cout << temp->data << ' ';
temp = temp->next;
}
cout << endl;
}
return os;
}
Node* searchList( Node* mainLList, Node* subList ){
Node* outputLList = nullptr;
Node* temp = mainLList;
Node* temp2 = mainLList;
Node* tempSub = subList;
while (tempSub != nullptr && temp != nullptr){
if (temp->data == tempSub->data){
if (!outputLList)
outputLList = temp;
if (temp->next){
temp = temp->next;
tempSub = tempSub->next;
}
else{
if(tempSub->next != nullptr)
outputLList = nullptr;
break;
}
}
else{
if (outputLList){
outputLList = nullptr;
tempSub = subList;
temp2 = temp2->next;
temp = temp2;
}
else
temp = temp->next;
}
}
return outputLList;
}
int main(){
cout << "Part One" << endl << endl;
Node* llist1 = nullptr;
push_front( 1, llist1 );
push_front( 9, llist1 );
push_front( 7, llist1 );
push_front( 5, llist1 );
cout << "L1: " << llist1;
Node* llist2 = nullptr;
push_front( 2, llist2 );
push_front( 3, llist2 );
push_front( 6, llist2 );
cout << "L2: " << llist2;
Node* aNode = searchNode( 7, llist1 );
cout << "Target: " << aNode;
cout << "Splicing L2 at target in L1" << endl;
spliceLList( aNode, llist2 );
cout << "L1: " << llist1;
cout << "L2: " << llist2;
cout << "\nPart two" << endl << endl;
Node* llist3 = nullptr;
push_front( 6, llist3 );
push_front( 5, llist3 );
push_front( 4, llist3 );
push_front( 2, llist3 );
push_front( 3, llist3 );
push_front( 2, llist3 );
push_front( 3, llist3 );
push_front( 2, llist3 );
push_front( 1, llist3 );
cout << "Target: " << llist3;
Node* llist4 = nullptr;
push_front( 1, llist4 );
cout << "\nAtempt match: " << llist4;
Node* llist5 = searchList(llist3, llist4);
cout << llist5;
Node* llist6 = nullptr;
push_front( 9, llist6 );
push_front( 3, llist6 );
cout << "\nAtempt match: " << llist6;
Node* llist7 = searchList(llist3, llist6);
cout << llist7;
Node* llist8 = nullptr;
push_front( 3, llist8 );
push_front( 2, llist8 );
cout << "\nAtempt match: " << llist8;
Node* llist9 = searchList(llist3, llist8);
cout << llist9;
Node* llist10 = nullptr;
push_front( 6, llist10 );
push_front( 5, llist10 );
push_front( 4, llist10 );
push_front( 2, llist10 );
cout << "\nAtempt match: " << llist10;
Node* llist11 = searchList(llist3, llist10);
cout << llist11;
Node* llist12 = nullptr;
push_front( 4, llist12 );
push_front( 2, llist12 );
push_front( 3, llist12 );
push_front( 2, llist12 );
cout << "\nAtempt match: " << llist12;
Node* llist13 = searchList(llist3, llist12);
cout << llist13;
Node* llist14 = nullptr;
push_front( 7, llist14 );
push_front( 6, llist14 );
push_front( 5, llist14 );
cout << "\nAtempt match: " << llist14;
Node* llist15 = searchList(llist3, llist14);
cout << llist15;
}