20201016 double linked list of data structure and algorithm

**The structure of two-way linked list is as follows:**

typedef struct _dbLinkList{
 type data;
 _dbLinkList* pre;
 _dbLinkList* next; 
}dbLinkList,dbLinkNode;

**Features of two-way linked list: * * it can be seen from the structure that two-way linked list inherits the features of one-way linked list. At the same time, compared with one-way linked list, its composition has an additional pre pointer to the previous element, with reverse traceability.
**Function call interface: * * its structure function initialization, tail insertion, query element, add element, destroy linked list are the same as one-way linked list,
However, there are some differences between prefiltering and deleting element interface functions.
See the interface function code Notes for details.

bool dbLinkListInsertFront(dbLinkList*& link, dbLinkList* &node) {
	if (!link || !node) return false;
	if (!link->next) {//When the list is empty
		node->next = NULL;
		node->pre = link;
		link->next = node;
	}
	else {//The second node is extrapolated by the head node, so the pointer associated with the second node must be assigned before the original value can be changed
		node->next = link->next;//Point the new node to the second node
		link->next->pre = node;	//First point the second node to the new node	  
	    
		link->next = node;//Point the first node to the new node 
		node->pre = link;	//Point the new node to the first node				
	}
	return true;
}

The above call functions of forward insertion method are divided into null list insertion and non null list insertion. At the same time, when non null list is inserted, the order of the pointers before and after the connection is very important, and the extrapolated nodes must be assigned first.
The following is the delete element function, which is similar to the forward interpolation function. It can be divided into two cases: the deleted node is the tail node and the non tail node.

bool dbLinkListDeleteEle(dbLinkList* &link, int i) {
	if (!link) return false;
	dbLinkList* p = link;
	int pos = 0;
	while (p && pos < i) {
		p = p->next;
		pos++;
	}
	if (!p || pos > i) return false;
	if (!p->next) {//The tail node is deleted
		p->pre->next = NULL;
	}
	else {//Delete non tail node
		p->next->pre = p->pre;
		p->pre->next = p->next;
	}
	
	delete p;
	return true;
}

Other general functions and debugging codes are as follows (including the previous interface program, which can be used for pro testing)

#include<Windows.h>
#include<iostream>
using namespace std;

typedef struct _dbLinkList {
	int data;
	_dbLinkList* pre;
	_dbLinkList* next;
}dbLinkListNode,dbLinkList;
//Function realization
bool initdbLinkList(dbLinkList* &link);//Bidirectional list initialization
bool dbLinkListInsertFront(dbLinkList*& link, dbLinkList* &node);//Forward insertion of linked list
bool dbLinkListPrint(dbLinkList*& link);//Print list
bool dbLinkListInsertBack(dbLinkList* &link,dbLinkList* &node);//Chain table tail insertion method
bool dbLinkListInsert(dbLinkList* &link,int i,dbLinkList* &node);//Insert anywhere
bool dbLinkListGetEle(dbLinkList*& link, int i, dbLinkList*& node);//Get element at position i
//Change the data type to query as needed
bool dbLinkListFindEle(dbLinkList*& link, int e);//Query whether there is element e
bool dbLinkListDeleteEle(dbLinkList*& link, int i);//Delete data at position i
void dbLinkListDestroy(dbLinkList*& link);//Destroy linked list
int main() {
	dbLinkList* link;
	dbLinkListNode* node;
	initdbLinkList(link);
	int cout;
	std::cout << "To insert data by forward interpolation, enter the number of data to insert:" << std::endl;
	cin >> cout;
	for (int i = 0; i < cout; i++) {
		node = new dbLinkListNode;
		cin >> node->data;
		dbLinkListInsertFront(link, node);
	}
	dbLinkListPrint(link);

	std::cout << "**************To insert data by tail insertion, enter the number of data to insert:***********" << std::endl;
	cin >> cout;
	for (int i = 0; i < cout; i++) {
		node = new dbLinkListNode;
		cin >> node->data;
		dbLinkListInsertBack(link, node);
	}
	dbLinkListPrint(link);

	std::cout << "**************To insert data anywhere, enter the location and data to insert:***********" << std::endl;
	cin >> cout;	
	node = new dbLinkListNode;
	cin >> node->data;
	if (dbLinkListInsert(link, cout, node)) {
		std::cout << "Insert successfully!" << std::endl;
		dbLinkListPrint(link);
	}
	else {
		std::cout << "Insert failed!" << std::endl;
	}

	std::cout <<endl<< "**************To get the data of the specified location, enter the location to get:***********" << std::endl;
	cin >> cout;
	node = new dbLinkListNode;
	if (dbLinkListGetEle(link, cout, node)) {
		std::cout << "Success! The first"<<cout<<"Bit data is:"<<node->data << std::endl;	
	}
	else {
		std::cout << "Get failed!" << std::endl;
	}
	std::cout << endl << "**************Query whether there is specified data***********" << std::endl;
	cout = 4;
	if (dbLinkListFindEle(link,cout)) {
		std::cout << "Query succeeded!" << std::endl;
	}
	else {
		std::cout << "Query failed!" << std::endl;
	}

	std::cout << endl << "**************Delete data at specified location,Please enter the location***********" << std::endl;
	cin>>cout;
	if (dbLinkListDeleteEle(link, cout)) {
		std::cout << "Delete successfully!" << std::endl;
		dbLinkListPrint(link);
	}
	else {
		std::cout << "Delete failed!" << std::endl;
	}

	
	dbLinkListDestroy(link);
	system("pause");
	return 0;
}
bool initdbLinkList(dbLinkList* &link) {
	link = new dbLinkList;
	if (!link) return false;
	link->pre = NULL;
	link->next = NULL;
	link->data = -1;
	return true;
}
bool dbLinkListInsertFront(dbLinkList*& link, dbLinkList* &node) {
	if (!link || !node) return false;
	if (!link->next) {//When the list is empty
		node->next = NULL;
		node->pre = link;
		link->next = node;
	}
	else {//The second node is extrapolated by the head node, so the pointer associated with the second node must be assigned before the original value can be changed
		node->next = link->next;//Point the new node to the second node
		link->next->pre = node;	//First point the second node to the new node	  
	    
		link->next = node;//Point the first node to the new node 
		node->pre = link;	//Point the new node to the first node				
	}
	return true;
}
bool dbLinkListPrint(dbLinkList*& link) {
	if (!link) return false;
	dbLinkList* p = link;
	while (p->next) {
		p = p->next;
		cout << p->data << " ";
	}
	cout << endl;
	cout << "Print double linked list in reverse!" << endl;
	while (p!=link) {
		cout << p->data << " ";
		p = p->pre;
	}
	return true;
}
bool dbLinkListInsertBack(dbLinkList*& link, dbLinkList*& node) {
	if (!link || !node) return false;
	dbLinkList* p = link;
	while (p->next != NULL) {
		p = p->next;
	}
	p->next = node;
	node->pre = p;
	node->next = NULL;
	return true;
}
bool dbLinkListInsert(dbLinkList*& link, int i, dbLinkList*& node) {
	if (!link || !node) return false;
	dbLinkList* p = link;
	int pos = 0;
	while (p && pos < i - 1) {//Find the i-1 node
		p = p->next;
		pos++;
	}
	if (pos > i - 1 || !p) return false;
	
	p->next->pre = node;
	node->next = p->next;
	p->next = node;
	node->pre = p;
	
	return true;	
}
bool dbLinkListGetEle(dbLinkList*& link, int i, dbLinkList*& node) {
	if (!link || !node) return false;
	dbLinkList* p = link;
	int pos = 0;
	while (p && pos < i) {
		p = p->next;
		pos++;
	}
	if (!p || pos > i) return false;
	node = p;
	node->next = p->next;
	node->pre = p->pre;
	return true;
}
bool dbLinkListFindEle(dbLinkList*& link, int e) {
	if (!link) return false;
	dbLinkList* p = link;
	while (p && p->data != e) {
		p = p->next;
	}
	if (!p || p==link)  return false;
	else  return true;
}
bool dbLinkListDeleteEle(dbLinkList* &link, int i) {
	if (!link) return false;
	dbLinkList* p = link;
	int pos = 0;
	while (p && pos < i) {
		p = p->next;
		pos++;
	}
	if (!p || pos > i) return false;
	if (!p->next) {//The tail node is deleted
		p->pre->next = NULL;
	}
	else {//Delete non tail node
		p->next->pre = p->pre;
		p->pre->next = p->next;
	}
	
	delete p;
	return true;
}
void dbLinkListDestroy(dbLinkList*& link) {
	if (!link) return;
	dbLinkList* p = link;
	while (p->next) {
		link = p->next;
		delete p;
		p = link;
	}
	delete p;
}
48 original articles published, praised 0, and 347 visitors
Private letter follow

Tags: Windows

Posted on Thu, 16 Jan 2020 10:37:06 -0500 by mike760534211