腾讯面试题 C++ 双向链表 模板类实现

c++中的双向链表写法,主要实现(增删查改,链表逆置,构造函数,运算符重载,等)

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)  //赋值构造函数
{
    if(*this==slist)
    {
        return *this;
    }
    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;
}

运行结果:


哪里有不对的欢迎批评指正!

 
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值