c++中的双向链表写法,主要实现(增删查改,链表逆置,构造函数,运算符重载,等)
#include <bits/stdc++.h>
using namespace std;
template<typename T>
struct node{ //链表节点的结构
T data; //数据域
node<T>* next; //后继指针
node<T>* prev; //前驱指针
node(T x):data(x),next(nullptr),prev(nullptr){}
};
template<typename T>
class linklist{
private:
node<T>* head; //头指针
node<T>* tail; //尾指针
public:
linklist();
//拷贝构造函数
linklist(const linklist<T>& slist);
//赋值运算符
linklist<T>& operator=(const linklist<T>& slist);
void pushback(T x);
void pushfront(T x);
void popback();
void popfront();
int mysize();
void myclear();
void myreverse();
void print()
{
node<T>* temp=head;
while(temp!=nullptr)
{
cout<<temp->data<<"->";
temp=temp->next;
}
cout<<"null"<<endl;
}
};
template<typename T>
linklist<T>::linklist():head(nullptr),tail(nullptr){}
template<typename T> //目标对象不存在,源对象存在
linklist<T>::linklist(const linklist<T>& slist) //拷贝构造函数
{
head=nullptr;
tail=nullptr;
node<T>* cur=slist.head;
while(cur)
{
pushback(cur->data);
cur=cur->next;
}
}
template<typename T> //目标对象和源对象都存在
linklist<T>& linklist<T>::operator=(const linklist<T>& slist) //赋值构造函数
{
myclear();
node<T>* cur=slist.head;
while(cur)
{
pushback(cur->data);
cur=cur->next;
}
return *this;
}
template<typename T>
void linklist<T>::pushback(T x)
{
if(head==nullptr)
{
head=new node<T>(x);
tail=head;
}
else
{
node<T>* temp=new node<T>(x);
tail->next=temp;
temp->prev=tail;
tail=temp;
}
return;
}
template<typename T>
void linklist<T>::pushfront(T x)
{
if(head==nullptr)
{
head=new node<T>(x);
tail=head;
}
else
{
node<T>* temp=new node<T>(x);
temp->next=head;
head->prev=temp;
head=temp;
}
}
template<typename T>
void linklist<T>::popback()
{
if(head==nullptr)
{
cout<<"空链表"<<endl;
return;
}
if(tail->prev==nullptr)
{
delete tail;
head=tail=nullptr;
}
else
{
node<T>* tmp=tail;
tail=tail->prev;
tail->next=nullptr;
delete tmp;
}
}
template<typename T>
void linklist<T>::popfront()
{
if(head==nullptr)
{
cout<<"空链表"<<endl;
return;
}
if(head->next==nullptr)
{
delete head;
head=tail=nullptr;
}
else
{
node<T>* tmp=head;
head=head->next;
head->prev=nullptr;
delete tmp;
}
}
template<typename T>
int linklist<T>::mysize()
{
int cnt=0;
node<T>* tmp=head;
while(tmp)
{
cnt++;
tmp=tmp->next;
}
return cnt;
}
template<typename T>
void linklist<T>::myclear()
{
if(head==nullptr)
{
return;
}
int n=mysize();
for(int i=0;i<n;i++)
{
popfront();
}
}
template<typename T>
void linklist<T>::myreverse()
{
node<T>* pre=nullptr;
node<T>* next=nullptr;
tail=head;
while(head)
{
next=head->next;
head->next=pre;
head->prev=next;
pre=head;
head=next;
}
head=pre;
}
struct Student{
unsigned id;
string name;
unsigned age;
Student(unsigned id,string name,unsigned age):id(id),name(name),age(age){}
friend ostream& operator<<(ostream& output,const Student& st)
{
output<<st.id<<" "<<st.name<<" "<<st.age;
return output;
}
};
int main()
{
linklist<Student> mylist;
mylist.pushback(Student{1,"hello",24});
mylist.print();
mylist.pushback(Student{2,"hello",27});
mylist.print();
mylist.pushback(Student{3,"bib",29});
mylist.print();
mylist.pushfront(Student{4,"jason",0});
mylist.print();
mylist.pushfront(Student{5,"foo",20});
mylist.print();
mylist.popfront();
mylist.print();
mylist.popback();
mylist.print();
mylist.myreverse();
mylist.print();
linklist<Student> mylist1=mylist;
mylist1.print();
mylist1.myclear();
mylist1.print();
return 0;
}
运行结果:
哪里有不对的欢迎批评指正!