-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.h
More file actions
154 lines (127 loc) · 2.83 KB
/
LinkedList.h
File metadata and controls
154 lines (127 loc) · 2.83 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//
// Created by nikstarling on 3/4/20.
//
#ifndef PARSER_LINKEDLIST_H
#define PARSER_LINKEDLIST_H
#include <iostream>
#include <cmath>
template <class T>
struct Node
{
T data;
Node *next;
void delete_next()
{
if(this->next != NULL)
{
if(this->next->next != NULL)
{
Node *target = this->next;
this->next = target->next;
delete target;
}
}
}
};
template <class T>
class LinkedList
{
private:
Node<T> *head, *tail, *current;
unsigned int size;
public:
void create_node(T data);
void delete_node(int id);
unsigned int get_size(){return this->size;}
Node<T>* get_next(){return this->current->next;}
Node<T>* get_current(){return this->current;}
void set_next();
Node<T>* get_head(){return this->head;}
void set_head(){this->current = this->head;}
void shrink(){this->size -= 1;}
LinkedList();
~LinkedList();
template <class U>
friend std::ostream& operator<<(std::ostream &out, LinkedList<U> &a);
};
template<class T>
void LinkedList<T>::set_next()
{
if(this->current->next != NULL)
this->current = this->current->next;
}
template <class T>
void LinkedList<T>::delete_node(int id)
{
this->current = this->head;
Node<T> *target;
if(id == 0)
{
target = this->head;
this->head = this->head->next;
} else {
for (int i = 0; i < id - 1; ++i) {
this->current = this->current->next;
}
target = this->current->next;
if (id != this->size - 1)
{
this->current->next = target->next;
} else {
this->current->next = NULL;
}
}
delete target;
this->size--;
this->current = this->head();
}
template <class U>
std::ostream& operator<<(std::ostream &out, LinkedList<U> &a)
{
a.current = a.head;
while(a.current != NULL)
{
out << a.current->data << " ";
a.current = a.current->next;
}
return out;
}
template <class T>
LinkedList<T>::LinkedList()
{
this->size = 0;
this->head = NULL;
this->tail = NULL;
this->current = NULL;
}
template <class T>
LinkedList<T>::~LinkedList()
{
Node<T> *previous = head;
while(this->current != NULL)
{
previous = this->current;
this->current = this->current->next;
delete previous;
}
}
template <class T>
void LinkedList<T>::create_node(T data)
{
Node<T> *node = new Node<T>;
node->data = data;
node->next = NULL;
if(this->head == NULL)
{
this->head = node;
this->tail = node;
node = NULL;
this->current = this->head;
} else {
this->tail->next = node;
this->tail = node;
}
this->size++;
this->current = this->head;
}
#endif //PARSER_LINKEDLIST_H