C++实现双向链表

双向链表结构:
这里写图片描述
实现:

List.h
#pragma once
#include <iostream>
#include <string>
#include <assert.h>
using namespace std;

typedef int DateType;

struct Node
{
public:
    Node()
        :_pNext(NULL)
        , _pPrev(NULL)
        , _data(NULL)
    {}
    Node(const DateType &data)
        :_pNext(NULL)
        ,_pPrev(NULL)
        ,_data(data)
    {}
    Node *_pNext;
    Node *_pPrev;
    DateType _data;
};
class List
{
public:
    List()
    {
        _pHead = new Node;
    }
    void Display();  //打印
    void PushBack(const DateType data);   //尾部插入
    void PopBack();  //尾部删除
    void PushFront(const DateType data);     //头部插入
    void PopFront();      //头部删除
    void Erase(Node *pos);   //删除指定节点
    Node* Find(DateType &d);   //查询节点位置
    void Insert(Node*pos, const DateType &data);  //指定地点插入
    size_t Size();  //大小
    void Clear();   //清空链表
private:
    Node *_pHead;
};
List.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include "List.h"
void List::Display() {  //打印
    assert(this);
    Node *cur = _pHead->_pNext;
    if (cur == NULL) {
        cout << "空链" << endl;
        return;
    }
    while (cur != NULL) {
        cout << cur->_data << "--";
        cur = cur->_pNext;
    }
    cout << endl;
}
void List::PushBack(const DateType data) {
    assert(this);
    Node *next = _pHead;
    while ((next!=NULL)&&(next->_pNext != NULL)) {
        next = next->_pNext;     //指向尾部
    }

    Node *cur = new Node(data);
    next->_pNext = cur;
    cur->_pPrev = next;
    cur->_pNext = NULL;
}
void List::PopBack() {  //尾部删除
    assert(this);
    if (_pHead->_pNext == NULL){   //空链
        cout << "已经是空链无法继续删除" << endl;
        return;
    }
    Node *cur = _pHead;
    while ((cur != NULL) && (cur->_pNext != NULL)) {
        cur = cur->_pNext;     //指向尾部
    }
    Node *dest = cur->_pPrev;
    delete cur;
    dest->_pNext = NULL;
}
void List::PushFront(const DateType data){    //头部插入
    assert(this);
    if (_pHead->_pNext == NULL)
    {
        PushBack(data);
        return;
    }
    Node *Data = new Node(data);
    Node *cur = _pHead->_pNext;
    _pHead->_pNext = Data;
    Data->_pPrev = _pHead;
    Data->_pNext = cur;
    cur->_pPrev = Data;
}
void List::PopFront() {   //头部删除
    assert(this);
    if (_pHead->_pNext == NULL) {
        cout << "已经是空链无法继续删除" << endl;
        return;
    }
    Node*next = _pHead->_pNext->_pNext;
    Node*cur = _pHead->_pNext;
    delete cur;
    if (next == NULL) {    //链中只有一个节点
        _pHead->_pNext = NULL;
        return;
    }
    _pHead->_pNext = next;
    next->_pPrev = _pHead;
}
void List::Erase(Node *pos) {   //删除指定节点
    assert(this);
    Node *cur = pos;
    Node *next = pos->_pNext;
    cur->_data = next->_data;
    cur->_pNext = next->_pNext;
    next->_pNext->_pPrev = cur;
    delete next;
}
Node* List::Find(DateType &d) {   //查询节点位置
    assert(this);
    int pos = 0;
    Node*cur = _pHead;
    while (cur != NULL) {
        if (cur->_data == d)
            return cur;
        cur = cur->_pNext;
    }
    return NULL;
}
void List::Insert(Node*pos, const DateType &data) {  //指定地点插入
    assert(this);
    Node *cur = pos->_pNext;
    Node *dest = new Node(data);
    pos->_pNext = dest;
    dest->_pPrev = pos;
    dest->_pNext = cur;
    cur->_pPrev = dest;
}
size_t List::Size() {  //大小
    int count = 0;
    Node *cur = _pHead->_pNext;
    while (cur != NULL) {
        cur = cur->_pNext;
    }
    return count;
}
void List::Clear() {   //清空链表
    assert(this);
    if (_pHead == NULL) {
        cout << "空链,无序继续清空" << endl;
        return;
    }
    Node *cur = _pHead->_pNext;
    Node *next = _pHead->_pNext->_pNext;
    while (cur->_pNext != NULL) {
        delete cur;
        cur = next;
        next = next->_pNext;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值