以下为例
DoubleLink.h
struct DLNode
{
int data;
DLNode * last;
DLNode * next;
};
class DoubleLink
{
public:
DoubleLink();
void insertNode(int position,int data);//在位置m(从0开始)插入新节点(注意位置0和最后一个位置的两个节点是连一起的)
void deleteNode(int position);
void travel(bool zf,int position,int n);//zf=0正向输出,否则反向输出。从position位置开始输出,(循环)输出n个数
int getData(int position);
private:
DLNode * m_pHead;
int m_iLength;
};
DoubleLink.cpp
#include"DoubleLink.h"
#include<iostream>
#include<stdlib.h>
using namespace std;
DoubleLink::DoubleLink()
{
m_pHead = new DLNode;
m_pHead->last = NULL;
m_pHead->next = NULL;
m_iLength = 0;
cout << "DoubleLink()" << endl;
}
void DoubleLink::insertNode(int position,int data)
{
DLNode * node = new DLNode;
node->data = data;
if(m_iLength == 0){
m_pHead->next = node;
node->last = node; //是一个头尾相连循环的链表,当只有一个节点时,该节点的前后指向都是节点自身
node-> next = node;
m_iLength++;
}else{
if(position == 0){
node->next = m_pHead->next;
node->last = m_pHead->next->last;
m_pHead->next->last->next = node;
m_pHead->next->last = node;
m_pHead->next = node; //0位置和其他位置的区别就在于头指针指向了该位置
m_iLength++;
}else{
DLNode* current = m_pHead->next;
for(int i = 0;i < position;i ++){
current = current->next;
}
node->next = current;
node->last = current->last;
current->last->next = node;
current->last = node;
m_iLength++;
}
}
}
void DoubleLink::deleteNode(int position)
{
if(m_iLength == 0){
cout << "链表为空无节点可以删除" << endl;
}else{
if(m_iLength == 1){
delete m_pHead->next;
m_pHead->next = NULL;
m_iLength--;
}else{
if(position == 0){
m_pHead->next->next->last = m_pHead->next->last;
m_pHead->next->last->next = m_pHead->next->next;
DLNode*temp = m_pHead->next;
m_pHead->next = m_pHead->next->next;
delete temp;
temp = NULL;
m_iLength--;
}else{
DLNode* deleteNode = m_pHead->next;
for(int i = 0;i < position;i++){
deleteNode = deleteNode->next;
}
deleteNode->last->next = deleteNode->next;
deleteNode->next->last = deleteNode->last;
delete deleteNode;
deleteNode = NULL;
m_iLength--;
}
}
}
}
void DoubleLink::travel(bool zf,int position,int n)
{
if(m_iLength == 0){
cout << "空链表!" << endl;
}else{
if(!zf){
cout << "正向输出:";
DLNode* p = m_pHead->next;
for(int i = 0;i < position;i++)p = p->next;
for(int i = 0;i < n;i++){
cout << p->data << ",";
p = p->next;
}
cout << endl;
}else{
cout << "反向输出:";
DLNode* p = m_pHead->next;
for(int i = 0;i < position;i++)p = p->next;
for(int i = 0;i < n;i++){
cout << p->data << ",";
p = p->last;
}
cout << endl;
}
}
}
int DoubleLink::getData(int position)
{
if(m_iLength == 0){
cout << "空链表!" << endl;
return 9999;
}else{
DLNode*p = m_pHead->next;
for(int i = 0;i < position;i ++){
p = p->next;
}
return p->data;
}
}
demo.cpp
#include<iostream>
#include<stdlib.h>
#include"DoubleLink.h"
using namespace std;
int main()
{
DoubleLink dl;
int data = dl.getData(3);
cout << data << endl;
dl.deleteNode(5);
dl.travel(0,3,10);
for(int i = 0;i <6;i++){
dl.insertNode(0,i);//5、4、3、2、1、0
}
data = dl.getData(3);
cout << data << endl;
dl.deleteNode(0);
dl.travel(0,3,10);//从位置3开始正向循环输出10个数据
dl.travel(1,3,10);//反向
system("pause");
return 0;
}
运行结果: