数据结构---双向链表

双向链表的三个重要组成部分:
1.数据域
2.前指针域
3.后指针域

基本操作:
1.初始化列表
2.前插法
3.后插法
4.任意位置插入
5.删除元素
感想:与单链表的区别的就是多了一个前指针.其他的没有太大的变化
1.初始化:

typedef struct DBLink {
    
    
	int date;
	struct DBLink* prew;
	struct DBLink* next;
}DBLinkList, DBLinkNode;
bool Dbinit_Link(DBLinkList*& L) {
    
    
	//初始化
	L = new DBLinkList;
	if (!L) return false;
	L->date = -1;
	L->next = NULL;
	L->prew = NULL;
	return true;
}

2.前插法:
如果是没有元素,那么就使头结点指向新节点
新节点的前指针指向头结点
如果已经有了其他的元素了,那么就使新元素的尾指针指,向头节点原来指向的节点,让头节点指向新节点
再是原来的第二个节点的头节点指向新节点,新节点的前指针指向头节点
在这里插入图片描述

bool DbLinkInserct_front(DBLinkList*& L, DBLinkNode* s) {
    
    
	if (!L || !s) return false;
	if (L->next == NULL) {
    
    
		L->next = s;
		s->prew = L;
		s->next = NULL;
	}
	else {
    
    
		s->next = L->next;
		L->next = s;
		s->next->prew = s;
		s->prew = L;
	}
	return true;
}

3.后插法

bool DbLinkInserct_back(DBLinkList*& L, DBLinkNode* s) {
    
    
	if (!L || !s) return false;
	DBLinkNode* p;
	p = L;
	while (p->next != NULL) {
    
    
		p = p->next;
	}
	p->next = s;
	s->prew = p;
	s->next = NULL;
	return true;
}

4.任意插入:

bool DbLinkInserct(DBLinkList*& L, int index, DBLinkNode* s) {
    
    
	if (!L || !s) return false;
	int i = 0;
	DBLinkList* p = L;
	while (p && i < index) {
    
    
		p = p->next;
		i++;
	}
	if (!p) return false;
	p->prew->next = s;
	s->next = p;
	s->prew = p->prew;
	p->prew = s;
	return true;
}

5.删除元素:

bool DbLinkDelete(DBLinkList*& L, int index) {
    
    
	if (!L) return false;
	int i = 1;
	DBLinkList* p = L->next;
	DBLinkNode* q = NULL;
	while (p && i < index) {
    
    
		p = p->next;
		i++;
	}
	p->prew->next = p->next;
	if (p->next)
		p->next->prew = p->prew;
	delete p;
	return true;
}

全部源代码;

#include<Windows.h>
#include<iostream>
using namespace std;
typedef struct DBLink {
    
    
	int date;
	struct DBLink* prew;
	struct DBLink* next;
}DBLinkList, DBLinkNode;
bool Dbinit_Link(DBLinkList*& L) {
    
    
	//初始化
	L = new DBLinkList;
	if (!L) return false;
	L->date = -1;
	L->next = NULL;
	L->prew = NULL;
	return true;
}
bool DbLinkInserct_front(DBLinkList*& L, DBLinkNode* s) {
    
    
	if (!L || !s) return false;
	if (L->next == NULL) {
    
    
		L->next = s;
		s->prew = L;
		s->next = NULL;
	}
	else {
    
    
		s->next = L->next;
		L->next = s;
		s->next->prew = s;
		s->prew = L;
	}
	return true;
}
bool DbLinkInserct_back(DBLinkList*& L, DBLinkNode* s) {
    
    
	if (!L || !s) return false;
	DBLinkNode* p;
	p = L;
	while (p->next != NULL) {
    
    
		p = p->next;
	}
	p->next = s;
	s->prew = p;
	s->next = NULL;
	return true;
}
bool DBLink_print(DBLinkList*& L) {
    
    
	DBLinkNode* p = NULL;
	p = L->next;
	while (p) {
    
    
		cout << p->date << "\t";
		p = p->next;
	}
	cout << endl;
	return 0;
}
bool DbLinkInserct(DBLinkList*& L, int index, DBLinkNode* s) {
    
    
	if (!L || !s) return false;
	int i = 0;
	DBLinkList* p = L;
	while (p && i < index) {
    
    
		p = p->next;
		i++;
	}
	if (!p) return false;
	p->prew->next = s;
	s->next = p;
	s->prew = p->prew;
	p->prew = s;
	return true;
}
bool DbLinkDelete(DBLinkList*& L, int index) {
    
    
	if (!L) return false;
	int i = 1;
	DBLinkList* p = L->next;
	DBLinkNode* q = NULL;
	while (p && i < index) {
    
    
		p = p->next;
		i++;
	}
	p->prew->next = p->next;
	if (p->next)
		p->next->prew = p->prew;
	delete p;
	return true;
}
void DBLink_Destroy(DBLinkList*& L) {
    
    
	DBLinkNode* p = L;
	cout << "销毁链表!" << endl;
	while (L) {
    
    
		L = L->next;
		delete p;
		p = L;
	}
}	
int main() {
    
    
	DBLinkList* L;
	DBLinkNode* s;
	int n;
	Dbinit_Link(L);
	//1.前插法

	cout << "输入要前插入的数据个数:" << endl;
	cin >> n;
	while (n) {
    
    
		s = new DBLinkNode;
		cout << "请输入要插入的数据:";
		cin >> s->date;
		if (DbLinkInserct_front(L, s)) {
    
    
			cout << "插入成功!" << endl;
		}
		else {
    
    
			cout << "插入失败!" << endl;
		}
		n--;
	}
	//2.输出
	DBLink_print(L);
	/*x
	//3.后插法
	cout << "输入要后插入的数据个数:" << endl;
	cin >> n;
	while (n) {
		s = new DBLinkNode;
		cout << "请输入要插入的数据:";
		cin >> s->date;
		if (DbLinkInserct_back(L, s)) {
			cout << "插入成功!" << endl;
		}
		else {
			cout << "插入失败!" << endl;
		}
		n--;
	}
	DBLink_print(L);
	*/
	//4.任意位置插入
	int pos;
	cout << "要插入的个数:";
	cin >> n;
	while (n) {
    
    
		s = new DBLinkNode;
		cout << "输入要插入的位置和元素:";
		cin >> pos >> s->date;
		if (DbLinkInserct(L, pos, s)) {
    
    
			cout << "插入成功!" << endl;
			DBLink_print(L);
		}
		else {
    
    
			cout << "插入失败!" << endl;
		}
		n--;
	}
	//5.删除元素
	cout << "输入要删除的元素的位置:";
	cin >> n;
	if (DbLinkDelete(L, n)) {
    
    
		cout << "删除成功!" << endl;
	}
	else {
    
    
		cout << "删除失败!" << endl;
	}
	DBLink_print(L);
	DBLink_Destroy(L);
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_49324123/article/details/111246244