-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.h
More file actions
140 lines (120 loc) · 2.92 KB
/
Copy pathnode.h
File metadata and controls
140 lines (120 loc) · 2.92 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
#ifndef Node_H
#define Node_H
#include <cassert>
#include <iostream>
using namespace std;
/************************************************
* Node
* Data Structure that implements a linked-list.
***********************************************/
template <class T>
class Node
{
public:
T data; // holds the data
Node<T>* pNext; // points to the next node
// default constructor : empty and kinda useless
Node() : pNext(NULL) {};
// non-default constructor : create a new node with the data
Node(T nData) { this->data = nData; pNext = NULL; }
};
/***************************************************
* Node :: copy
* Receives a node, copys it, and returns the copy
**************************************************/
template <class T>
Node <T> * copy(Node <T> *node)
{
Node<T> *copy = new Node<T>;
copy->data = node->data;
copy->pNext = node->pNext;
Node<T> *head = copy; // We do not loop through yet because we want to keep track of the head
node = node->pNext;
// Now we can loop through
while(node)
{
copy->pNext = new Node<T>;
copy = copy->pNext;
copy->data = node->data;
node = node->pNext;
}
// return the head of the node and not anything inbetween
// otherwise we lose part of the list
return head;
}
/***************************************************
* Node :: INSERT
* Insert a node inside of the linked-list
**************************************************/
template <class T>
void insert(T nData, Node<T> *&prev, bool head = false)
{
// this is the node that will be inserted
Node <T>* nNode = new Node<T>(nData);
if (head || prev == NULL)
{
nNode->pNext = prev;
prev = nNode;
}
else
{
nNode->pNext = prev->pNext;
prev->pNext = nNode;
}
}
/***************************************************
* Node :: find
* Takes a template parameter and returns an node
* with the item in the linked-list. If the item is
* not found, it returns NULL.
**************************************************/
template <class T>
Node <T> * find(Node<T>* head, const T & get)
{
while(head != NULL)
{
if (head->data == get)
return head;
else
head = head->pNext;
}
return head;
}
/***************************************************
* Node :: freeData
* Takes a Node parameter and will recursively free
* up all the space in the linked list.
**************************************************/
template <class T>
void freeData(Node<T> *&n)
{
if (n != NULL) // if there's data, delete
{
freeData(n->pNext);
delete n;
n = NULL;
}
else
{
return; // anchor
}
}
/******************************************
* NODE :: insertion operator
* Display node data
*****************************************/
template <class T>
ostream & operator << (ostream & out, const Node <T> *node)
{
while(node)
{
out << node->data;
if (node->pNext)
{
out << ", ";
}
node = node->pNext;
}
return out;
}
#endif // Node_H