// DoubleList.cpp : 定义控制台应用程序的入口点。
//双向链表
#include "stdafx.h"
#include <iostream>
using namespace std;
template<class Type>
struct Node
{
Type data;
Node<Type>* prior; //指向前驱结点
Node<Type>* next; //指向后继结点
};
template<class Type>
class DoubleList
{
public:
DoubleList();//默认构造函数
DoubleList(const DoubleList<Type>& otherList);//拷贝构造函数
~DoubleList();
void createInsertHead();//头插法
void createInsertRear();//尾插法
void initList();//生成头结点,尾部设置为NULL
bool isEmpty();
int length();
void destoryList();
void getFirstData(Type& firstdata);
void find(const Type finddata);
void insertFirst(const Type newdata);
void insertLast(const Type newdata);
void insertBefore(const int pos,const Type newdata);
void insertAfter(const int pos,const Type newdata);
void deleteNode(const Type deletedata);
void deleteNode(const int pos,Type& deletedata);
void reverse();
const DoubleList<Type>& operator=(const DoubleList<Type>&otherList);
friend ostream& operator<< <>(ostream& cout,const DoubleList<Type>& list);
void print();//逆向打印
private:
int m_length;//链表中结点个数
Node<Type>* head; //指向头结点
};
template<class Type>
DoubleList<Type>::DoubleList()//默认构造函数
{
head = new Node<Type>;
head->next = NULL;
head->prior = NULL;
m_length = 0;
}
template<class Type>
DoubleList<Type>::DoubleList(const DoubleList<Type>& otherList)//拷贝构造函数
{
head = new Node<Type>;
head->next = NULL;
head->prior = NULL;
m_length = otherList.m_length;
Node<Type> *current = head;
Node<Type> *otherlistcurrent = otherList.head->next;
while(otherlistcurrent!=NULL)
{
Node<Type> *newnode = new Node<Type>;
newnode->data = otherlistcurrent->data;
newnode->next = current->next;
current->next = newnode;
newnode->prior = current;
current = current->next;
otherlistcurrent = otherlistcurrent->next;
}
}
template<class Type>
DoubleList<Type>::~DoubleList()
{
destoryList();
}
template<class Type>
void DoubleList<Type>::createInsertHead()//头插法
{
Node<Type> *newnode;
cout<<"请输入链表的长度:";
cin>>m_length;
cout<<"请输入链表的元素:";
for(int i=1; i<=m_length; i++)
{
newnode = new Node<Type>;
cin>>newnode->data;
newnode->next = head->next;
head->next = newnode;
newnode->prior = head;
if (i!=1)//注意
{
newnode->next->prior = newnode;
}
}
}
template<class Type>
void DoubleList<Type>::createInsertRear()//尾插法
{
Node<Type> *current = head;
Node<Type> *newnode;
cout<<"请输入链表的长度:";
cin>>m_length;
cout<<"请输入链表的元素:";
for(int i=1; i<=m_length; i++)
{
newnode = new Node<Type>;
cin>>newnode->data;
newnode->next = current->next;
current->next = newnode;
newnode->prior = current;
current = current->next;
}
}
template<class Type>
void DoubleList<Type>::initList()//生成头结点,尾部设置为NULL
{
destoryList();
head = new Node<Type>;
head->next = NULL;
head->prior = NULL;
m_length = 0;
}
template<class Type>
bool DoubleList<Type>::isEmpty()
{
if (head->next == NULL)
{
return true;
}
else
{
return false;
}
}
template<class Type>
int DoubleList<Type>::length()
{
return m_length;
}
template<class Type>
void DoubleList<Type>::destoryList()
{
Node<Type> *current;
while(head!=NULL)
{
current = head;
head = current->next;
delete current;
}
head = NULL;
m_length = 0;
}
template<class Type>
void DoubleList<Type>::getFirstData(Type& firstdata)
{
if (head->next == NULL)
{
cout<<"链表为空!"<<endl;
}
else
{
firstdata = head->next->data;
}
}
template<class Type>
void DoubleList<Type>::find(const Type finddata)
{
Node<Type> *current;
if (isEmpty())
{
cout<<"链表为空!"<<endl;
}
else
{
current = head->next;
while(current!=NULL && current->data!=finddata)
{
current = current->next;
}
if (current == NULL)
{
cout<<finddata<<"不存在!"<<endl;
}
else
{
cout<<finddata<<"被找到!"<<endl;
}
}
}
template<class Type>
void DoubleList<Type>::insertFirst(const Type newdata)
{
Node<Type> *newnode = new Node<Type>;
newnode->data = newdata;
if (isEmpty())
{
newnode->next = head->next;
head->next = newnode;
newnode->prior = head;
}
else
{
newnode->next = head->next;
head->next = newnode;
newnode->prior = head;
newnode->next->prior = newnode;
}
m_length++;
}
template<class Type>
void DoubleList<Type>::insertLast(const Type newdata)
{
Node<Type> *newnode = new Node<Type>;
newnode->data = newdata;
Node<Type> *current = head;
while(current->next!=NULL)
{
current = current->next;
}
newnode->next = current->next;
current->next = newnode;
newnode->prior = current;
m_length++;
}
template<class Type>
void DoubleList<Type>::insertBefore(const int pos,const Type newdata)
{
int i=1;
if (pos<1 || pos>m_length)
{
cout<<"指定的位置不正确!"<<endl;
return;
}
if (isEmpty())
{
cout<<"链表为空!"<<endl;
}
else
{
Node<Type> *newnode = new Node<Type>;
newnode->data = newdata;
Node<Type> *current = head;
while(i<pos)
{
current = current->next;
i++;
}
newnode->next = current->next;
current->next = newnode;
newnode->prior = current;
newnode->next->prior = newnode;
m_length++;
}
}
template<class Type>
void DoubleList<Type>::insertAfter(const int pos,const Type newdata)
{
int i=0;
if (pos<1 || pos>m_length)
{
cout<<"指定的位置不正确!"<<endl;
return;
}
if (isEmpty())
{
cout<<"链表为空!"<<endl;
}
else
{
Node<Type> *newnode = new Node<Type>;
newnode->data = newdata;
Node<Type> *current = head;
while(i<pos)
{
current = current->next;
i++;
}
newnode->next = current->next;
current->next = newnode;
newnode->prior = current;
newnode->next->prior = newnode;
m_length++;
}
}
template<class Type>
void DoubleList<Type>::deleteNode(const Type deletedata)
{
if (isEmpty())
{
cout<<"链表为空!"<<endl;
return;
}
Node<Type> *precurrent = head;
Node<Type> *current = head->next;
while(current!=NULL && current->data!=deletedata)
{
precurrent = current;
current = current->next;
}
if (current==NULL)
{
cout<<deletedata<<"不存在!"<<endl;
}
else
{
if (current->next!=NULL)
{
current->next->prior = precurrent;//这步不能忘记
}
precurrent->next = current->next;
delete current;
cout<<deletedata<<"已被删除!"<<endl;
m_length--;
}
}
template<class Type>
void DoubleList<Type>::deleteNode(const int pos,Type& deletedata)
{
int i=1;
if (pos<1 || pos>m_length)
{
cout<<"指定的位置不正确!"<<endl;
return;
}
if (isEmpty())
{
cout<<"链表为空!"<<endl;
deletedata = -1;
return;
}
Node<Type> *precurrent = head;
Node<Type> *current;
while(i<pos)
{
precurrent = precurrent->next;
i++;
}
current = precurrent->next;
deletedata = current->data;
if (current->next != NULL)
{
current->next->prior = precurrent;
}
precurrent->next = current->next;
delete current;
cout<<"位置为"<<pos<<"的元素"<<deletedata<<"已被删除!"<<endl;
m_length--;
}
template<class Type>
void DoubleList<Type>::reverse()
{
Node<Type> *current = head->next;
head->next =NULL;
if (current == NULL)
{
cout<<"链表为空!"<<endl;
}
else
{
while(current!=NULL)
{
Node<Type> *nextcurrent = current->next;
current->next = head->next;
head->next = current;
current->prior = head;
if (current->next != NULL)
{
current->next->prior = current;
}
current = nextcurrent;
}
}
}
template<class Type>
const DoubleList<Type>& DoubleList<Type>::operator=(const DoubleList<Type>&otherList)
{
if (this!=&otherList)
{
if (!isEmpty())
{
initList();
}
Node<Type> *current = head;
Node<Type> *otherlistcurrent = otherList.head->next;
while(otherlistcurrent!=NULL)
{
Node<Type> *newnode = new Node<Type>;
newnode->data = otherlistcurrent->data;
newnode->next = current->next;
current->next = newnode;
newnode->prior = current;
current = current->next;
otherlistcurrent = otherlistcurrent->next;
}
m_length = otherList.m_length;
}
return *this;
}
template<class Type>
ostream& operator<< <>(ostream& cout,const DoubleList<Type>& list)
{
Node<Type> *current = list.head->next;
if (current==NULL)
{
cout<<"链表为空!"<<endl;
}
while(current!=NULL)
{
cout<<current->data<<" ";
current = current->next;
}
cout<<endl;
return cout;
}
template<class Type>
void DoubleList<Type>::print()
{
Node<Type> *current = head->next;
if (current==NULL)
{
cout<<"链表为空!"<<endl;
}
while(current->next!=NULL)
{
current = current->next;
}
cout<<current->data<<endl;
while(current!=head)
{
cout<<current->data<<" ";
current = current->prior;
}
cout<<endl;
}
int test(int argc, char* argv[])
{
DoubleList<int> *list = new DoubleList<int>;
list->createInsertHead();
cout<<"list:"<<*list<<endl;
list->initList();
list->createInsertRear();
cout<<"list:"<<*list<<endl;
int firstdata;
list->getFirstData(firstdata);
cout<<"第一个元素是:"<<firstdata<<endl;
list->find(9);
list->insertFirst(45);
list->insertLast(50);
list->insertBefore(3,2012);
list->insertAfter(4,719);
list->deleteNode(5);
int deletedata;
list->deleteNode(4,deletedata);
cout<<"list:"<<*list<<endl;
DoubleList<int> *copylist = new DoubleList<int>(*list);
cout<<"copylist:"<<*copylist<<endl;
list->reverse();
copylist = list;
cout<<"copylist:"<<*copylist<<endl;
copylist->print();
system("pause");
return 0;
}
//双向链表
#include "stdafx.h"
#include <iostream>
using namespace std;
template<class Type>
struct Node
{
Type data;
Node<Type>* prior; //指向前驱结点
Node<Type>* next; //指向后继结点
};
template<class Type>
class DoubleList
{
public:
DoubleList();//默认构造函数
DoubleList(const DoubleList<Type>& otherList);//拷贝构造函数
~DoubleList();
void createInsertHead();//头插法
void createInsertRear();//尾插法
void initList();//生成头结点,尾部设置为NULL
bool isEmpty();
int length();
void destoryList();
void getFirstData(Type& firstdata);
void find(const Type finddata);
void insertFirst(const Type newdata);
void insertLast(const Type newdata);
void insertBefore(const int pos,const Type newdata);
void insertAfter(const int pos,const Type newdata);
void deleteNode(const Type deletedata);
void deleteNode(const int pos,Type& deletedata);
void reverse();
const DoubleList<Type>& operator=(const DoubleList<Type>&otherList);
friend ostream& operator<< <>(ostream& cout,const DoubleList<Type>& list);
void print();//逆向打印
private:
int m_length;//链表中结点个数
Node<Type>* head; //指向头结点
};
template<class Type>
DoubleList<Type>::DoubleList()//默认构造函数
{
head = new Node<Type>;
head->next = NULL;
head->prior = NULL;
m_length = 0;
}
template<class Type>
DoubleList<Type>::DoubleList(const DoubleList<Type>& otherList)//拷贝构造函数
{
head = new Node<Type>;
head->next = NULL;
head->prior = NULL;
m_length = otherList.m_length;
Node<Type> *current = head;
Node<Type> *otherlistcurrent = otherList.head->next;
while(otherlistcurrent!=NULL)
{
Node<Type> *newnode = new Node<Type>;
newnode->data = otherlistcurrent->data;
newnode->next = current->next;
current->next = newnode;
newnode->prior = current;
current = current->next;
otherlistcurrent = otherlistcurrent->next;
}
}
template<class Type>
DoubleList<Type>::~DoubleList()
{
destoryList();
}
template<class Type>
void DoubleList<Type>::createInsertHead()//头插法
{
Node<Type> *newnode;
cout<<"请输入链表的长度:";
cin>>m_length;
cout<<"请输入链表的元素:";
for(int i=1; i<=m_length; i++)
{
newnode = new Node<Type>;
cin>>newnode->data;
newnode->next = head->next;
head->next = newnode;
newnode->prior = head;
if (i!=1)//注意
{
newnode->next->prior = newnode;
}
}
}
template<class Type>
void DoubleList<Type>::createInsertRear()//尾插法
{
Node<Type> *current = head;
Node<Type> *newnode;
cout<<"请输入链表的长度:";
cin>>m_length;
cout<<"请输入链表的元素:";
for(int i=1; i<=m_length; i++)
{
newnode = new Node<Type>;
cin>>newnode->data;
newnode->next = current->next;
current->next = newnode;
newnode->prior = current;
current = current->next;
}
}
template<class Type>
void DoubleList<Type>::initList()//生成头结点,尾部设置为NULL
{
destoryList();
head = new Node<Type>;
head->next = NULL;
head->prior = NULL;
m_length = 0;
}
template<class Type>
bool DoubleList<Type>::isEmpty()
{
if (head->next == NULL)
{
return true;
}
else
{
return false;
}
}
template<class Type>
int DoubleList<Type>::length()
{
return m_length;
}
template<class Type>
void DoubleList<Type>::destoryList()
{
Node<Type> *current;
while(head!=NULL)
{
current = head;
head = current->next;
delete current;
}
head = NULL;
m_length = 0;
}
template<class Type>
void DoubleList<Type>::getFirstData(Type& firstdata)
{
if (head->next == NULL)
{
cout<<"链表为空!"<<endl;
}
else
{
firstdata = head->next->data;
}
}
template<class Type>
void DoubleList<Type>::find(const Type finddata)
{
Node<Type> *current;
if (isEmpty())
{
cout<<"链表为空!"<<endl;
}
else
{
current = head->next;
while(current!=NULL && current->data!=finddata)
{
current = current->next;
}
if (current == NULL)
{
cout<<finddata<<"不存在!"<<endl;
}
else
{
cout<<finddata<<"被找到!"<<endl;
}
}
}
template<class Type>
void DoubleList<Type>::insertFirst(const Type newdata)
{
Node<Type> *newnode = new Node<Type>;
newnode->data = newdata;
if (isEmpty())
{
newnode->next = head->next;
head->next = newnode;
newnode->prior = head;
}
else
{
newnode->next = head->next;
head->next = newnode;
newnode->prior = head;
newnode->next->prior = newnode;
}
m_length++;
}
template<class Type>
void DoubleList<Type>::insertLast(const Type newdata)
{
Node<Type> *newnode = new Node<Type>;
newnode->data = newdata;
Node<Type> *current = head;
while(current->next!=NULL)
{
current = current->next;
}
newnode->next = current->next;
current->next = newnode;
newnode->prior = current;
m_length++;
}
template<class Type>
void DoubleList<Type>::insertBefore(const int pos,const Type newdata)
{
int i=1;
if (pos<1 || pos>m_length)
{
cout<<"指定的位置不正确!"<<endl;
return;
}
if (isEmpty())
{
cout<<"链表为空!"<<endl;
}
else
{
Node<Type> *newnode = new Node<Type>;
newnode->data = newdata;
Node<Type> *current = head;
while(i<pos)
{
current = current->next;
i++;
}
newnode->next = current->next;
current->next = newnode;
newnode->prior = current;
newnode->next->prior = newnode;
m_length++;
}
}
template<class Type>
void DoubleList<Type>::insertAfter(const int pos,const Type newdata)
{
int i=0;
if (pos<1 || pos>m_length)
{
cout<<"指定的位置不正确!"<<endl;
return;
}
if (isEmpty())
{
cout<<"链表为空!"<<endl;
}
else
{
Node<Type> *newnode = new Node<Type>;
newnode->data = newdata;
Node<Type> *current = head;
while(i<pos)
{
current = current->next;
i++;
}
newnode->next = current->next;
current->next = newnode;
newnode->prior = current;
newnode->next->prior = newnode;
m_length++;
}
}
template<class Type>
void DoubleList<Type>::deleteNode(const Type deletedata)
{
if (isEmpty())
{
cout<<"链表为空!"<<endl;
return;
}
Node<Type> *precurrent = head;
Node<Type> *current = head->next;
while(current!=NULL && current->data!=deletedata)
{
precurrent = current;
current = current->next;
}
if (current==NULL)
{
cout<<deletedata<<"不存在!"<<endl;
}
else
{
if (current->next!=NULL)
{
current->next->prior = precurrent;//这步不能忘记
}
precurrent->next = current->next;
delete current;
cout<<deletedata<<"已被删除!"<<endl;
m_length--;
}
}
template<class Type>
void DoubleList<Type>::deleteNode(const int pos,Type& deletedata)
{
int i=1;
if (pos<1 || pos>m_length)
{
cout<<"指定的位置不正确!"<<endl;
return;
}
if (isEmpty())
{
cout<<"链表为空!"<<endl;
deletedata = -1;
return;
}
Node<Type> *precurrent = head;
Node<Type> *current;
while(i<pos)
{
precurrent = precurrent->next;
i++;
}
current = precurrent->next;
deletedata = current->data;
if (current->next != NULL)
{
current->next->prior = precurrent;
}
precurrent->next = current->next;
delete current;
cout<<"位置为"<<pos<<"的元素"<<deletedata<<"已被删除!"<<endl;
m_length--;
}
template<class Type>
void DoubleList<Type>::reverse()
{
Node<Type> *current = head->next;
head->next =NULL;
if (current == NULL)
{
cout<<"链表为空!"<<endl;
}
else
{
while(current!=NULL)
{
Node<Type> *nextcurrent = current->next;
current->next = head->next;
head->next = current;
current->prior = head;
if (current->next != NULL)
{
current->next->prior = current;
}
current = nextcurrent;
}
}
}
template<class Type>
const DoubleList<Type>& DoubleList<Type>::operator=(const DoubleList<Type>&otherList)
{
if (this!=&otherList)
{
if (!isEmpty())
{
initList();
}
Node<Type> *current = head;
Node<Type> *otherlistcurrent = otherList.head->next;
while(otherlistcurrent!=NULL)
{
Node<Type> *newnode = new Node<Type>;
newnode->data = otherlistcurrent->data;
newnode->next = current->next;
current->next = newnode;
newnode->prior = current;
current = current->next;
otherlistcurrent = otherlistcurrent->next;
}
m_length = otherList.m_length;
}
return *this;
}
template<class Type>
ostream& operator<< <>(ostream& cout,const DoubleList<Type>& list)
{
Node<Type> *current = list.head->next;
if (current==NULL)
{
cout<<"链表为空!"<<endl;
}
while(current!=NULL)
{
cout<<current->data<<" ";
current = current->next;
}
cout<<endl;
return cout;
}
template<class Type>
void DoubleList<Type>::print()
{
Node<Type> *current = head->next;
if (current==NULL)
{
cout<<"链表为空!"<<endl;
}
while(current->next!=NULL)
{
current = current->next;
}
cout<<current->data<<endl;
while(current!=head)
{
cout<<current->data<<" ";
current = current->prior;
}
cout<<endl;
}
int test(int argc, char* argv[])
{
DoubleList<int> *list = new DoubleList<int>;
list->createInsertHead();
cout<<"list:"<<*list<<endl;
list->initList();
list->createInsertRear();
cout<<"list:"<<*list<<endl;
int firstdata;
list->getFirstData(firstdata);
cout<<"第一个元素是:"<<firstdata<<endl;
list->find(9);
list->insertFirst(45);
list->insertLast(50);
list->insertBefore(3,2012);
list->insertAfter(4,719);
list->deleteNode(5);
int deletedata;
list->deleteNode(4,deletedata);
cout<<"list:"<<*list<<endl;
DoubleList<int> *copylist = new DoubleList<int>(*list);
cout<<"copylist:"<<*copylist<<endl;
list->reverse();
copylist = list;
cout<<"copylist:"<<*copylist<<endl;
copylist->print();
system("pause");
return 0;
}