双向链表结构:
实现:
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;
}
}