ProDeveloperTutorial.com

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

对循环单链接列表(C ++中的解决方案)执行以下操作

前开发者教程 2019年2月24日

1.将单链接列表转换为循环链接列表。

2.检查循环链接列表中的节点数

3.检查给定列表是否为循环链接列表。

 

1.将单链接列表转换为循环链接列表

为此,我们需要遍历列表的末尾并将最后一个节点的下一个分配给列表的开头

2.检查循环链接列表中的节点数

为此,我们取一个临时节点,并开始计算节点数,直到临时节点不等于头节点为止。

3.检查给定列表是否为循环链接列表。

为此,我们需要一个临时节点并开始遍历列表,直到临时节点不等于head为止,或者在非循环LL情况下直到临时节点为空。

 

C ++解决方案

#include<iostream>
#include<vector>
#include<string>

using namespace std;

struct Node
{
	int data;
	Node *next;

	Node(int x)
	{
		data = x;
		next = NULL;
	}
};


void print_list(Node *head)
{
	Node *start = head;

	while(start)
	{
		cout<<start->data<<" -> ";
		start = start->next;
	}
	cout<<"\n";
}

void make_linked_list_circular(Node *head)
{
	Node *start = head;

	while(head -> next != NULL)
		head = head -> next;

	head -> next = start;
}

void count_nodes_circular_linked_list(Node *head)
{
	Node *temp = head -> next;
	int count = 1;

	while(temp != head)
	{
		count++;
		temp = temp -> next;
	}

	cout<<"\n\nThe total number of nodes in circular linked list is "<< count << endl;
}

void check_if_list_is_circular_linked_list(Node *head)
{
	Node *temp = head -> next;

	while(temp != NULL && temp != head)
		temp = temp -> next;

	if (temp == head)
		cout<<"The list is circular linked list"<<endl;
	else
		cout<<"The list is not circular linked list"<<endl;
}

int main()
{
	Node *head = new Node(2);
	head -> next = new Node(1);
	head -> next -> next = new Node(4);
	head -> next -> next -> next = new Node(3);
	head -> next -> next -> next  -> next = new Node(6);
	head -> next -> next -> next  -> next -> next = new Node(5);

	cout<<"The original list = "<<endl;
	print_list(head);

	cout<<"Check if the list is circular"<<endl;;
	check_if_list_is_circular_linked_list(head);


	make_linked_list_circular(head);

	cout<<"\n\nAfter making list as circular, check if the list is circular "<<endl;
	check_if_list_is_circular_linked_list(head);

	count_nodes_circular_linked_list(head);


	return 0;
}


输出:

The original list =
2 -> 1 -> 4 -> 3 -> 6 -> 5 ->

Check if the list is circular
The list is not circular linked list

After making list as circular, check if the list is circular
The list is circular linked list

The total number of nodes in circular linked list is 6

 

该网站上可用的教程列表:

C编程20+章C ++编程80+章
100多个编码问题数据结构和算法85+章
系统设计20+章Shell脚本编写12章
4g LTE 60+章节最常见的编码问题
5G NR 50+章Linux系统编程20+章
分享
电子邮件
鸣叫
领英
Reddit
绊倒
Pinterest的
上一篇文章
下一篇

关于作者

前开发者教程

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

ProDeveloperTutorial.com

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


        <figcaption id="k4Twf4e"><hr class="Nad1hAQ"><aside id="iAhRFH9"><caption id="FeyzHcY" class="Fja6bra"></caption></aside></hr></figcaption>

      1. <nav class="OlNkj67"><figure id="tjp8nxj"></figure></nav>

        <sub id="LAtJVvE" class="LvduCXz"><p><q id="EZD6kec" class="EiKj01O"></q></p></sub>


                1. <figcaption id="JMjqFl7" class="JLZGOnl"><basefont id="YLylqej" class="Y9Pj2IO"><address class="OJ6VDSl"></address></basefont></figcaption>
                  <textarea class="RaxUV8h"><applet class="R77a9bN"></applet></textarea>