C ++ language doubly linked list

This article is the basic operation of a doubly linked list as well as two-way linked list on the use of C ++ templates to achieve, before Bowen implement doubly linked lists C language , it has been for everyone to analyze the structure of the doubly linked list, and by way of illustration for everyone It explains the basic operations of a doubly linked list. This article use C ++ to achieve the basic operation of a doubly linked list, including:

Doubly linked list of the basic operation of C ++ language

Doubly linked list Realization of functions
Header insertion node list established Tail insertion node list established
Achieve the specified position of the insertion node Finding the existence of a given value
Node delete the specified location Junction modifying the specified position
Length two-way linked list Print doubly linked list

Doubly linked list of nodes defined

Because doubly linked list node consists of three parts, to point to the current node's immediate predecessor node of the pointer field , for storing data elements of the data field , and a direct successor of the current node to point to the target domain .

So, first we need to package three elements a node class, taking the point defined and implemented using the constructor initializes addition, considering the node to use in the doubly linked list class, the class is defined as the doubly linked list node the friend class.

class doubleLinkedListNode
{
	private:
		doubleLinkedListNode<T> *prior;//双向结点前驱指针指向该结点的前驱结点
		T data;//储存结点数据
		doubleLinkedListNode<T> *next;//双向结点的后驱指针指向该结点的后继结点
		//将双向链表类定义为结点的友元类
		friend  class doubleLinkedList<T>;
	public:
		//结点的无参构造函数,将结点指针域初始化为NULL
		doubleLinkedListNode()
		{
			prior = NULL;
			next = NULL;
		}
		//结点的有参构造函数,初始化指针域和数据域
		doubleLinkedListNode(T _data,doubleLinkedListNode<T> *_prior = NULL,doubleLinkedListNode<T> *_next = NULL)
		{
			prior = _prior;//初始化前驱指针
			data = _data;//初始化数据域
			next = _next;//初始化后继指针
		}
		~doubleLinkedListNode()
		{
			prior = NULL;
			next = NULL;
		}
};

The basic operation of a doubly linked list

Achieve a doubly linked list insertion head node, tail node insertion, the insertion position of the node specified to establish the list to find a specified position given value, deleting the node designated location, modify the position of the node specified, the length of the doubly linked list, Print doubly linked list, followed by one by one to explain achieve:

Header insertion node list established

First Node bidirectional linked list implementation, to achieve head insertion node can be divided into two cases, one is only a first node when only necessary head and newNode the two pointers can be associated with the other two pointer is still NULL state. Another case is that there are nodes of the case, this time with the node is inserted in the middle of similar, need to be adjusted four pointers, first let newNode associated with the successor node, and let newNode the head associated with the node.
Thus, the head insertion node implemented as follows:

template<class T>
bool doubleLinkedList<T>::insertNodeByhead(T item)
{
	//创建一个新的结点
	doubleLinkedListNode<T>* newNode = new doubleLinkedListNode<T>(item);
	if (newNode == NULL){
		cout << "内存分配失败,新结点无法创建" << endl;
		return false;
	}
	//分两种情况,head的next是否为NULL,然后处理四个指针
	if(head->next == NULL)
	{
		head->next = newNode;
		newNode->prior = head;
		return true;
	}
	else{
		newNode->next = head->next;
		head->next->prior = newNode;
		newNode->prior = head;
		head->next = newNode;
		return true;
	}
}

Tail insertion node list established

Node is inserted at the end, of course, a first step is to find the last node, and then thereafter inserted, both ends of the doubly linked list because the pointer is pointing to NULL , it is inserted only at the end of two pointers need to be adjusted on ok.

template<class T>
bool doubleLinkedList<T>::insertNodeBytail(T item)
{
	//创建一个新的结点
	doubleLinkedListNode<T>* newNode = new doubleLinkedListNode<T>(item);
	if (newNode == NULL){
		cout << "内存分配失败,新结点无法创建" << endl;
		return false;
	}
	//首先找到最后一个结点
	doubleLinkedListNode<T>* lastNode = head;
	while(lastNode->next != NULL)
	{
		lastNode = lastNode->next;//没找到就一直循环
	}
	//找到调整指针
	lastNode->next = newNode;
	newNode->prior = lastNode;
	return true;
}

Achieve the specified position of the insertion node

Only need to insert a two-step at the specified location, the first and find the specified location, then adjust the pointer is to insert a new node, intervening is the most complex, can not escape adjust four pointers, but first still make new knot point and the successor node to establish the relevance and let the new node is inserted into a relationship with a predecessor node to achieve a new node.

bool doubleLinkedList<T>::insertNode(T item,int n)
{
	if(n<1){
		cout<<"输入的非有效位置!"<<endl;
		return false;
	}
	doubleLinkedListNode<T>* pMove = head;//创建一个新的指针,设置为游标指针
	//首先找到插入位置
	for(int i=1;i<n;i++)
	{
		pMove = pMove->next;
		if(pMove == NULL&& i<=n)
		{
			cout<<"插入位置无效!"<<endl;
			return false;
		}
	}
	//创建一个新的结点
	doubleLinkedListNode<T>* newNode = new doubleLinkedListNode<T>(item);
	if (newNode == NULL){
		cout << "内存分配失败,新结点无法创建" << endl;
		return false;
	}
	//插入新的结点
	newNode->next = pMove->next;   
	if (pMove->next != NULL)
	{
		pMove->next->prior = newNode;
	}
	newNode->prior = pMove;
	pMove->next = newNode;
	return true;
}

Finding the existence of a given value

Find a given element, which is a linked list traversal process, the next node from scratch node traversing, after all, the first head node is not stored data items.

template<class T>
bool doubleLinkedList<T>::findData(T item)
{
	doubleLinkedListNode<T> *pMove = head->next;  //设置游标指针
	if(pMove == NULL)//链表为空
	{
		return false;
	}
	while(pMove)//遍历链表
	{
		if(pMove->data == item){
			return true;
		}
		pMove = pMove->next;
	}
	return false;
}

Node delete the specified location

Delete the specified node, the first step to find the node to delete, delete the need to define a temporary pointer pointing to the node to be deleted, do not forget to delete pointer processing after the last release of the node space.

template<class T>
bool doubleLinkedList<T>::deleteData(int n)
{
	if (n<1||n>getLength())
	{
		cout << "输入非有效位置" << endl;
		return false;
	}
	doubleLinkedListNode<T> * pMove = head;//设置游标指针
	doubleLinkedListNode<T> * pDelete;             
	//查找删除结点的位置
	for (int i = 1; i <= n; i++)
	{
		pMove = pMove->next;                                         //游标指针后移
	}
	//删除结点
	pDelete = pMove;      
	pMove->prior->next = pDelete->next;
	pMove->next->prior = pDelete->prior;
	delete pDelete;//释放空间
	return true;
}

Junction modifying the specified position

Node data modification specified position, of course, still have to find the specified location, and then modify it, after modifying the original data returned as a reference, the specific usage in the test write function, may not be used as a reference .

template<class T>
bool doubleLinkedList<T>::changeListElements(int n,T item,T &x)
{
	if (n<1||n>getLength())
	{
		cout << "输入非有效位置" << endl;
		return false;
	}
	doubleLinkedListNode<T> *pMove = head->next;  //设置游标指针
	for(int i=1;i<n;i++)//找到指定位置1
	{
		pMove = pMove->next;
	}
	x = pMove->data;
	pMove->data = item;
	return true;
}

Length two-way linked list

Calculating a function of the length of the doubly linked list, a doubly linked list in a package private members of a variable length , in order to record the length of the doubly linked list, a doubly linked list traversal, the number of nodes is calculated one by one length of a doubly linked list.

template<class T>
int doubleLinkedList<T>::getLength()
{
	doubleLinkedListNode<T> *pMove = head->next;  //设置游标指针
	int length=0;
	//遍历链表,计算结点数
	while(pMove!=NULL)
	{
		pMove = pMove->next;  //游标指针后移
		length++;       //计算length
	}
	return length;
}

Print doubly linked list

Print doubly linked list, beginning from the second node traverse the list, because the first one of the first node is no data, the printing process is a process of traversal.

template<class T>
void doubleLinkedList<T>::printLinkedlist()
{
	//从第二个结点开始打印,表头不含数据
	doubleLinkedListNode<T>* pMove = head->next;
	while(pMove)
	{
		cout<<pMove->data<<" ";
		pMove = pMove->next;//移动指针
	}
	cout<<endl;
}

Those are the times Bowen and share the use of C ++ language doubly linked list, after writing in C language, writing feel it is more relaxed, the only difference is to use class to encapsulate. The complete code, and test the code I've Push to Github, like a small partner welcome Star! C ++ language doubly linked list Github address , if you also want to understand the small partners other data structure to achieve can also be my MyBlog , we together discussion, if there is a bad place to write is also please be magnanimous, but also welcome in the comments area, I correct them, and common progress!

Published 19 original articles · won praise 8 · views 603

Guess you like

Origin blog.csdn.net/qq_43336390/article/details/104153964