📄 linkedlist.cpp
字号:
/*Copyright (c) 2004 baysideAll rights reserved.Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditionsare met:1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS ORIMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIESOF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUTNOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANYTHEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OFTHIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/#include "baygui.h"/** 僐儞僗僩儔僋僞 */LinkedList::LinkedList(){ dataListLength = 0; firstItem = NULL; endItem = NULL;}/** 僨僗僩儔僋僞 */LinkedList::~LinkedList(){ removeAll();}/** 儕僗僩偺挿偝傪摼傞 */int LinkedList::getLength(){ return dataListLength;}/** 巜掕偟偨弴斣偺崁栚傪摼傞 */LinkedItem *LinkedList::getItem(int index){ // NULL僠僃僢僋 if (index >= dataListLength) return NULL; if (firstItem == NULL) return NULL; // 嵟弶偲嵟屻僠僃僢僋
if (index == 0) return firstItem; if (index == dataListLength - 1) return endItem; // index夞儖乕僾 LinkedItem *item = firstItem; for (int i = 0; i < index; i++) { item = item->next; } return item;}/** 捛壛 */void LinkedList::add(LinkedItem *item){ if (item == NULL) {
return;
} else if (firstItem == NULL) {
firstItem = item;
endItem = item;
} else {
LinkedItem *s = firstItem;
while (s->next != NULL) {
s = s->next;
}
item->prev = s;
s->next = item;
endItem = item;
} dataListLength++;
}/** 嶍彍 */void LinkedList::remove(LinkedItem *item){ if (item == NULL) {
return;
} else if (item->next == NULL) {
if (item->prev != NULL) {
endItem = item->prev;
endItem->next = NULL;
} else {
firstItem = NULL;
endItem = NULL;
}
item->prev = NULL;
item->next = NULL; delete(item);
} else {
if (item->prev != NULL) {
item->prev->next = item->next;
item->next->prev = item->prev;
} else {
firstItem = item->next;
firstItem->prev = NULL;
}
item->prev = NULL;
item->next = NULL; delete(item);
} dataListLength--;
}/** 慡嶍彍 */void LinkedList::removeAll(){ while (firstItem != NULL) {
remove(firstItem);
} dataListLength = 0;
}/** 巜掕偟偨崁栚傪堦斣嵟屻偵帩偭偰偄偔 */void LinkedList::sort(LinkedItem *item){ if (item == NULL) {
return;
} else if (item->next == NULL) {
return;
} else {
if (item->prev != NULL) {
item->prev->next = item->next;
item->next->prev = item->prev;
} else {
firstItem = item->next;
firstItem->prev = NULL;
}
item->prev = endItem;
endItem->next = item;
item->next = NULL;
endItem = item;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -