首页 > 代码库 > C++__双向循环链表(练习)
C++__双向循环链表(练习)
双向循环链表
link.h
#ifndef LINK_H_ #define LINK_H_ #define HEADER 0 #define TAIL -1 typedef int data_type; enum LINK_OP { LINK_ERR = -1, LINK_OK }; class LINK { private: LINK *last; data_type data; LINK *next; public: LINK(); LINK(data_type data); virtual ~LINK(); data_type getData() const; LINK *getLast() const; LINK *getNext() const; void setData(data_type data); void setLast(LINK *last); void setNext(LINK *next); // LINK *CreateList(data_type data); // void DestroyList(LINK *pList); int InsertItem(LINK *pList, data_type tData, int iOffset); int DeleteItem(LINK *pList, data_type *pData, int iOffset); int UpdateItem(LINK *pList, data_type tNew, data_type tOld); int SearchItem(LINK *pList, data_type tData); void ShowList(LINK *pList, int iFlag); void operator delete(void *pList); }; #endif /* LINK_H_ */
link.cpp
#include "LINK.h" #include <iostream> using namespace std; LINK::LINK() { // TODO Auto-generated constructor stub this->setData(0); this->setLast(this); this->setNext(this); } LINK::LINK(data_type data) { // TODO Auto-generated constructor stub this->setData(data); this->setLast(this); this->setNext(this); } LINK::~LINK() { // TODO Auto-generated destructor stub } data_type LINK::getData() const { return data; } LINK *LINK::getLast() const { return last; } LINK *LINK::getNext() const { return next; } void LINK::setData(data_type data) { this->data =http://www.mamicode.com/ data; } void LINK::setLast(LINK *last) { this->last = last; } void LINK::setNext(LINK *next) { this->next = next; } /* LINK *LINK::CreateList(data_type data) { LINK *pList = new LINK(data); if (!pList) return NULL; pList->setLast(pList); pList->setNext(pList); return pList; } void LINK::DestroyList(LINK *pList) { if (!pList) return; LINK *Tmp = pList->getNext(); while (pList != Tmp) { pList->setNext(Tmp->getNext()); delete Tmp; Tmp = pList->getNext(); } delete pList; return; } */ int LINK::InsertItem(LINK *pList, data_type tData, int iOffset) { if ((!pList) || -1 > iOffset) return LINK_ERR; LINK *New = new LINK(tData); if (!New) return LINK_ERR; LINK *Tmp = NULL; switch (iOffset) { case HEADER: Tmp = pList->getNext(); New->setNext(Tmp); Tmp->setLast(New); New->setLast(pList); pList->setNext(New); break; case TAIL: Tmp = pList->getLast(); New->setLast(Tmp); Tmp->setNext(New); New->setNext(pList); pList->setLast(New); break; default: Tmp = pList->getNext(); while (iOffset--) { Tmp = Tmp->getNext(); } New->setNext(Tmp); New->setLast(Tmp->getLast()); Tmp->getLast()->setNext(New); Tmp->setLast(New); break; } return LINK_OK; } int LINK::DeleteItem(LINK *pList, data_type *pData, int iOffset) { if ((!pList) || (!pData) || -1 > iOffset) return LINK_ERR; LINK *Del = NULL; switch (iOffset) { case HEADER: Del = pList->getNext(); *pData = http://www.mamicode.com/Del->getData(); Del->getNext()->setLast(pList); pList->setNext(Del->getNext()); break; case TAIL: Del = pList->getLast(); *pData = http://www.mamicode.com/Del->getData(); Del->getLast()->setNext(pList); pList->setLast(Del->getLast()); break; default: Del = pList->getNext(); while (iOffset--) { Del = Del->getNext(); } if (pList == Del) { cout << "iOffset error" << endl; return LINK_ERR; } *pData = http://www.mamicode.com/Del->getData(); Del->getLast()->setNext(Del->getNext()); Del->getNext()->setLast(Del->getLast()); break; } Del->setNext(NULL); delete Del; return LINK_OK; } int LINK::UpdateItem(LINK *pList, data_type tNew, data_type tOld) { if (!pList) return LINK_ERR; int flage = 1; LINK *Tmp = pList; while (flage) { if (Tmp->getData() == tOld) { Tmp->setData(tNew); break; } Tmp = Tmp->getNext(); if (Tmp == pList) flage = 0; } return LINK_OK; } int LINK::SearchItem(LINK *pList, data_type tData) { if (!pList) return LINK_ERR; int flage = 1; int i = 0; LINK *Tmp = pList; while (flage) { if (Tmp->getData() == tData) { cout << "The data is No." << i << endl; } Tmp = Tmp->getNext(); i++; if (Tmp == pList) flage = 0; } return LINK_OK; } void LINK::ShowList(LINK *pList, int iFlag) { if (!pList) return; int flage = 1; LINK *Tmp = NULL; switch (iFlag) { case HEADER: Tmp = pList; while (flage) { cout << Tmp->getData() << " "; Tmp = Tmp->getNext(); if (Tmp == pList) flage = 0; } break; case TAIL: Tmp = pList; while (flage) { cout << Tmp->getData() << " "; Tmp = Tmp->getLast(); if (Tmp == pList) flage = 0; } break; default: break; } cout << endl; return; } void LINK::operator delete(void *pList) { if (!pList) return; LINK *Tmp = ((LINK *) pList)->getNext(); while ((Tmp != pList) && (NULL != Tmp)) { cout<<Tmp->getData()<<" "; ((LINK *)pList)->setNext(Tmp->getNext()); free(Tmp); Tmp = ((LINK *) pList)->getNext(); } cout<<((LINK *) pList)->getData()<<endl; free(pList); return; }
main.cpp
#include "LINK/LINK.h" #include <iostream> using namespace std; void function() { int i = 9; LINK *pList = new LINK(i); if (!pList) return; while (i--) { pList->InsertItem(pList, i, 0); } pList->ShowList(pList, 0); pList->ShowList(pList, -1); pList->InsertItem(pList, 999, 0); pList->ShowList(pList, -1); pList->InsertItem(pList, 666, -1); pList->ShowList(pList, -1); pList->InsertItem(pList, 333, 33); pList->ShowList(pList, 0); pList->DeleteItem(pList, &i, 0); pList->ShowList(pList, 0); pList->DeleteItem(pList, &i, -1); pList->ShowList(pList, 0); pList->DeleteItem(pList, &i, 32); pList->ShowList(pList, 0); pList->UpdateItem(pList, 333, 0); pList->ShowList(pList, 0); pList->SearchItem(pList, 333); delete pList; cout << "end" << endl; } int main() { function(); return 0; }
C++__双向循环链表(练习)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。