-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode.cpp
62 lines (56 loc) · 1.26 KB
/
node.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
#include <iostream>
using namespace std;
struct node
{
const char*contents;
node*last;
node*next;
};
bool stringEqual(const char * subject, const char * test)
{
int i = 0, len = 0;
while(test[len] != '\0') ++len;
while(test[i] == subject[i] && test[i] != '\0') ++i;
if (len == i) return true;
else return false;
}
node recurse (node cur, const char * c)
{
int i = 0;
cout << cur.contents <<" "<< c << endl;
//cout << stringEqual(c,cur.contents) <<endl;
if ( stringEqual(c,cur.contents) )
{
//this node's contents match
cout << "Match found" << endl;
cur.last->next = cur.next;
return cur;
}
else
{
//this node's contents do not match
if (cur.next != NULL)
return recurse(*cur.next, c);
else return *cur.last;
}
}
int main()
{
node * Nodes[5];
const char * fruits[5] = {"apple", "pear", "orange", "banana", "plum"};
for(int i = 0; i < 5; ++i){
Nodes[i] = new node;
Nodes[i]->contents = fruits[i];
}
for(int i = 0; i < 4; ++i){
Nodes[i]->next = &*Nodes[i + 1];
}
Nodes[4]->next = NULL;
Nodes[0]->last = NULL;
for(int i = 1; i < 4; ++i){
Nodes[i]->last = &*Nodes[i - 1];
}
node newnode = recurse(*Nodes[0],"orange");
cout << "Found a match: " << newnode.contents << endl;
return 0;
}