-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.h
96 lines (90 loc) · 3.07 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
92
93
94
95
96
#ifndef A_LINKEDLIST_H
#define A_LINKEDLIST_H
#include <iostream>
namespace Abuelfateh {
typedef enum linkedListByte {
// to select the required function in copy|move
START = 0x00,
END = 0x01,
AFTER = 0x02,
BEFORE = 0x03,
ORDERED = 0x04,
// to be used with insertOrdered
ASC = 0x05,
DESC = 0x06
} LLByte;
template <typename T, typename K>
class LinkedList {
public:
LinkedList(LLByte defInsert = ORDERED);
~LinkedList();
// insert new node at the start of the list,
// key argument is defined to be used internaly by insertOrdered
void insertStart(const T &data, K key = NULL);
// insert new node at the end of the list
void insertEnd(const T &data);
// insert new node after the current cursor
void insertAfter(const T &data);
// insert new node before the current cursor
// key argument is defined to be used internaly by insertOrdered
void insertBefore(const T &data, K key = NULL);
// insert new node in ordered form
// generally in ascending order, or LL_DESC to insert in descending order (useful for Stack implementation)
void insertOrdered(const T &data, const K key, LLByte order = ASC);
// Short hand to insert using the default insert method
void insert(const T &data, K key = NULL, LLByte order = ASC);
// check if the list is empty return true
bool isEmpty();
// clear the list, make it empty
void clear();
// remove the node in current position
void remove();
// set the cursor to the first element
void toStart();
// set the cursor to the last element
void toEnd();
// advance the cursor forward by one element
void advance();
// advance the cursor backward by one element
void goBack();
// check if there is next element, to be used in a loop
bool hasNext();
// check if there is data in the current element
bool hasData();
// move the cursur to specific key of to the end of the list => (cur = NULL)
void toKey(K key);
// get the data in the current cursor position
T * getData() const;
// overloaded to return the data in a buffer
bool getData(T &buffer);
// return the current key value
K getKey() const;
// get the length of the list
int getLength() const;
// Update current data
void update(const T &data);
// Reverse the list values
void reverse();
// Copy current data in another LinkedList
void copy(LinkedList<T, K> &list, K key, int fn = LL_ORDERED, LLByte order = ASC);
// Move current data in another LinkedList
void move(LinkedList<T, K> &list, K key, int fn = LL_ORDERED, LLByte order = ASC);
private:
struct Node {
K key;
T data;
Node *next;
} *head, *tail, *cur, *prev;
int length;
// set the default insert function
LLByte defInsert;
// private method to reset all control variables
void _init();
// create new node, here we separate the creation of the new node to be able to
// handle memory allocation error once in all insert* functions
Node *_createNode(const T &data);
// separate first node creation as it repeated in most insert* functions
void _insertFirstNode(Node *n);
};//EOC
}//EONs
#endif // !A_LINKEDLIST_H