C++课程设计——链表类

C++课程设计,我链表类,编完之后感觉对链表有了更深刻的认识
// h9.h




#ifndef h9_h_


#define h9_h_

#include<iostream>

using namespace std;


typedef struct LNode
{
struct LNode* next;
struct LNode* prev;
int data;
}*LinkList,LNode;


class List
{
private:
LinkList HeadList;
int LengthList;
public:
List();
List(const List& temp);
~List();
int SearchLNode(int i);
bool InsertLNode(int i,int data_);
bool DeleteLNode(int i);
void ShowList();
int GetLengthList();
List& operator=(const List& temp);
List operator+(const List& temp);
List operator-(const List& temp);
List& operator+=(List& temp);
List& operator-=(const List& temp);
friend ostream& operator<<(ostream& out,List& temp);
};

#endif


// 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;
}
}


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;
}


// h9main.cpp


#include"h9.h"

int main(void)
{
List l,t,lt;
int temp;
l.InsertLNode(1,10);
l.InsertLNode(2,20);
l.InsertLNode(3,30);
l.InsertLNode(4,40);
l.ShowList();
t.InsertLNode(1,20);
t.InsertLNode(2,40);
t.InsertLNode(3,70);
t.InsertLNode(4,80);
t.ShowList();
l-=t;
l.ShowList();
cout<<l;
return 0;
}
面向对象程序设计课程作业 1. 请创建一个数据型为T的链表模板List,实现以下成员函数: 1) 默认构造函数List(),将该链表初始化为一个空链表(10分) 2) 拷贝构造函数List(const List& list),根据一个给定的链表构造当前链表(10分) 3) 析构函数~List(),释放链表中的所有节点(10分) 4) Push_back(T e)函数,往链表最末尾插入一个元素为e的节点(10分) 5) operator<<()友元函数,将链表的所有元素按顺序输出(10分) 6) operator=()函数,实现两个链表的赋值操作(10分) 7) operator+()函数,实现两个链表的连接,A=B+C(10分) 2. 请编写main函数,测试该模板的正确性: 1) 用List模板定义一个List型的模板对象int_listB,从键盘读入m个整数,调用Push_back函数将这m个整数依次插入到该链表中;(4分) 2) 用List模板定义一个List型的模板对象int_listC,从键盘读入n个整数,调用Push_back函数将这n个整数依次插入到该链表中;(4分) 3) 用List模板定义一个List型的模板对象int_listA,调用List的成员函数实现A = B + C;(4分) 4) 用cout直接输出int_listA的所有元素(3分) 5) 用List模板定义List型的模板对象double_listA, double_listB, double_listC,重复上述操作。(15分) 3. 输入输出样例: 1) 输入样例 4 12 23 34 45 3 56 67 78 3 1.2 2.3 3.4 4 4.5 5.6 6.7 7.8 2) 输出样例 12 23 34 45 56 67 78 1.2 2.3 3.4 4.5 5.6 6.7 7.8
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值