-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLinkedList.h
91 lines (89 loc) · 1.8 KB
/
LinkedList.h
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
#pragma once
/*
AFrame - Arduino framework library for ASensor and AWind libraries
Copyright (C)2014-2018 Andrei Degtiarev. All right reserved
You can always find the latest version of the library at
https://github.com/AndreiDegtiarev/AFrame
This library is free software; you can redistribute it and/or
modify it under the terms of the MIT license.
Please see the included documents for further information.
*/
#include "Log.h"
///Internal class, implements list entry container.
template <class T> class LinkedEntry
{
template<class U> friend class LinkedList;
LinkedEntry* Next; //!<Pointer to the next list item
T* Item; //!<Pointer to the list element
LinkedEntry(T* item)
{
Item = item;
Next=NULL;
}
};
///Implements list container. Examples can be found in ASensor and AWind libraries
template <class T> class LinkedList
{
LinkedEntry<T> * Head; //!<Pointer to the first item
int _count; //!<Number of list items
public:
LinkedList()
{
Head=NULL;
_count=0;
};
///Copy constructor
/*LinkedList(LinkedList &srcList)
{
Head=NULL;
_count=0;
for(int i=0;i<srcList.Count();i++)
Add(srcList[i]);
};*/
///Adds new element to the list
void Add(T *item)
{
LinkedEntry<T> *new_entry=new LinkedEntry<T>(item);
if(Head!=NULL)
{
LinkedEntry<T> *cur= Head;
while(cur->Next!=NULL)
{
cur=cur->Next;
}
cur->Next = new_entry;
}
else
{
Head = new_entry;
}
_count++;
}
///Returns numbr of list items
int Count()
{
return _count;
}
///Implements [] operator
T* operator[](int pos)
{
if(pos>=_count)
{
out<<F("Error: index is too big: ")<<pos<<endln;
return NULL;
}
else
{
int index=0;
LinkedEntry<T> *cur= Head;
while(cur!=NULL)
{
if(index == pos)
return cur->Item;
cur=cur->Next;
index++;
}
}
return NULL;
}
};