-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDeleteDuplicates.cpp
More file actions
118 lines (103 loc) · 3.24 KB
/
DeleteDuplicates.cpp
File metadata and controls
118 lines (103 loc) · 3.24 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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
// Example of removing duplicates in a linked-list (as seen in class).
// This code is not ideal, it is for illustration purposes.
// You are strongly recommended to create Classes when you write your own programs.
struct EmailType {
int id;
string too;
EmailType * link;
};
void print(EmailType * list) {
EmailType * current = list;
while(current) {
cout << "ID: " << current->id << "\t" << current->too << endl;
current = current->link;
}
}
// we saw this in class
void deleteNodesById(int id, EmailType * list) {
EmailType * lastVisited = list;
while(lastVisited) {
if(lastVisited->link) {
if( id == lastVisited->link->id ) {
// found a duplicate, update the links, delete it
EmailType * temp = lastVisited->link;
lastVisited->link = lastVisited->link->link;
delete temp;
}
else {
lastVisited = lastVisited->link;
}
}
else {
lastVisited = NULL;
}
}
}
// we saw this in class
void deleteDuplicates(EmailType * list) {
EmailType * current = list;
while(current) {
deleteNodesById(current->id, current);
current = current->link;
}
}
int main()
{
// initialize a list, we want 'head' to always point to the first element of the list
EmailType * head = 0;
// Step 1. add a new element (directly) to the list
head = new EmailType;
head->id = 1;
head->too = string("one");
head->link = NULL;
// print the list
cout << endl << "List at step 1" << endl;
print(head);
// Step 2. add another element to the list (manually to the end of the list)
head->link = new EmailType;
head->link->id = 2;
head->link->too = string("two");
head->link->link = 0; // zero here is equivalent to NULL
cout << endl << "List at step 2" << endl;
print(head);
// Step 3. add yet another element to the list (manually at the begining of the list)
EmailType * node = new EmailType;
node->id = 3;
node->too = string("three");
node->link = head; // connect the node to the list
head = node; // the head of the list now points to the (newly created) node
cout << endl << "List at step 3" << endl;
print(head);
// Step 4: similar to previous step, but add a few duplicates (at begining of the list)
// The variable 'node' is just re-used
for( int i = 0; i < 3; i++ ) {
node = new EmailType;
node->id = 2;
node->too = string("twos");
node->link = head; // connect the node to the list
head = node; // the head of the list now points to the (newly created) node
}
cout << endl << "List at step 4" << endl;
print(head);
// Step 5. add another '3' element to the list to make things interesting (manually at the begining of the list)
node = new EmailType;
node->id = 3;
node->too = string("three");
node->link = head; // connect the node to the list
head = node; // the head of the list now points to the (newly created) node
cout << endl << "List at step 5" << endl;
print(head);
// delete duplicates
deleteDuplicates(head);
cout << endl << "List after delete duplicates" << endl;
print(head);
// clean up memory from last node to firs (because I'm not writing a method to do it via a while loop)
// and I know for sure that there are only three nodes at this point in the list.
delete head->link->link;
delete head->link;
delete head;
}