首先定义一下双链表的结点结构
#include<iostream>
using namespace std;
class Node
{
public:
Node()=default; // c++ 11标准,如果不支持其实就是各种成员值默认都赋0
Node(int val):value(val),pre(nullptr),next(nullptr){}
inline Node*& getPre(){ return pre; } // 得到前一个结点
inline Node*& getNext(){ return next; } //得到后一个结点
inline int getValue(){ return value; } //得到当前结点的值
private:
Node *pre;
Node *next;
int value;
};
void CreateList(Node *&head); //创建一个双链表
void printLList(Node *head); //打印双链表
void Insert(Node *&head, size_t pos, int val); //插入一个结点
void DeleteByValue(Node *&head,int val); //按给的值删一个元素
void DeleteByPos(Node *&head,size_t position); //按位置删一个元素
Node* SearchByValue(Node *head, int val, size_t *where=0); //按值查找
Node* SearchByPos(Node *head, size_t pos, int *val=0); //按给的位置查找元素
size_t Length(Node *head); //返回链表的长度
void CreateList(Node *&head)
{
int input;
size_t pos=1;
cout<<"正在创建链表,输入结点值 (ctrl+z 结束输入)"<<endl;
while(cin >> input)
Insert(head,pos++,input);
cout<<"The list was created!"<<endl<<endl;
cin.clear();
return;
}
void Insert(Node *&head, size_t pos, int val)
{
size_t length = Length(head);
if(pos < 1 || pos > (length+1)) // 插入位置不合法
{
cout<<"The given position is illegal!"<<endl<<endl;
return;
}
Node *newNode = new Node(val);
if(pos == 1) // 表头插入
{
if(head == nullptr) //空表
head = newNode;
else //非空表
{
head->getPre() = newNode;
newNode->getNext() = head;
head = newNode;
}
}
else
{
Node *position = SearchByPos(head,pos-1);;
if( pos == (length+1)) // 表尾插入
{
position->getNext() = newNode;
newNode->getPre() = position;
}
else // 其他位置插入
{
newNode->getNext() = position->getNext();
newNode->getPre() = position;
position->getNext()->getPre() = newNode;
position->getNext() = newNode;
}
}
return;
}
size_t Length(Node *head)
{
size_t len = 0;
Node *pos = head;
while( pos != nullptr )
{
++len;
pos = pos->getNext();
}
return len;
}
void DeleteByValue(Node *&head,int val)
{
Node* pos = SearchByValue(head,val);
if(pos == nullptr)
{
cout<<val<<" is not found, cannot delete it!"<<endl<<endl;
return;
}
else if(pos == head) // 删表头
head = head->getNext();
else if(pos->getNext() != nullptr) //删其他位置
{
pos->getPre()->getNext() = pos->getNext();
pos->getNext()->getPre() = pos->getPre();
}
else //删表尾
pos->getPre()->getNext() = nullptr;
delete pos;
pos = nullptr;
cout<<val<<" is deleted!"<<endl<<endl;
return;
}
void DeleteByPos(Node *&head,size_t position)
{
size_t length = Length(head);
if(position < 1 || position > length)
{
cout<<"The position is illegal, cannot delete it!"<<endl<<endl;
return;
}
Node* pos = SearchByPos(head,position);
if(position == 1) // 删除 表头
head = head->getNext();
else
{
if(pos->getNext() == nullptr) //删除 表尾
pos->getPre()->getNext() = nullptr;
else //删其他位置
{
pos->getPre()->getNext() = pos->getNext();
pos->getNext()->getPre() = pos->getPre();
}
}
delete pos;
pos = nullptr;
cout<<"The value at position "<<position<<" is deleted!"<<endl<<endl;
return;
}
Node* SearchByValue(Node *head, int val, size_t *where)
{
if(where != 0)
++(*where);
Node *pos = head;
while(pos != nullptr && val != pos->getValue())
{
pos = pos->getNext();
if(where != 0)
++(*where);
}
if(pos == nullptr)
return nullptr;
else
return pos;
}
Node* SearchByPos(Node *head, size_t pos, int *val)
{
size_t length = Length(head);
if(pos < 1 || pos > length)
return nullptr;
Node *position = head;
size_t temp = 0;
while(position != nullptr && temp < pos-1)
{
++temp;
position = position->getNext();
}
if(position == nullptr)
return nullptr;
else
{
if(val != 0)
*val = position->getValue();
return position;
}
}
void printLList(Node *head)
{
if(head == nullptr)
cout<<"The list is empty!"<<endl<<endl;
else
{
do
{
cout<<head->getValue()<<" ";
head = head->getNext();
}while(head != nullptr);
cout<<endl<<endl;
}
return;
}
int main(int argc, char** argv)
{
Node *temp;
size_t pos;
int val;
Node *head = nullptr;
CreateList(head); //创建链表
printLList(head);
cout<<"插入, 输入要插入的位置和要插入的值(位置从1开始)"<<endl;
cin>>pos>>val;
Insert(head,pos,val);
printLList(head);
pos=0;
cout<<"按值查找, 输入要查找的值"<<endl;
cin>>val;
temp = SearchByValue(head,val,&pos);
if(temp == nullptr)
cout<<"The value of "<<val<<" was no found!"<<endl<<endl;
else
cout<<"The value of "<<val<<" was found! Its position is "<<pos<<endl<<endl;
val=0;
cout<<"按地址查找, 输入要查找的位置"<<endl;
cin>>pos;
temp = SearchByPos(head,pos,&val);
if(temp == nullptr)
cout<<"The value at position "<<pos<<" was no found!"<<endl<<endl;
else
cout<<"The value at position "<<pos<<" was found! Its value is "<<val<<endl<<endl;
cout<<"按值删除,输入要删除的值"<<endl;
cin>>val;
DeleteByValue(head,val);
printLList(head);
cout<<"按地址删除,输入要删除的位置"<<endl;
cin>>pos;
DeleteByPos(head,pos);
printLList(head);
return 0;
}
运行样例