-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLinkedList++.hpp
134 lines (120 loc) · 3.69 KB
/
LinkedList++.hpp
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
//*******************************//
// Made By: Vedant Paranjape //
// LinkedList++ //
// 18 December 2017 //
// 16:55 IST //
//*******************************//
////////////// class definiton //////////////////////////////////////////////
template<typename T>
class LinkedList{ //linkedlist class
private: //private data members
struct node //definiton of structure node
{
T data; //data variable
node* next; //next node pointer
};
node* h; //pointer pointing to head of linked list
node* n; //pointer pointing to newly created node
node* t;
node* createnode(T data); //function to create new node, pack them with data and return the pointer to the location
int size_ll; //variable to store size of linked list
public:
LinkedList(); //constructor
void add(T data); //function prototype to add new node to the linked list
T& operator[](int location); //operator overloading to access the contents of linked list just like a array
void del(int location); //function to delete the node
int size(); //function to return the size of linked list
};
////////////////////////////////////////////////////////////////////////////
/////////////////// constructor //////////////////////////////////////
template<typename T>
LinkedList<T>::LinkedList()
{
h = nullptr; //intialize the values of the variables
n = nullptr;
t = nullptr;
size_ll = 0;
}
/////////////////////////////////////////////////////////////////////
///////////////// create node function /////////////////////////////
template<typename T>
typename LinkedList<T>::node* LinkedList<T>::createnode(T data)
{
n = new node; //create new node
n->data = data; //set the data
n->next = nullptr;
return n; //return the pointer of the new node
}
////////////////////////////////////////////////////////////////////
////////////// add node ///////////////////////////////////////////
template<typename T>
void LinkedList<T>::add(T data)
{
if(h == nullptr)
{
h = createnode(data);
t = h;
size_ll++;
}
else
{
t->next = createnode(data);
t = t->next;
size_ll++;
}
}
////////////////////////////////////////////////////////////////////
//////////// operator overloaded function to return the value /////
template<typename T>
T& LinkedList<T>::operator[](int location)
{
int counter = 0;
node* s = nullptr;
s = h;
T* returnval;
while(s != nullptr)
{
counter++;
if((counter-1) == location)
{
returnval = &s->data;
}
s = s->next;
}
return *returnval;
}
//////////////////////////////////////////////////////////////////
///////////// delete node ///////////////////////////////////////
template<typename T>
void LinkedList<T>::del(int location)
{
int counter = 0;
node* d = nullptr;
node* d_next = nullptr;
d = h;
if(location == 0)
{
h = h->next;
delete d;
}
while(d != nullptr)
{
counter++;
if(counter == location)
{
d_next = d->next;
d->next = d_next->next;
delete d_next;
}
d = d->next;
}
size_ll--;
}
///////////////////////////////////////////////////////////////////
///////////// return linked list size /////////////////////////////
template<typename T>
int LinkedList<T>::size()
{
return size_ll;
}
//////////////////////////////////////////////////////////////////