//构造链表C++
struct Node{
int data; //仍然假定只保存int类型数据
Node *next;//指针
//为该结构体提供一个构造函数
Node(int a=0,Node*b=nullptr):data(a),next(b){}
};
class LinkedList{
public:
//这是单链表节点的结构体
struct Node{
int data;
Node *next;
Node(int a=0,Node *b=nullptr):data(a),next(b){}
};
private:
Node *head;//链表的头结点
int size; //保存数据的数量,这是一个冗余数据
public:
LinkedList();
LinkedList(const LinkedList&rhs);
//原生数组构造函数,构造一个内容与给定数组相同的链表
LinkedList(int const a[],int n);
//填充构造函数,构造一个内容为n个value的链表
LinkedList(int n,int value);
//析构函数,一定要自行实现,否则有内存泄漏
~LinkedList();
int getSize()const{return size;}
};
void LinkedList::insert(int pos,int value)
{ size++;
Node *p=head;
for(int i=-1;i<pos-1;++i) p=p->next;
Node*q=new Node(value);
q->next=p->next;
p->next=q;
}
void LinkedList::remove(int pos)
{ size--;
Node*q=head;
for(int i=-1;i<pos-1;++i) q=q->next;
Node*p=q->next;
q->next=p->next;
delete p;
}
int LinkedList::at(int pos)const
{
Node*p=head;
for(int i=-1;i<pos;++i) p=p->next;
return p->data;
}
void LinkedList::modify(int pos,int newValue)
{
Node*p=head;
for(int i=-1;i<pos;++i) p=p->next;
p->data=newValue;
}
void LinkedList::disp()const
{
for(Node*p=head->next;p;p=p->next)
{
cout<<p->data<<" ";
}
cout<<endl;
}
LinkedList& LinkedList::operator = (const LinkedList&rhs)
{
Node*p=head;
for(Node*q=rhs.head->next;q;q=q->next)
{
p->next=new Node(q->data);
p=p->next;
}
this->size=rhs.size;
return *this;
}
LinkedList& LinkedList::operator += (const LinkedList&rhs)
{
Node*q=head;
for(int i=-1;i<size-1;++i)
q=q->next;
for(Node*p=rhs.head->next;p;p=p->next)
{
q->next=new Node(p->data);
q=q->next;
}
this->size+=rhs.size;
return *this;
}
//方括号运算符重载
int& LinkedList::operator [] (int pos)
{
Node*q=head;
for(int i=-1;i<pos;++i)
q=q->next;
return q->data;
}
const int& LinkedList::operator [] (int pos)const
{
return (const_cast<LinkedList*>(this))->operator[](pos);
}
const LinkedList operator + (const LinkedList&lhs,const LinkedList&rhs)
{
LinkedList temp(lhs);
temp+=rhs;
return temp;
}
//关系运算符重载,按照字典序比较顺序表
bool operator == (const LinkedList&lhs,const LinkedList&rhs)
{
if(lhs.getSize()==rhs.getSize())
{
for(int i=0;i<lhs.getSize();++i)
{
if(lhs.at(i)!=rhs.at(i))
return 0;
}
return 1;
}
else
return 0;
}
bool operator != (const LinkedList&lhs,const LinkedList&rhs)
{if (lhs==rhs)
return 0;
else
return 1;
}
bool operator < (const LinkedList&lhs,const LinkedList&rhs)
{
int len=lhs.getSize();
if(lhs.getSize()>rhs.getSize())
len=rhs.getSize();
for(int i=0;i<len;++i)
{
if(lhs.at(i)>rhs.at(i))
return 0;
if(lhs.at(i)<rhs.at(i))
return 1;
}
if(lhs.getSize()<rhs.getSize())
return 1;
if(lhs.getSize()>=rhs.getSize())
return 0;
}
bool operator <= (const LinkedList&lhs,const LinkedList&rhs)
{
if(lhs<rhs||rhs==lhs)
return 1;
else return 0;
}
bool operator > (const LinkedList&lhs,const LinkedList&rhs)
{
if(lhs<=rhs)
return 0;
else return 1;
}
bool operator >= (const LinkedList&lhs,const LinkedList&rhs)
{
if(lhs<rhs)
return 0;
else return 1;
}
//流输出运算符重载,所有内容输出一行,每个数据后面接一个空格
using std::ostream;
ostream& operator << (ostream&os,const LinkedList&rhs)
{
for(int i=0;i<rhs.getSize();++i)
{
os<<rhs.at(i)<<" ";
}
return os;
}
int LinkedList::compareTo(const LinkedList&rhs)const
{
int len=this->size;
if(len>rhs.size)
len=rhs.size;
Node*lp=head->next;
Node*rp=rhs.head->next;
for(int i=0;i<len;i++)
{
if(lp->data<rp->data)
return -1;
if(lp->data>rp->data)
return 1;
lp=lp->next;
rp=rp->next;
}
if(this->size==rhs.size)
return 0;
if(this->size<rhs.size)
return -1;
if(this->size>rhs.size)
return 1;
}
void LinkedList::disp(ostream&os)const
{
for(Node*q=this->head->next;q;q=q->next)
{
os<<q->data<<" ";
}
}
//构造函数C语言
typedef struct LNode{
ElemType data;
struct LNode *next;
}*Link,*Position;
typedef struct{
Link head,tail;
int len;
}LinkList;
//取第i个节点的data
Status GetElem_L(LinkedList &L,int i,ElemType &e){
//L为头指针
//当第i个元素存在时,其值赋给e并返回OK,否则返回EROOR
LNode *p=L->next; int j=1;
while(p&&j<i){
p=p->next;++j;
}
if(!p||j>i)return ERROR;
e=p->data;
return OK;
}
//在带头节点的单向链表中i位置前插入元素i
Status ListInsert_L(LinkedList &L,int i,ElemType e){
LNode *p=L;int j=0;
while(p&&j<i-1){p=p->next; j++;} //寻找第i-1个节点
if(!p||j>i-1) return ERROR;//报错检查
LNode *q=new struct LNode;//q=(LinkedList)malloc(sizeof(LNode));
q->next=p->next;
p->next=q;
q->data=i;
return OK;
}
//在链表中删除第i个元素并由e返回它的值
Status ListDelete_L(LinkedList &L,int i,ElemType e){
LNode *p=L;int j=0;
while(p&&j<i-1){p=p->next; j++;} //寻找第i-1个节点
if(!(p->next)||j>i-1) return ERROR;//报错检查
LNode *q=p->next; p->next=q->next;
e=q->data;free(q);
return OK;
}
//从表尾到表头输入建立链表
void CreateList_L(LinkedList &L,int n){
L=(LinkedList)malloc(sizeof(LNode));
L->next=NULL;//头结点
for(int i=0;i<n;++i)
{
p=(LinkedList)malloc(sizeof(LNode));
scanf(&p->data);
p->next=L->next; L->next=p;
}
}
//将两个有序链表并为一个有序链表
void MergeList_L(LinkedList &La,LinkedList &Lb,LinkedList &Lc){
//La,Lb非递减排列,合成之后的Lc也非递减排列
LNode *pa=La;LNode *pb=Lb; LNode *pc=Lc=La;
while(pa&&pb){
if(pa->data<=pb->data)
{pc->next=pa;pc=pa;pa=pa->next;}
else{pc->next=pb;pc=pb;pb=pb->next;
}
pc->next=pa?pa:pb;
free(Lb);
}
Status InitList(LinkList &L);//构造一个空的链表