-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.cpp
More file actions
70 lines (60 loc) · 1.71 KB
/
Copy pathNode.cpp
File metadata and controls
70 lines (60 loc) · 1.71 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
// implementation of Node class
// Author: Ali Selcuk AKYUZ
// Mail: selcuk@retinarobotics.com || e174043@metu.edu.tr
// Electrical and Electronics Engineering Department
// Middle East Technical University - ANKARA
// If any questions please send me an email
#include "Node.h"
template<class T>
Node<T>::Node()
{
// default constructor
// this is to allow us to create an object without any initialization
}
// This constructor is just to set next pointer of a node and the data contained.
template<class T>
Node<T>::Node(const T& item,Node<T>* ptrnext)
{
this->data = item;
this->next = ptrnext;
}
template<class T>
Node<T>*Node<T>::NextNode()
{
return this->next;
}
// This methods inserts a node just after the node that the method belongs to
// TO-DO: Consider a better implementation
template<class T>
void Node<T>::InsertAfter(Node<T> *p)
{
// not to lose the rest of the list, we ought to link the rest of the
// list to the Node<T>* p first
p->next = this->next;
// now we should link the previous Node to Node<T> *p , i.e the Node that we are
//inserting after,
this->next = p;
}
// Deletes the node from the list and returns the deleted node
template<class T>
Node<T>* Node<T>::DeleteAfter()
{
// store the next Node in a temporary Node
Node<T>* tempNode = next;
// check if there is a next node
if(next != NULL)
next = next->next;
return tempNode;
}
template<class T>
Node<T> * GetNode(const T& item, Node<T>* nextptr = NULL)
{
Node<T>* newnode; // Local ptr for new node
newnode = new Node<T>(item,nextptr);
if ( newnode == NULL)
{
cerr << "Memory allocation failed." << endl;
exit(1);
}
return newnode;
}