C++实现双向链表(含头结点)

 VS2005运行通过,如有问题,请各位大牛指正。

注意:双向链表含有头结点

#include 
using namespace std;
template
struct Node
{
	Type data;
	Node* prior;
	Node* next;
};
template
class DoubleList
{
protected:
	int len;//链表中结点个数
	Node* Head; //指向头结点
public:
	DoubleList();//默认构造函数
	DoubleList(const DoubleList& otherList);//拷贝构造函数
	~DoubleList();
	void createListForward();//头插法
	void createBackward();//尾插法
	void initList();//生成头结点,尾部设置为NULL
	bool isEmptyList();
	int length();
	void destoryList();
	void getFirstData(Type& firstItem);  
	void search(const Type searchItem);  
	void insertFirst(const Type newItem);  
	void insertLast(const Type newItem);  
	void insertBefore(const int pos,const Type newItem);  
	void insertAfter(const int pos,const Type newItem);  
	void deleteNode(const Type deleteItem);  
	void deleteNode(const int pos,Type& deleteItem);
	void reverse();
	const DoubleList& operator=(const DoubleList&otherList); 
	friend ostream& operator<< <>(ostream& cout,const DoubleList& list);//注意 在<< 后加上 <>表明这是个函数模板
};

template
DoubleList::DoubleList() //初始化时,只有一个头结点,有head指向
{
	Head = new Node;
	Head->next = NULL;
	len =0;
}

template
DoubleList::DoubleList(const DoubleList&otherList)
{
	//首先建立头结点
	Head = new Node;
	Head->next = NULL;
	Head->prior = NULL;
	len =0;
	Node* current = Head;//总是指向本链表的最后一个结点,待插入结点直接插入
	Node* otherListCurrent=otherList.Head->next;//otherListCurrent指向第一个元素
	while(otherListCurrent!=NULL)//拷贝的目标不为空
	{
		Node* newNode = new Node;
		newNode->data = otherListCurrent->data;
		newNode->next = current->next;
		current->next = newNode;
		newNode->prior = current;
		current=current->next;
		otherListCurrent = otherListCurrent->next;
		len++;
	}

}

template
const DoubleList& DoubleList::operator=(const DoubleList&otherList)//赋值函数
{
	Node* current = Head;//current总是指向要插的位置
	Node* otherListCurrent=otherList.Head->next;//otherListCurrent指向第一个元素
	if (this!=&otherList)//避免自己给自己赋值
	{
		if (current->next!=NULL)
		{
			initList();//自己有结点,先销毁,之后生成自己的头结点
		}
		while(otherListCurrent!=NULL)
		{
			Node* newNode = new Node;
			newNode->data = otherListCurrent->data;
			newNode->next = current->next;
			current->next = newNode;
			newNode->prior = current;
			current=current->next;
			otherListCurrent = otherListCurrent->next;
			len++;
		}

	}
	return *this;//为了连续赋值
}

template
DoubleList::~DoubleList()
{
	destoryList();
}

template
void DoubleList::createListForward()//头插法
{
	Node* newNode;
	cout<<"输入链表长度:"<>len;
	for (int i=0;i;
		cout<<"输入元素:"<>newNode->data;
		newNode->next=Head->next;
		Head->next = newNode; //每插入一个结点,都是要把它放在第一个结点的位置
		newNode->prior = Head;
		if (i!=0)
		{
			newNode->next->prior = newNode;
		}
	}
}

template
void DoubleList::createBackward()//尾插法
{
	Node* current = Head;//current指向头结点
	Node* newNode;
	cout<<"输入链表长度:"<>len;
	for (int i=0;i;
		cout<<"输入元素:"<>newNode->data;
		newNode->next = current->next;
		current->next = newNode;
		newNode->prior = current;
		current=current->next;
	}
}

template
void DoubleList::initList() //所有结点都销毁
{
	destoryList();
	Head = new Node;
	Head->next = NULL;
	Head->prior = NULL;
	len =0;
}

template
bool DoubleList::isEmptyList()
{
	if (Head->next==NULL)
	{
		return true;
	}
	else
	{
		return false;
	}
}

template
int DoubleList::length()
{
	return len;
}

template
void DoubleList::destoryList()//销毁包括头结点
{
	Node* current;
	while(Head!=NULL)
	{
		current = Head;
		Head = current->next;
		delete current;
	}
	Head=NULL;
	len=0;
}

template
void DoubleList::getFirstData(Type& firstItem)
{
	if (!isEmptyList())
	{
		firstItem = (Head->next)->data;
	}
	else
	{
		cout<<"链表为空!"<
void DoubleList::search(const Type searchItem)
{
	Node* current;
	if (isEmptyList())
	{
		cout<<"List is Empty"<next;//越过头结点,指向第一个元素
		while (current!=NULL && current->data!=searchItem)
		{
			current = current->next;
		}
		if (current!=NULL)
		{
			cout<
void DoubleList::insertFirst(const Type newItem)
{
	Node *first = Head->next;
	Node *newNode = new Node;
	newNode->data = newItem;
	if (!isEmptyList()) //链表不为空
	{
		first->prior =newNode;
		newNode->next = Head->next;
		newNode->prior = Head;
		Head->next = newNode;
	}
	else
	{
		newNode->next = Head->next;
		newNode->prior = Head;
		Head->next = newNode;
	}
}

template
void DoubleList::insertLast(const Type newItem)
{
	Node *newNode = new Node;
	newNode->data = newItem;
	Node* current = Head;  //寻找位置
	while (current->next != NULL)  
	{  
		current = current ->next;  
	}  
	newNode->next = current->next;
	newNode->prior = current;
	current->next = newNode;
}

template
void DoubleList::insertBefore(const int pos,const Type newItem)
{
	int i=1;  
	Node* current = Head->next;  
	if (pos<1 || pos>len)  
	{  
		cout<<"插入位置不正确!"<* newNode = new Node;  
	newNode->data = newItem;  
	if (1==pos)  
	{  
		current->prior =newNode;
		newNode->next = Head->next;
		newNode->prior = Head;
		Head->next = newNode;  
	}  
	else  
	{  
		while(inext;  
			i++;  
		}  
		newNode->next = current->next;
		current->next->prior = newNode; 
		newNode->prior = current;
		current->next = newNode;  
	}  
	len++; 
}

template
void DoubleList::insertAfter(const int pos,const Type newItem)
{
	int i=1;  
	Node* current = Head->next;//current指向第一个位置,和i配合,指向第i个结点   
	if (pos<1 || pos>len)  
	{  
		cout<<"插入位置不正确!"<* newNode = new Node;  
	newNode->data = newItem;  
	while(inext;  
		i++;  
	}  
	newNode->next = current->next;
	current->next->prior = newNode; 
	newNode->prior = current;
	current->next = newNode;   
	len++; 
}

template
void DoubleList::deleteNode(const Type deleteItem)
{
	Node* current=Head -> next;
	Node* trailCurrent = Head;//指向current前面的结点
	if (isEmptyList())
	{
		cout<<"List is Empty"<data!=deleteItem)
		{
			trailCurrent = current;
			current=current->next;
		}
		if (current==NULL)
		{
			cout<next!=NULL)
			{
				current->next->prior = trailCurrent;
			}
			trailCurrent->next = current->next;
			delete current;
			cout<
void DoubleList::deleteNode(const int pos,Type& deleteItem)
{
	int i=1;  
	Node* trailCurrent = Head;  
	Node* current;  
	if (pos<1 || pos>len)  
	{  
		cout<<"删除位置不正确!"<next;  
		i++;  
	}  
	current = trailCurrent->next;  //current指向要删除的结点,trailCurrent指向之前的结点
	if (current->next!=NULL)
	{
		current->next->prior = trailCurrent;
	}
	trailCurrent->next = current->next;
	deleteItem = current->data;
	delete current;
	cout<
void DoubleList::reverse()
{
	bool isFirst = true;
	Node* current = Head->next;
	Head->next=NULL;
	if (current==NULL)
	{
		cout<<"链表为空!"<* nextCurrent = current->next;
			current->next = Head->next;
			Head->next = current;
			current->prior = Head;
			if (current->next!=NULL)
			{
				current->next->prior = current;
			}
			current = nextCurrent;//指针继续往下走
		}
	}
}

template
ostream& operator<< (ostream& cout,const DoubleList& list)
{
	Node*current=list.Head->next; //有头结点,这是current指向第一个结点
	if (current!=NULL)
	{
		while (current!=NULL)
		{
			cout<data<next;
		}
	}
	else
	{
		cout<<"链表为空!"< list1;
	list1.createBackward();
	list1.reverse();
	cout<


 

你可能感兴趣的:(数据结构)