-
Notifications
You must be signed in to change notification settings - Fork 0
/
LLN.cpp
143 lines (102 loc) · 2.59 KB
/
LLN.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
//
// AUTHOR: Cole Walgren
// TITLE: Program 2
// DESCRIPTION: This file handles all the processes involving
// the individual nodes of the linked list.
//
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstring>
#include "LLN.h"
#include "LL.h"
using namespace std;
LLN::LLN(string s, LLN* n) {
//each node has a "next" of type node and "data" of type string
data = s;
next = n;
}
void LLN::print() {
//recursively prints nodes
cout << data << endl;
if (next) next->print();
}
void LLN :: addback(string s) {
//adds node with data "s" to end of list
if (!next) next = new LLN(s, nullptr);
else next->addback(s);
}
void LLN::setnext(LLN* p) {
//sets nodes next to "p"
next = p;
}
LLN::~LLN() {
//destructor method
delete next;
}
LLN* LLN::getnext() {
//returns nodes next
return next;
}
string LLN::getdata() {
//returns nodes data
return data;
}
void LLN::iSortNode(LLN* prev, LL* list){
// recursively sorts through till end of
// list, calls swapTwo if conditions are met
if(!next){
return;
} else {
next->iSortNode(this, list);
}
if(this->getnext() && compare(this, this->getnext())==true){
swapTwo(prev, this, this->getnext(), list);
}
return;
}
bool LLN::compare(LLN* node1, LLN* node2){
// converts each nodes data to lowercase and then compares
// returns true if a > b
// else return false
//convert each to lowercase
string a = node1->getdata();
string b = node2->getdata();
string a2, b2;
for(int i = 0; i < a.length(); i++){
char c = a[i];
if(c > 90) c = c-32; // if not cap, decrease ascii value
a2 = a2 + c;
}
for(int i = 0; i < b.length(); i++){
char d = b[i];
if(d > 90) d = d-32; // if not cap, decrease ascii value
b2 = b2 + d;
}
// if a > b, return true
if(a2 > b2){
return true;
}
return false;
}
void LLN::swapTwo(LLN* prev, LLN* node, LLN* nex, LL* list){
// swaps the position of node and next, adjusting pointers
// accordingly
if(prev){
prev->setnext(nex);
}else{
list->sethead(nex);
}
if(nex->getnext()){
node->setnext(nex->getnext());
}else{
node->setnext(nullptr);
}
nex->setnext(node);
if(node->getnext()){
if(compare(node->getnext(), node)==true){
swapTwo(nex, node, node->getnext(), list);
}
}
return;
}