-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4_6-8_linked_list_string.cpp
228 lines (175 loc) · 4.19 KB
/
4_6-8_linked_list_string.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
struct listNode {
char ch;
listNode * next;
};
typedef listNode * stringLinkedList;
void append(stringLinkedList& str, char ch);
char characterAt(stringLinkedList str, int pos);
void printString(stringLinkedList str);
void concatenate(stringLinkedList &str1, stringLinkedList str2);
void removeChars(stringLinkedList &str, int startIdx, int nrOfChars);
void removeLinkedList(stringLinkedList &head)
int main()
{
stringLinkedList str = nullptr;
append(str, 'T');
append(str, 'e');
append(str, 's');
append(str, 't');
printString(str);
printf("\nCharacter at index %d in \"Test\" is %c \n", 2, characterAt(str, 2));
stringLinkedList str2 = nullptr;
//stringLinkedList str1 = nullptr;
append(str2, 'b');
append(str2, 'e');
append(str2, 'd');
printString(str2);
concatenate(str, str2);
cout << "\nNew concatenated string: " << endl;
printString(str);
cout << "\n" << (void*) str << " " << (void*)str2 << endl;
int startIdx = 1;
int nrOfChars = 2;
removeChars(str, startIdx, nrOfChars);
printf("Removed %d characters starting from index %d running removeChars() function:\n", nrOfChars, startIdx);
printString(str);
removeLinkedList(str);
removeLinkedList(str2);
cin.get();
return 0;
}
void append(stringLinkedList& str, char ch)
{
listNode * newNode = new listNode;
newNode->next = nullptr;
newNode->ch = ch;
if (str == nullptr)
{
str = newNode;
}
else
{
listNode * loopPtr = str;
while (loopPtr->next != nullptr)
{
loopPtr = loopPtr->next;
}
loopPtr->next = newNode;
}
}
void printString(stringLinkedList str)
{
listNode * loopPtr = str;
while (loopPtr != nullptr)
{
cout << loopPtr->ch;
loopPtr = loopPtr->next;
}
}
char characterAt(stringLinkedList str, int pos)
{
int cnt = 0;
listNode * loopPtr = str;
while (loopPtr != nullptr && cnt < pos)
{
loopPtr = loopPtr->next;
cnt++;
}
if (cnt == pos)
{
return loopPtr->ch;
}
else
{
return 0;
}
}
// Exercise 4.8 remove multiple characters from linked list strings.
void removeChars(stringLinkedList &str, int startIdx, int nrOfChars)
{
// Find start index
listNode * loopPtr = str;
if (loopPtr == nullptr)
{
return;
}
int posCounter = 0;
int deleteCounter = 0;
listNode * tempPtr = loopPtr;
while (loopPtr != nullptr && posCounter < startIdx)
{
tempPtr = loopPtr;
loopPtr = loopPtr->next;
posCounter++;
}
//loopPtr = loopPtr->next;
listNode * deletePtr;
// Remove the chars until end
if (posCounter == startIdx)
{
while (loopPtr != nullptr && deleteCounter < nrOfChars)
{
deletePtr = loopPtr;
loopPtr = loopPtr->next;
delete deletePtr;
deleteCounter++;
}
if (startIdx == 0)
{
str = loopPtr;
}
else
{
tempPtr->next = loopPtr;
}
}
}
// Exercise 4.7 concatenate linked list strings.
void concatenate(stringLinkedList &str1, stringLinkedList str2)
{
// Loop to tail
listNode * loopPtr = str1;
listNode * loopPtr2 = str2;
if (loopPtr != nullptr)
{
while (loopPtr->next != nullptr)
{
loopPtr = loopPtr->next;
}
}
else
{
if (str2 != nullptr)
{
str1 = new listNode;
str1->ch = str2->ch;
loopPtr = str1;
loopPtr2 = str2->next;
}
}
while (loopPtr2 != nullptr)
{
loopPtr->next = new listNode;
loopPtr = loopPtr->next;
loopPtr->ch = loopPtr2->ch;
loopPtr->next = nullptr;
loopPtr2 = loopPtr2->next;
}
}
void removeLinkedList(stringLinkedList &head)
{
listNode * loopPtr = head;
listNode * deleteNode;
while (loopPtr != nullptr)
{
deleteNode = loopPtr;
loopPtr = loopPtr->next;
delete deleteNode;
}
head = nullptr;
return;
}