链表基本操作及面试题

头文件

#ifndef _LINKED_LIST_H__
#define _LINKED_LIST_H__


#include<iostream>
using namespace std;
#include<cassert>


typedef int Datatype;
struct Node
{
    Node(Datatype data=0,Node* prev = NULL, Node* next = NULL)
    :_data(data)
    , _prev(prev)
    , _next(next)
    {
        _data = data;
        _prev = prev;
        _next = next;
    }

    Datatype _data;
    Node* _prev;
    Node* _next;
};

 class List
{
public:
    //构造函数
    List();
    List(size_t n, Datatype data);
    //拷贝构造函数
    List(const List& ls);
    //赋值操作符重载
    List& operator=(const List& ls);
    //析构函数
    ~List();
public:
    //头插/头删一个元素
    void PushFront(Datatype x);
    void PopFtont();
    //尾插/尾删一个元素
    void PushBack(Datatype x);
    void PopBack();
    //任意位置插入或者删除一个元素
    void Insert(Node* pos,Datatype x);
    void Erase(Node* pos);


public:
    void Destroy();
    void Display();
    Node* Find(Datatype x);

private:
    Node* _pHead;
};

实现函数

#define _CRT_SECURE_NO_WARNINGS 1
#include"LinkedList.h"

List::List()
:_pHead(NULL)
{}

//构造函数
List::List(size_t n, Datatype data)
{
    _pHead = new Node;
    Node* cur = _pHead;
    while (n--)
    {
        Node* newnode = new Node(data);
        cur->_next = newnode;
        newnode->_prev = cur;
        cur = newnode;
    }
}

//拷贝构造函数
List::List(const List& ls)
{
    Destroy();
    Node* cur = ls._pHead;
    _pHead = new Node;
    Node* tmp = _pHead;
    while (cur&&cur->_next)
    {
        cur = cur->_next;
        Node* newnode = new Node(cur->_data);
        tmp->_next = newnode;
        newnode->_prev = tmp;
        tmp = tmp->_next;
    }
}

//赋值运算符重载
List& List:: operator=(const List& ls)
{
    if (this != &ls)
    {
        Destroy();
        if (NULL == ls._pHead)
        {
            _pHead = ls._pHead;
            return *this;
        }
        _pHead = new Node;
        Node* tmp = _pHead;
        Node* cur = ls._pHead;
        while (cur&&cur->_next)
        {
            cur = cur->_next;
            Node* newnode = new Node(cur->_data);
            tmp->_next = newnode;

            newnode->_prev = tmp;
            tmp = tmp->_next;
        }
    }
    return *this;
}

//销毁函数
void List::Destroy()
{
    Node* cur = _pHead;
    Node* del = cur;
    while (cur)
    {
        cur = cur->_next;
        delete del;
        del = cur;
    }
}

//析构函数
List::~List()
{
    if (_pHead)
        Destroy();
}

//头插一个函数
void List::PushFront(Datatype x)
{
    Node* cur = _pHead;
    Node* newnode = new Node(x);
    newnode->_next = cur->_next;
    cur->_next->_prev = newnode;
    cur->_next = newnode;
    newnode->_prev = cur;   
}

//头删一个元素
void List::PopFtont()
{
    Node* cur = _pHead;
    Node* del = cur;
    if (NULL == cur->_next)
    {
        del = del->_next;
        delete del;
        cur->_next = NULL;
        return;
    }
    del = del->_next;
    cur->_next = del->_next;
    del->_next->_prev = cur;
    delete del;
}


//尾插/尾删一个元素
void List:: PushBack(Datatype x)
{
    Node* cur = _pHead;
    while (cur->_next)
    {
        cur = cur->_next;
    }
    Node* newnode = new Node(x);
    cur->_next = newnode;
    newnode->_prev = cur;
}

void List:: PopBack()
{
    Node* cur = _pHead;
    Node* prev = cur;
    if (NULL == cur->_next)
    {
        return;
    }
    while (cur->_next)
    {
        prev = cur;
        cur = cur->_next;
    }
    free(cur);
    prev->_next = NULL;
}

//任意位置插入或者删除一个元素
void List::Insert(Node* pos, Datatype x)
{
    Node* cur = pos;
    assert(pos);
    if (NULL == cur->_next)
    {
        Node* newnode = new Node(x);
        cur->_next = newnode;
        newnode->_prev = cur;
        return;
    }
    Node* newnode = new Node(x);
    newnode->_next = cur->_next;
    cur->_next->_prev = newnode;
    cur->_next = newnode;
    newnode->_prev = cur;
}

void List::Erase(Node* pos)
{
    Node* cur = pos;
    Node* prev = cur->_prev;
    if (NULL == cur->_next)
    {
        delete cur;
        prev->_next = NULL;
        return;
    }
    prev->_next = cur->_next;
    cur->_next->_prev = prev;
    delete cur;
}

//查找值为x的第一个出现的位置
Node* List::Find(Datatype x)
{
    Node* cur = _pHead;
    while (cur && cur->_data != x)
    {
        cur = cur->_next;
    }
    if (NULL != cur)
    {
        return cur;
    }
    return NULL;
}




//打印函数
void List::Display()
{
    cout << "NULL->";
    Node* cur = _pHead;
    while (cur&&cur->_next)
    {
        cur = cur->_next;
        cout << cur->_data<<"->";
    }
    cout << "NULL" << endl;
}

测试函数

#define _CRT_SECURE_NO_WARNINGS 1
#include"LinkedList.h"


void test1()
{
    List ls1;
    ls1.Display();
    List ls2(4,4);
    ls2.Display();
    List ls3(3, 3);
    ls3.Display();
    ls3 = ls2;
    ls3.Display();
    List ls4(ls2);
    ls4.Display();
    ls3 = ls2;
    ls3.Display();
}

void test2()
{
    List ls1(1,4);
    Node* pos = NULL;
    ls1.Display();
    ls1.PushFront(1);
    ls1.Display();
    ls1.PopFtont();
    ls1.Display();
    ls1.PushBack(5);
    ls1.Display();
    ls1.PopBack();
    ls1.Display();
    ls1.PushFront(3); 
    ls1.PushFront(5); 
    ls1.PushFront(7);
    ls1.Display();
    pos = ls1.Find(7);
    cout << pos->_data << endl;
    ls1.Insert(pos, 9);
    ls1.Display();
    ls1.Erase(pos);
    ls1.Display();
}

int main()
{
    //test1();
    test2();
    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值