ProDeveloperTutorial.com

教程和编程解决方案
菜单
  • Shell脚本
  • 系统设计
  • Linux系统编程
  • 4g LTE
  • 编码问题
  • C
  • C ++
  • DSA
  • GIT

数据结构教程4:双链表[DLL],并附有C ++中的实现说明

前开发者教程 2019年5月19日

在上一章中,我们了解了单个链表。在本章中,我们将学习双链表。

双链表是一种特殊的数据结构,它是零个或多个国彩网的集合。

每个国彩网由3个部分组成,“ prev_link +数据+ next_link”。由于它是一个双链表,因此我们需要存储上一个国彩网和下一个国彩网的数据。

prev_link: 用于保存前一国彩网的信息。

数据: 它用于存储一些值。它可以是整数变量或结构。

next_link: 用于保存下一个国彩网的信息。

与“ prev_link +数据+ next_link”一起称为国彩网。

下面是我们如何表示一个双重LL国彩网:

双链表[DLL]以及C ++中的实现说明

具有多个国彩网的DLL可以显示如下:

双链表[DLL]以及C ++中的实现说明

 

从上图可以推断出以下几点:

  1. 第一个国彩网prev_link始终为null
  2. 最后一个国彩网next_link将始终为null
  3. 数据部分保存数据
  4. 头指针始终指向第一个国彩网

以下是我们将在DLL中学习的操作

  1. 在后面插入元素。
  2. 删除具有给定键值的元素。
  3. 搜索一个元素。
  4. 显示所有数据。

1.在后面插入元件。

在后面插入元件时,我们需要检查2个条件。

  1. 如果头国彩网为NULL

如果头国彩网为NULL,则表示我们要插入的元素是第一个元素。因此,我们执行以下步骤:

  1. 将值复制到数据部分
  2. 使prev_link和next_link指针指向NULL。

双链表[DLL]以及C ++中的实现说明

2.如果头国彩网不为NULL

 

如果head元素不为NULL,则意味着已经存在元素。因此,我们执行以下步骤在后部插入。

  1. 取一个新国彩网并复制该值。
  2. 拿一个临时指针,遍历列表的末尾。
  3. 使新国彩网的上一个指针指向临时国彩网
  4. 使新国彩网的下一个指针指向NULL。

双链表[DLL]以及C ++中的实现说明

2.删除具有给定键值的元素。

要删除,请按照以下步骤操作:

  1. 将临时指针移到该点,直到找到与该键相等的元素。
  2. 如果该元素是最小元素,则使上一个元素的下一个指针为NULL。
  3. 否则,请执行以下操作:
    1. 临时>prev->next = 临时>next;
    2. 临时>next->prev = 临时>prev;
  4. 删除临时国彩网。

3.搜索一个元素。

要搜索元素,我们执行以下步骤:

  1. 以临时指针指向头部
  2. 检查数据是否与密钥相同
  3. 如果不是,请将临时指针移至下一个国彩网
  4. 执行步骤2和3,直到找到密钥。

4.显示所有数据。

要显示,我们执行以下步骤:

  1. 以临时指针指向头部
  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+章
分享
电子邮件
鸣叫
领英
Reddit
绊倒
Pinterest的
上一篇文章
下一篇

关于作者

前开发者教程

每天我们都会讨论竞争性编程问题,请加入我们的网站:   电报频道

ProDeveloperTutorial.com

教程和编程解决方案
版权© 2020 ProDeveloperTutorial.com
从以下课程获得热门课程: 教育性的

  • <dt class="OQ9Vjmx"><thead class="W0E4rvv"><dt class="FStNQgM"></dt></thead></dt>

          <keygen id="pU8l8pu" class="p3sb67D"></keygen>

          <td id="Tbs0o4F" class="TeocsWd"></td>

          <keygen id="R6Rj0sD" class="R2lGXuQ"></keygen>
          <button class="JhQaiiB"></button>


                <article id="dTo91qB" class="dZcQ7bj"></article>


              1. <object id="j9w0nfF"><select id="vwzkEYF"></select></object>
              2. <col id="Ba94Grp"></col>