diff options
| author | Mike Buland <eichlan@xagasoft.com> | 2007-04-03 03:49:53 +0000 |
|---|---|---|
| committer | Mike Buland <eichlan@xagasoft.com> | 2007-04-03 03:49:53 +0000 |
| commit | f4c20290509d7ed3a8fd5304577e7a4cc0b9d974 (patch) | |
| tree | 13cdf64f7cf134f397a7165b7a3fe0807e37026b /src/old/linkedlist.cpp | |
| parent | 74d4c8cd27334fc7204d5a8773deb3d424565778 (diff) | |
| download | libbu++-f4c20290509d7ed3a8fd5304577e7a4cc0b9d974.tar.gz libbu++-f4c20290509d7ed3a8fd5304577e7a4cc0b9d974.tar.bz2 libbu++-f4c20290509d7ed3a8fd5304577e7a4cc0b9d974.tar.xz libbu++-f4c20290509d7ed3a8fd5304577e7a4cc0b9d974.zip | |
Ok, no code is left in src, it's all in src/old. We'll gradually move code back
into src as it's fixed and re-org'd. This includes tests, which, I may write a
unit test system into libbu++ just to make my life easier.
Diffstat (limited to 'src/old/linkedlist.cpp')
| -rw-r--r-- | src/old/linkedlist.cpp | 210 |
1 files changed, 210 insertions, 0 deletions
diff --git a/src/old/linkedlist.cpp b/src/old/linkedlist.cpp new file mode 100644 index 0000000..a9902bc --- /dev/null +++ b/src/old/linkedlist.cpp | |||
| @@ -0,0 +1,210 @@ | |||
| 1 | #include "linkedlist.h" | ||
| 2 | |||
| 3 | LinkedList::LinkedList( ) | ||
| 4 | { | ||
| 5 | pBase = NULL; | ||
| 6 | pTop = NULL; | ||
| 7 | pLast = NULL; | ||
| 8 | nSize = 0; | ||
| 9 | nLast = -1; | ||
| 10 | } | ||
| 11 | |||
| 12 | LinkedList::~LinkedList( ) | ||
| 13 | { | ||
| 14 | /* | ||
| 15 | Link *pCur = pBase; | ||
| 16 | while( pCur ) | ||
| 17 | { | ||
| 18 | Link *pLast = pCur; | ||
| 19 | pCur = pCur->pNext; | ||
| 20 | delete pLast; | ||
| 21 | } | ||
| 22 | */ | ||
| 23 | empty(); | ||
| 24 | } | ||
| 25 | |||
| 26 | void *LinkedList::getAt( int index ) | ||
| 27 | { | ||
| 28 | if( index < 0 || index >= nSize ) | ||
| 29 | return NULL; | ||
| 30 | |||
| 31 | return getPtrTo( index )->pData; | ||
| 32 | } | ||
| 33 | |||
| 34 | void LinkedList::append( void *data ) | ||
| 35 | { | ||
| 36 | if( pBase == NULL ) | ||
| 37 | { | ||
| 38 | pBase = new Link( data ); | ||
| 39 | pTop = pBase; | ||
| 40 | nSize++; | ||
| 41 | } | ||
| 42 | else | ||
| 43 | { | ||
| 44 | pTop->pNext = new Link( data ); | ||
| 45 | pTop = pTop->pNext; | ||
| 46 | nSize++; | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | void LinkedList::insertBefore( void *data, int pos ) | ||
| 51 | { | ||
| 52 | if( pos < 0 || pos > nSize ) | ||
| 53 | return; | ||
| 54 | |||
| 55 | if( pos == 0 ) | ||
| 56 | { | ||
| 57 | Link *pTmp = new Link( data, pBase ); | ||
| 58 | if( pBase == NULL ) | ||
| 59 | { | ||
| 60 | pTop = pTmp; | ||
| 61 | } | ||
| 62 | pBase = pTmp; | ||
| 63 | if( nLast >= 0 ) nLast++; | ||
| 64 | nSize++; | ||
| 65 | } | ||
| 66 | else | ||
| 67 | { | ||
| 68 | Link *pCur; | ||
| 69 | if( (pCur = getPtrTo( pos-1 )) == NULL ) | ||
| 70 | { | ||
| 71 | return; | ||
| 72 | } | ||
| 73 | Link *pNew = new Link( data, pCur->pNext ); | ||
| 74 | pCur->pNext = pNew; | ||
| 75 | if( pNew->pNext == NULL ) | ||
| 76 | { | ||
| 77 | pTop = pNew; | ||
| 78 | } | ||
| 79 | if( nLast >= pos ) nLast++; | ||
| 80 | nSize++; | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | int LinkedList::getSize( ) | ||
| 85 | { | ||
| 86 | return nSize; | ||
| 87 | } | ||
| 88 | |||
| 89 | bool LinkedList::isEmpty( ) | ||
| 90 | { | ||
| 91 | if( nSize == 0 ) | ||
| 92 | return true; | ||
| 93 | return false; | ||
| 94 | } | ||
| 95 | |||
| 96 | void LinkedList::deleteAt( int index ) | ||
| 97 | { | ||
| 98 | if( index >= nSize || | ||
| 99 | pBase == NULL ) | ||
| 100 | return; | ||
| 101 | |||
| 102 | if( index == 0 ) | ||
| 103 | { | ||
| 104 | Link *pTmp = pBase->pNext; | ||
| 105 | delete pBase; | ||
| 106 | pBase = pTmp; | ||
| 107 | if( nLast >= 0 ) nLast--; | ||
| 108 | nSize--; | ||
| 109 | if( pBase == NULL ) | ||
| 110 | { | ||
| 111 | pTop = NULL; | ||
| 112 | } | ||
| 113 | else if( pBase->pNext == NULL ) | ||
| 114 | { | ||
| 115 | pTop = pBase; | ||
| 116 | } | ||
| 117 | } | ||
| 118 | else | ||
| 119 | { | ||
| 120 | Link *pCur = getPtrTo( index-1 ); | ||
| 121 | if( pCur->pNext == pTop ) | ||
| 122 | { | ||
| 123 | pTop = pCur; | ||
| 124 | } | ||
| 125 | Link *pTmp; | ||
| 126 | if( pCur->pNext == NULL ) | ||
| 127 | { | ||
| 128 | pTmp = NULL; | ||
| 129 | } | ||
| 130 | else | ||
| 131 | { | ||
| 132 | pTmp = pCur->pNext->pNext; | ||
| 133 | } | ||
| 134 | delete pCur->pNext; | ||
| 135 | pCur->pNext = pTmp; | ||
| 136 | if( nLast == index ) nLast = -1; | ||
| 137 | else if( index < nLast ) nLast--; | ||
| 138 | nSize--; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | void LinkedList::empty() | ||
| 143 | { | ||
| 144 | while( nSize > 0 ) | ||
| 145 | { | ||
| 146 | deleteAt( 0 ); | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | void LinkedList::setSize( int newSize ) | ||
| 151 | { | ||
| 152 | if( newSize < nSize ) | ||
| 153 | { | ||
| 154 | // Delete items off of the end of the list. | ||
| 155 | while( nSize > newSize ) | ||
| 156 | { | ||
| 157 | deleteAt( nSize-1 ); | ||
| 158 | } | ||
| 159 | } | ||
| 160 | else | ||
| 161 | { | ||
| 162 | // Add null items to the end of the list. | ||
| 163 | while( nSize < newSize ) | ||
| 164 | { | ||
| 165 | append( NULL ); | ||
| 166 | } | ||
| 167 | } | ||
| 168 | } | ||
| 169 | |||
| 170 | void LinkedList::setAt( int index, void *data ) | ||
| 171 | { | ||
| 172 | if( index >= nSize || index < 0 ) | ||
| 173 | return; | ||
| 174 | |||
| 175 | getPtrTo( index )->pData = data; | ||
| 176 | } | ||
| 177 | |||
| 178 | LinkedList::Link *LinkedList::getPtrTo( int index ) | ||
| 179 | { | ||
| 180 | if( index < 0 || index >= nSize ) | ||
| 181 | return NULL; | ||
| 182 | if( index == nLast ) | ||
| 183 | { | ||
| 184 | return pLast; | ||
| 185 | } | ||
| 186 | if( index == 0 ) | ||
| 187 | { | ||
| 188 | pLast = pBase; | ||
| 189 | nLast = 0; | ||
| 190 | return pBase; | ||
| 191 | } | ||
| 192 | else | ||
| 193 | { | ||
| 194 | Link *pCur = pBase; | ||
| 195 | int nCur = 0; | ||
| 196 | if( nLast < index && nLast >= 0 ) | ||
| 197 | { | ||
| 198 | pCur = pLast; | ||
| 199 | nCur = nLast; | ||
| 200 | } | ||
| 201 | while( nCur != index ) | ||
| 202 | { | ||
| 203 | pCur = pCur->pNext; | ||
| 204 | nCur++; | ||
| 205 | } | ||
| 206 | nLast = index; | ||
| 207 | pLast = pCur; | ||
| 208 | return pCur; | ||
| 209 | } | ||
| 210 | } | ||
