在上一章中,我们了解了单个链表。在本章中,我们将学习双链表。
双链表是一种特殊的数据结构,它是零个或多个国彩网的集合。
每个国彩网由3个部分组成,“ prev_link +数据+ next_link”。由于它是一个双链表,因此我们需要存储上一个国彩网和下一个国彩网的数据。
prev_link: 用于保存前一国彩网的信息。
数据: 它用于存储一些值。它可以是整数变量或结构。
next_link: 用于保存下一个国彩网的信息。
与“ prev_link +数据+ next_link”一起称为国彩网。
下面是我们如何表示一个双重LL国彩网:
具有多个国彩网的DLL可以显示如下:
从上图可以推断出以下几点:
- 第一个国彩网prev_link始终为null
- 最后一个国彩网next_link将始终为null
- 数据部分保存数据
- 头指针始终指向第一个国彩网
以下是我们将在DLL中学习的操作
- 在后面插入元素。
- 删除具有给定键值的元素。
- 搜索一个元素。
- 显示所有数据。
1.在后面插入元件。
在后面插入元件时,我们需要检查2个条件。
- 如果头国彩网为NULL
如果头国彩网为NULL,则表示我们要插入的元素是第一个元素。因此,我们执行以下步骤:
- 将值复制到数据部分
- 使prev_link和next_link指针指向NULL。
2.如果头国彩网不为NULL
如果head元素不为NULL,则意味着已经存在元素。因此,我们执行以下步骤在后部插入。
- 取一个新国彩网并复制该值。
- 拿一个临时指针,遍历列表的末尾。
- 使新国彩网的上一个指针指向临时国彩网
- 使新国彩网的下一个指针指向NULL。
2.删除具有给定键值的元素。
要删除,请按照以下步骤操作:
- 将临时指针移到该点,直到找到与该键相等的元素。
- 如果该元素是最小元素,则使上一个元素的下一个指针为NULL。
- 否则,请执行以下操作:
- 临时>prev->next = 临时>next;
- 临时>next->prev = 临时>prev;
- 删除临时国彩网。
3.搜索一个元素。
要搜索元素,我们执行以下步骤:
- 以临时指针指向头部
- 检查数据是否与密钥相同
- 如果不是,请将临时指针移至下一个国彩网
- 执行步骤2和3,直到找到密钥。
4.显示所有数据。
要显示,我们执行以下步骤:
- 以临时指针指向头部
- 显示数据
- 将临时指针移到下一个国彩网
- 执行步骤2和3,直到到达LL的尽头。
使用C ++实现双链表
#include<iostream> using namespace std; struct Node { int val; Node *prev; Node *next; }; Node *head; void insert_rear(int value) { Node *temp = head; if (head != NULL) { // if head is not null while(temp->next != NULL) { temp = temp ->next; } Node *newNode = new Node; temp->next = newNode; newNode->prev = temp; newNode->val = value; newNode->next = NULL; } else { //if head is nul Node *newNode = new Node; newNode->val = value; newNode->prev = NULL; newNode->next = NULL; head = newNode; } } void remove(int x) { Node *temp = head; while(temp->val != x) { temp = 临时>next; } if(temp -> next == NULL) { temp->prev->next = NULL; } else { temp->prev->next = 临时>next; temp->next->prev = 临时>prev; } delete temp; } void search(int x) { Node *temp = head; int found = 0; while(temp != NULL) { if(temp->val == x) { cout<<"\nFound"; found = 1; break; } temp = 临时>next; } if(found==0) { cout<<"\nNot Found"; } } void display() { Node *temp =head; while(temp !=NULL) { cout<< 临时>val<<"\t"; temp = 临时>next; } } int main() { int choice, x; do { cout<<"\n1. Insert"; cout<<"\n2. Delete"; cout<<"\n3. Search"; cout<<"\n4. Display"; cout<<"\n5. Exit"; cout<<"\n\nEnter you choice : "; cin>>choice; switch (choice) { case 1 : cout<<"\nEnter the element to be inserted at rear : "; cin>>x;; insert_rear(x); break; case 2 : cout<<"\nEnter the element to be removed : "; cin>>x; remove(x); break; case 3 : cout<<"\nEnter the element to be searched : "; cin>>x; search(x); break; case 4 : display(); break; } } while(choice != 5); return 0; }
输出:
1. Insert 2. Delete 3. Search 4. Display 5. Exit Enter you choice : 1 Enter the element to be inserted at rear : 1 1. Insert 2. Delete 3. Search 4. Display 5. Exit Enter you choice : 1 Enter the element to be inserted at rear : 2 1. Insert 2. Delete 3. Search 4. Display 5. Exit Enter you choice : 1 Enter the element to be inserted at rear : 3 1. Insert 2. Delete 3. Search 4. Display 5. Exit Enter you choice : 1 Enter the element to be inserted at rear : 4 1. Insert 2. Delete 3. Search 4. Display 5. Exit Enter you choice : 4 1 2 3 4 1. Insert 2. Delete 3. Search 4. Display 5. Exit Enter you choice : 3 Enter the element to be searched : 3 Found 1. Insert 2. Delete 3. Search 4. Display 5. Exit Enter you choice : 2 Enter the element to be removed : 3 1. Insert 2. Delete 3. Search 4. Display 5. Exit Enter you choice : 4 1 2 4 1. Insert 2. Delete 3. Search 4. Display 5. Exit Enter you choice : 5
进一步阅读:
AJ关于DS和算法的权威指南。单击此处以学习算法和数据结构教程的完整列表。 85多个章节可供学习。
该网站上可用的教程列表:
C编程20+章 | C ++编程80+章 |
100多个编码问题 | 数据结构和算法85+章 |
系统设计20+章 | Shell脚本编写12章 |
4g LTE 60+章节 | 最常见的编码问题 |
5G NR 50+章 | Linux系统编程20+章 |