|
1 /* |
|
2 * Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <stdlib.h> |
|
20 #include <assert.h> |
|
21 #include "LINKLIST.H" |
|
22 |
|
23 // LinkedListIterator |
|
24 |
|
25 LinkedListIterator::LinkedListIterator(ListItem* aItem): |
|
26 iCurrentItem(aItem) |
|
27 {} |
|
28 |
|
29 LinkedListIterator::LinkedListIterator(const LinkedList& aList): |
|
30 iCurrentItem(aList.iHead) |
|
31 {} |
|
32 |
|
33 ListItem* LinkedListIterator::operator() () |
|
34 { |
|
35 ListItem* p = iCurrentItem; |
|
36 if(iCurrentItem) |
|
37 iCurrentItem = iCurrentItem->iNext; |
|
38 return p; |
|
39 } |
|
40 |
|
41 void LinkedListIterator::Reset() |
|
42 { |
|
43 iCurrentItem = iList->iHead; |
|
44 } |
|
45 |
|
46 // ListItem |
|
47 |
|
48 ListItem::ListItem(): |
|
49 iNext(NULL) |
|
50 {} |
|
51 |
|
52 ListItem::~ListItem() |
|
53 {} |
|
54 |
|
55 // LinkedList |
|
56 |
|
57 LinkedList::LinkedList(): |
|
58 iHead(NULL), |
|
59 iTail(NULL) |
|
60 {} |
|
61 |
|
62 LinkedList::~LinkedList() |
|
63 {} |
|
64 |
|
65 void LinkedList::AddToHead(ListItem* aNewItem) |
|
66 { |
|
67 if(iHead) |
|
68 aNewItem->iNext = iHead; |
|
69 else |
|
70 iTail = aNewItem; |
|
71 iHead = aNewItem; |
|
72 } |
|
73 |
|
74 void LinkedList::AddToTail(ListItem* aNewItem) |
|
75 { |
|
76 if(iTail) |
|
77 iTail->iNext = aNewItem; |
|
78 else |
|
79 iHead = aNewItem; |
|
80 iTail = aNewItem; |
|
81 } |
|
82 |
|
83 void LinkedList::DeleteAll() |
|
84 { |
|
85 ListItem* item = iHead; |
|
86 while(item) |
|
87 { |
|
88 ListItem* p = item; |
|
89 item=item->iNext; |
|
90 delete p; |
|
91 } |
|
92 iHead = NULL; |
|
93 iTail = NULL; |
|
94 } |
|
95 |
|
96 void LinkedList::AddAfter(ListItem* aExistingItem,ListItem* aNewItem) |
|
97 { |
|
98 if(aExistingItem==NULL) |
|
99 AddToHead(aNewItem); |
|
100 else if(aExistingItem == iTail) |
|
101 AddToTail(aNewItem); |
|
102 else |
|
103 { |
|
104 aNewItem->iNext = aExistingItem->iNext; |
|
105 aExistingItem->iNext = aNewItem; |
|
106 } |
|
107 } |
|
108 |
|
109 ListItem* LinkedList::Previous(ListItem* aItem) |
|
110 { |
|
111 assert(aItem != NULL); |
|
112 assert(iHead != NULL); |
|
113 ListItem* p = iHead; |
|
114 if(p == aItem) |
|
115 return NULL; |
|
116 while(p->iNext != aItem && p->iNext != NULL) |
|
117 p = p->iNext; |
|
118 assert(p->iNext == aItem); |
|
119 return p; |
|
120 } |
|
121 |
|
122 void LinkedList::RemoveItem(ListItem* aItem) |
|
123 { |
|
124 assert(aItem != NULL); |
|
125 assert(iHead != NULL); |
|
126 // If item to remove is at head of list. |
|
127 if (iHead == aItem) |
|
128 { |
|
129 if(iTail == aItem) // Item being removed is only item in list |
|
130 iTail = NULL; |
|
131 iHead = aItem->iNext; |
|
132 return; |
|
133 } |
|
134 // Search through the list for the item. |
|
135 ListItem* p = iHead; |
|
136 while( p && (p->iNext!=aItem)) |
|
137 p = p->iNext; |
|
138 assert(p!=NULL); |
|
139 if (iTail == p->iNext) |
|
140 iTail = p; |
|
141 p->iNext = p->iNext->iNext; |
|
142 } |
|
143 |
|
144 ListItem* LinkedList::TailItem() |
|
145 { |
|
146 return iTail; |
|
147 } |
|
148 |
|
149 int LinkedList::IsEmpty() |
|
150 { |
|
151 return(iHead==NULL); |
|
152 } |