📄 h9.cpp
字号:
#include"h9.h"//构造,析构函数List::List(){ if(!(HeadList = new LNode)) { cout<<"distribute memory fail!\n"; return; } HeadList->next = NULL; HeadList->prev = NULL; LengthList=0;}List::~List(){ LinkList temp = NULL; while(HeadList->next != NULL) { temp = HeadList->next; HeadList->next = temp->next; delete temp; } LengthList = 0; delete HeadList;}List::List(const List& temp){ LinkList newlist,lastlist,p; p = temp.HeadList->next; HeadList = new LNode; lastlist = HeadList; this->HeadList->next = NULL; this->HeadList->prev = NULL; this->LengthList = temp.LengthList; while(p) { newlist = new LNode; newlist->data = p->data; lastlist->next = newlist; newlist->prev = lastlist; newlist->next = NULL; lastlist = newlist; p = p->next; }}//public方法实现bool List::InsertLNode(int i,int data_){ LinkList p = HeadList->next,Pinsert; int j = 1; Pinsert = new LNode; Pinsert->data = data_; Pinsert->next = NULL; Pinsert->prev = NULL; if(i == 1) { Pinsert->prev = HeadList; HeadList->next = Pinsert; LengthList = 1; return true; } else { while(p != NULL&&j < i-1) //查询第i-1行 { p = p->next; j++; } if(p == NULL) { cout<<"illegal operate!\n"; return false; } Pinsert->next = p; p->prev->next = Pinsert; Pinsert->prev = p->prev; p->prev = Pinsert; LengthList++; } return true;}int List::SearchLNode(int i){ int j = 1,temp; LinkList p=HeadList->next; while(p&&j < i) { p = p->next; j++; } if(p == NULL || j > i) { cout << "search fail\n"; return -1; } temp = p->data; return temp;}bool List::DeleteLNode(int i){ int j = 1; LinkList p = HeadList->next; while(p && j < i) { p = p->next; j++; } if(p == NULL||j > i) { cout << "delete fail!\n"; return false; } p->prev->next = p->next; p->next->prev = p->prev; delete p; LengthList--; return true;}void List::ShowList(){ LinkList p = HeadList->next; cout<<"Now,this List is "; for(;p != NULL;p = p->next) { cout << p->data<<"<==>"; } cout << "NULL\n";}int List::GetLengthList(){ return LengthList;}List& List::operator=(const List& temp){ LinkList newlist,lastlist,p; p = temp.HeadList->next; HeadList = new LNode; lastlist = HeadList; this->HeadList->next = NULL; this->HeadList->prev = NULL; this->LengthList = temp.LengthList; while(p) { newlist = new LNode; newlist->data = p->data; lastlist->next = newlist; newlist->prev = lastlist; newlist->next = NULL; lastlist = newlist; p = p->next; } return *this;}List List::operator+(const List& temp){ List nowList,tempList(*this); LinkList p = tempList.HeadList->next,q = temp.HeadList->next; while(p->next != NULL) { p = p->next; } p->next = q; q->prev = p; delete temp.HeadList; nowList=tempList; nowList.LengthList = tempList.LengthList + temp.LengthList; return nowList;}List& List::operator-=(const List& temp){ List t(temp); LinkList p ,q = t.HeadList->next,r; while(q != NULL) { p = this->HeadList->next; while(p != NULL) { if(p->data == q->data) { r = p->prev; p->prev->next=p->next; p->next->prev=p->prev; delete p; this->LengthList--; p = r; } p = p->next; } q = q->next; } return *this;}List& List::operator+=(List& temp){ LinkList p = this->HeadList->next,q = temp.HeadList->next; while(p->next != NULL) { p = p->next; } p->next = q; q->prev = p; delete temp.HeadList; this->LengthList += temp.LengthList; return *this;}List List::operator-(const List& temp){ List t(temp),m(*this); LinkList p,q = t.HeadList->next,r; while(q != NULL) { p = m.HeadList->next; while(p != NULL) { if(p->data == q->data) { r = p->prev; p->prev->next = p->next; p->next->prev = p->prev; delete p; m.LengthList--; p = r; } p = p->next; } q = q->next; } return m;}ostream& operator<<(ostream& out,List& temp){ List t(temp); out << "length of List:"<< t.GetLengthList() << endl; out << "data of List:"; for(int i = 0;i < t.GetLengthList();i++) { out << t.HeadList->next->data << " "; t.HeadList = t.HeadList->next; } out<<"\n"; return out;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -