-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLLN.cpp
More file actions
158 lines (142 loc) · 3.32 KB
/
Copy pathLLN.cpp
File metadata and controls
158 lines (142 loc) · 3.32 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
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
//
// LLN.cpp
// PG4 V4 - Modified delInitial to proporly delete all instances of the word
// V3 - Corrected variable mistype in addinorder_0
//
// Created by Lawrence Johnson on 11/1/14.
// Copyright (c) 2014 Lawrence Johnson. All rights reserved.
//
#include <iostream>
#include <string>
#include <cstdlib>
#include <algorithm> // V2 added
#include "LLN.h"
using namespace std;
/*
Create new node with i = I and next = n
*/
LLN::LLN (string I, LLN * n) { i = I; next = n;}
/*
Destructor
*/
LLN::~LLN () {
delete next;
}
/*
Get i variable
*/
string LLN::geti () {return i;}
/*
Get next variable
*/
LLN * LLN::getnext () {return next;}
/*
Set next variable
*/
void LLN::setnext (LLN *n) {next = n;}
/*
Print i
*/
void LLN::print() {
cout << i << " ";
}
/*
Prints current node and passes tells next node to print
*/
void LLN::printall () {
print ();
cout << endl;
if (next) next->printall ();
}
/*
Add string to the back of the list
*/
void LLN::addback (string I) {
if (next==NULL) next = new LLN (I,NULL);
else next->addback (I);
}
/*
Add string in a smallest to largest order
*/
LLN * LLN::addinorder_0 (string I) {
string temp = i;
string temp2 = I; // V3 - variable set to i instead of I
transform(temp.begin(), temp.end(), temp.begin(), ::tolower);
transform(temp2.begin(), temp2.end(), temp2.begin(), ::tolower);
if (temp > temp2) return new LLN (I,this);
if (!next) {next = new LLN (I,NULL); return this;}
next = next->addinorder_0 (I);
return this;
}
/*
Delete first instance of a the passed string
*/
LLN * LLN::delfirst (string I) {
if (i==I) {
LLN *t = next;
next = NULL;
delete this;
return t;
}
if (next) next = next->delfirst (I);
return this;
}
/*
Delete last instance of a the passed string
*/
void LLN::delfirst_0 (string I, LLN *prev, LL *l) {
if (i==I) {
if (!prev) l->sethead (this->next);
else prev->setnext (this->next);
next = NULL;
delete this;
} else
if (next) next->delfirst_0 (I,this,l);
}
/*
Delete last instance of a the passed string
*/
bool LLN::dellast (string I, LLN *prev, LL *l) {
bool del = false;
if (next) del = next->dellast (I,this,l);
if (del) return true;
if (i==I) {
if (!prev) l->sethead (this->next);
else prev->setnext (this->next);
next = NULL;
delete this;
return true;
}
return false;
}
/*
Delete all appearences of a word
*/
LLN * LLN::DelInitial (string c) {
if (next) next = next->DelInitial (c);
string temp = i;
transform(temp.begin(), temp.end(), temp.begin(), ::tolower);
transform(c.begin(), c.end(), c.begin(), ::tolower); // V4 added to make sure comparisons are all lower cased.
if (temp.find(c) != string::npos) {
LLN *t = next;
next = NULL;
delete this;
return t;
}
return this;
}
/*
Check if the book title already appears in the list
*/
bool LLN::check(string s){
transform(s.begin(), s.end(), s.begin(), ::tolower);
bool match = false;
if (!next) match = false;
if (next && i != s) match = next->check(s);
string temp = i;
transform(temp.begin(), temp.end(), temp.begin(), ::tolower);
if (temp == s) {
return true;
}
return match;
}