-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLList.cpp
More file actions
128 lines (111 loc) · 2.6 KB
/
Copy pathLList.cpp
File metadata and controls
128 lines (111 loc) · 2.6 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
//linked list implementation
template <typename E> class LList: public List<E>
{
private:
Link<E>* head; //pointer to list header
Link<E>* tail; //pointer to last element
Link<E>* curr; //access to current element
int cnt; //size of list
void init() //initialization helper method
{
curr = tail = head = new Link<E>;
cnt = 0;
}
void removeall() //return link nodes to free store
{
while(head != NULL)
{
curr = head;
head = head->next;
delete curr;
}
}
public:
LList(int size=defaultSize) //constructor
{
init();
}
~LList() //destructor
{
removeall();
}
void print() const; //print list contents
void clear() //clear
{
removeall();
init();
}
//insert "it" at current position
void insert(const E& it)
{
curr->next = new Link<E>(it, curr->next);
if (tail == curr) tail = curr->next; //new tail
cnt++;
}
void append(const E& it) //append "it" to list
{
tail = tail->next = new Link<E>(it,NULL);
cnt++;
}
//remove and return current element
E remove()
{
Assert(curr->next != NULL, "no element");
E it = curr->next->element; //remember value
Link<E>* ltemp = curr->next; //remember link node in order to delete
if (tail == curr->next) tail = curr; //reset tail if the last emement is removed
curr->next = curr->next->next; //remoev from list
delete ltemp; //reclaim space
cnt--;
return it;
}
void moveToStart() //place curr at list start
{
curr = head;
}
void moveToEnd() //place curr at list end
{
curr = tail;
}
//move curr one step left; no change if already at front
void prev()
{
if (curr=head) return; //no previous element
Link<E>* temp = head;
//march down list until we find the previous element
while (temp->next!=curr) temp = temp->next;
curr = temp;
}
//move curr one step right; no change if already at end
void next()
{
if (curr!=tail)
curr = curr->next;
}
int length() const //return length
{
return cnt;
}
//return the position of the current element
int currPos() const
{
Link<E>* temp = head;
int i;
for (i=0; curr!=temp;i++)
temp=temp->next;
return i;
}
//move down list to "pos" position
void moveToPos(int pos)
{
Assert((pos>=0)&&(pos<=cnt), "Position out of range");
curr = head;
for(int i=0;i,pos;i++)
curr = curr->next;
}
const E& getValue() const //return current element
{
Assert(curr->next!=NULL, "no element");
return curr->next->element;
}
};