标签:c++实现双向单链表
所谓双链表就是除了头指针及尾指针以外,每个结点都有直接前驱和后继。
双链表就是在单链表的基础上加了一个前驱指针。
头指针无前驱但是有后继,尾指针有前驱但是无后继。
"DList.h"
#pragma once
#include <iostream>
using namespace std;
typedef int DataType;
struct Node
{
Node(const DataType& d) //Node的构造函数,为Node初始化
:_data(d)
,_next(0)
,_prev(0)
{}
DataType _data;//数据域
Node* _next;//下一个指针
Node* _prev;//前驱
};
class DList
{
//对"<<"的重载。因在类外,应声明为友元
friend ostream& operator<<(ostream& os,const DList& s);
public:
DList()
:_head(0)
,_tail(0)
{}
~DList()
{
Node* cur = _head;
while(cur)
{
Node* del = cur;
cur = cur->_next;
delete del;
}
_head = NULL;
_tail = NULL;
}
public:
//函数声明
void PushBack(const DataType& d);//后插
void PopBack();//后删
void PushFront(const DataType& d);//前插
void PopFront();//前删
Node* Find(const DataType& d);//查找某一元素
void Insert(Node* pos,const DataType& d);//在某个位置后插入
void Reverse(DList& s);//逆序
void Sort(DList& s);//排序
void Remove(const DataType& d);//删除某一元素
void RemoveAll(const DataType& d);//删除所有d元素
void Erase(Node* pos);//删除某一位置上的元素
private:
Node* _head;//头指针
Node* _tail;//尾指针
};
//函数的实现
// 因在类外实现,所以函数处应加上域名
“DList.cpp”
#include "DList.h"
ostream& operator<<(ostream& os,const DList& s)
{
Node* cur = s._head;
while(cur)
{
os<<cur->_data<<"<=>";
cur = cur->_next;
}
os<<"over"<<endl;
return os;
}
void DList::PushBack(const DataType& d)
{
Node* newNode = new Node(d);
if(_head == NULL)
{
_head = newNode;
_tail = _head;
}
else
{
_tail->_next = newNode;
newNode->_prev = _tail;
_tail = newNode;
_tail->_next = NULL;
}
}
void DList::PopBack()
{
if(_head == NULL)
{
return;
}
else if(_head == _tail)
{
delete _head;
_head = NULL;
_tail = NULL;
}
else
{
Node* del = _tail;
_tail = _tail->_prev;
_tail->_next = NULL;
delete del;
}
}
void DList::PushFront(const DataType& d)
{
Node* newNode = new Node(d);
if(_head == NULL)
{
_head = newNode;
_tail = _head;
}
else
{
newNode->_next = _head;
_head->_prev = newNode;
_head = newNode;
}
}
void DList::PopFront()
{
if(_head == NULL)
{
return;
}
else if(_head == _tail)
{
delete _head;
_head = NULL;
_tail = NULL;
}
else
{
Node* del = _head;
_head = _head->_next;
_head->_prev = NULL;
delete del;
}
}
Node* DList::Find(const DataType& d)
{
Node* cur = _head;
while(cur)
{
if(cur->_data == d)
{
return cur;
}
cur =cur->_next;
}
return NULL;
}
void DList::Insert(Node* pos,const DataType& d)
{
Node* newNode = new Node(d);
Node* cur = _head;
if(_tail == pos)
{
_tail->_next = newNode;
newNode->_prev = _tail;
_tail = newNode;
_tail->_next = NULL;
}
else
{
while(cur)
{
if(cur == pos)
{
newNode->_next = cur->_next;
cur->_next = newNode;
}
cur = cur->_next;
}
}
}
void DList::Reverse(DList& s)
{
if(_head == NULL)
return;
else if(_head == _tail)
return;
else
{
Node* cur = _tail;
while(cur)
{
cout<<cur->_data<<"<=>";
cur = cur->_prev;
}
}
}
void DList::Sort(DList& s)
{
if(s._head == NULL)
return;
else if(s._head == s._tail)
return;
else
{
Node* cur = NULL;
Node* ret = NULL;
DataType tmp = 0;
while(s._head != ret)
{
cur = s._head;
while(cur&&(cur->_next != ret))
{
if(cur->_data > cur->_next->_data)
{
tmp = cur->_data;
cur->_data = cur->_next->_data;
cur->_next->_data = tmp;
}
cur = cur->_next;
}
ret = cur;
}
}
}
void DList::Remove(const DataType& d)
{
Node* cur = _head;
Node* tmp = NULL;
Node* del = NULL;
Node* newNode = NULL;
if(_head == NULL)
return;
while(cur)
{
if(cur->_data == d)
{
del = cur;
if(cur == _head)
{
_head = _head->_next;
_head->_prev = NULL;
delete del;
return;
}
else
{
tmp->_next = cur->_next;
cur->_next->_prev = tmp;
cur = tmp->_next;
delete del;
return;
}
}
tmp = cur;
cur = cur->_next;
}
}
void DList::RemoveAll(const DataType& d)
{
Node* cur = _head;
Node* tmp = NULL;
Node* del = NULL;
Node* newNode = NULL;
if(_head == NULL)
return;
while(cur)
{
if(cur->_data == d)
{
del = cur;
if(cur == _head)
{
_head = _head->_next;
_head->_prev = NULL;
cur = _head;
}
else
{
tmp->_next = cur->_next;
cur->_next->_prev = tmp;
cur = tmp->_next;
}
delete del;
}
else
{
tmp = cur;
cur = cur->_next;
}
}
}
void DList::Erase(Node* pos)//删除某一位置
{
Node* cur = _head;
Node* del = NULL;
if(pos == _head)
{
del = _head;
_head = _head->_next;
_head->_prev = NULL;
delete del;
}
else if(pos == _tail)
{
del = _tail;
_tail = _tail->_prev;
_tail->_next = NULL;
delete del;
}
else
{
while(cur)
{
if(cur->_next == pos)
{
del = cur->_next;
cur->_next = pos->_next;
pos->_next->_prev = cur;
delete del;
}
cur = cur->_next;
}
}
}
#include "DList.h"
ostream& operator<<(ostream& os,const DList& s)
{
Node* cur = s._head;
while(cur)
{
os<<cur->_data<<"<=>";
cur = cur->_next;
}
os<<"over"<<endl;
return os;
}
void DList::PushBack(const DataType& d)
{
Node* newNode = new Node(d);
if(_head == NULL)
{
_head = newNode;
_tail = _head;
}
else
{
_tail->_next = newNode;
newNode->_prev = _tail;
_tail = newNode;
_tail->_next = NULL;
}
}
void DList::PopBack()
{
if(_head == NULL)
{
return;
}
else if(_head == _tail)
{
delete _head;
_head = NULL;
_tail = NULL;
}
else
{
Node* del = _tail;
_tail = _tail->_prev;
_tail->_next = NULL;
delete del;
}
}
void DList::PushFront(const DataType& d)
{
Node* newNode = new Node(d);
if(_head == NULL)
{
_head = newNode;
_tail = _head;
}
else
{
newNode->_next = _head;
_head->_prev = newNode;
_head = newNode;
}
}
void DList::PopFront()
{
if(_head == NULL)
{
return;
}
else if(_head == _tail)
{
delete _head;
_head = NULL;
_tail = NULL;
}
else
{
Node* del = _head;
_head = _head->_next;
_head->_prev = NULL;
delete del;
}
}
Node* DList::Find(const DataType& d)
{
Node* cur = _head;
while(cur)
{
if(cur->_data == d)
{
return cur;
}
cur =cur->_next;
}
return NULL;
}
void DList::Insert(Node* pos,const DataType& d)
{
Node* newNode = new Node(d);
Node* cur = _head;
if(_tail == pos)
{
_tail->_next = newNode;
newNode->_prev = _tail;
_tail = newNode;
_tail->_next = NULL;
}
else
{
while(cur)
{
if(cur == pos)
{
newNode->_next = cur->_next;
cur->_next = newNode;
}
cur = cur->_next;
}
}
}
void DList::Reverse(DList& s)
{
if(_head == NULL)
return;
else if(_head == _tail)
return;
else
{
Node* cur = _tail;
while(cur)
{
cout<<cur->_data<<"<=>";
cur = cur->_prev;
}
}
}
void DList::Sort(DList& s)
{
if(s._head == NULL)
return;
else if(s._head == s._tail)
return;
else
{
Node* cur = NULL;
Node* ret = NULL;
DataType tmp = 0;
while(s._head != ret)
{
cur = s._head;
while(cur&&(cur->_next != ret))
{
if(cur->_data > cur->_next->_data)
{
tmp = cur->_data;
cur->_data = cur->_next->_data;
cur->_next->_data = tmp;
}
cur = cur->_next;
}
ret = cur;
}
}
}
void DList::Remove(const DataType& d)
{
Node* cur = _head;
Node* tmp = NULL;
Node* del = NULL;
Node* newNode = NULL;
if(_head == NULL)
return;
while(cur)
{
if(cur->_data == d)
{
del = cur;
if(cur == _head)
{
_head = _head->_next;
_head->_prev = NULL;
delete del;
return;
}
else
{
tmp->_next = cur->_next;
cur->_next->_prev = tmp;
cur = tmp->_next;
delete del;
return;
}
}
tmp = cur;
cur = cur->_next;
}
}
void DList::RemoveAll(const DataType& d)
{
Node* cur = _head;
Node* tmp = NULL;
Node* del = NULL;
Node* newNode = NULL;
if(_head == NULL)
return;
while(cur)
{
if(cur->_data == d)
{
del = cur;
if(cur == _head)
{
_head = _head->_next;
_head->_prev = NULL;
cur = _head;
}
else
{
tmp->_next = cur->_next;
cur->_next->_prev = tmp;
cur = tmp->_next;
}
delete del;
}
else
{
tmp = cur;
cur = cur->_next;
}
}
}
void DList::Erase(Node* pos)//删除某一位置
{
Node* cur = _head;
Node* del = NULL;
if(pos == _head)
{
del = _head;
_head = _head->_next;
_head->_prev = NULL;
delete del;
}
else if(pos == _tail)
{
del = _tail;
_tail = _tail->_prev;
_tail->_next = NULL;
delete del;
}
else
{
while(cur)
{
if(cur->_next == pos)
{
del = cur->_next;
cur->_next = pos->_next;
pos->_next->_prev = cur;
delete del;
}
cur = cur->_next;
}
}
}
//函数测试
“test.cpp”
#include "DList.h"
void Test1()
{
DList l1;
/*l1.PushBack(1);
l1.PushBack(2);
l1.PushBack(3);
l1.PushBack(4);
cout<<l1;
l1.PopBack();
l1.PopBack();
cout<<l1;
l1.PopBack();
l1.PopBack();
cout<<l1;*/
l1.PushFront(1);
l1.PushFront(2);
l1.PushFront(3);
l1.PushFront(4);
cout<<l1;
l1.PopFront();
l1.PopFront();
cout<<l1;
l1.PopFront();
l1.PopFront();
l1.PopFront();
cout<<l1;
}
void Test2()
{
DList l1;
l1.PushBack(1);
l1.PushBack(2);
l1.PushBack(3);
l1.PushBack(4);
cout<<l1;
Node* ret = l1.Find(3);
if(ret != NULL)
{
cout<<ret->_data<<endl;
}
else
{
cout<<"链表中无此元素"<<endl;
}
//l1.Insert(ret,5);
l1.Erase(ret);
cout<<l1;
}
void Test3()
{
DList l1;
l1.PushBack(1);
l1.PushBack(7);
l1.PushBack(1);
l1.PushBack(5);
l1.PushBack(4);
l1.PushBack(1);
l1.PushBack(0);
cout<<l1;
//l1.Reverse(l1);
/*l1.Sort(l1);
cout<<l1;*/
l1.Remove(4);
l1.RemoveAll(1);
cout<<l1;
}
int main()
{
Test2();
return 0;
}
标签:c++实现双向单链表
原文地址:http://10810429.blog.51cto.com/10800429/1751948