博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
微信小程序 查找兄弟节点_使用C ++程序在链接列表中查找节点
阅读量:2535 次
发布时间:2019-05-11

本文共 4239 字,大约阅读时间需要 14 分钟。

微信小程序 查找兄弟节点

Given a linked list and an integer N, you need to find and return index where N is present in the Linked List. Return -1 if n is not present in the Linked List.

给定一个链表和一个整数N,您需要查找并返回索引,其中链表中存在N。 如果n在链接列表中不存在,则返回-1。

Indexing of nodes starts from 0.

节点的索引从0开始。

Input format:    Line 1: Linked list elements (separated by space and terminated by -1)    Line 2: Integer n     Output format:    Index

Example:

例:

Sample Input 1:    3 4 5 2 6 1 9 -1    5    Sample Output 1:    2    Sample Input 2:    3 4 5 2 6 1 9 -1    6    Sample Output 2:    4

Description:

描述:

In this question, we are given a linked list and a data. We have to find the index of the Node which contains the data.

在这个问题中,我们得到了一个链表和一个数据。 我们必须找到包含数据的Node的索引

Example:

例:

2->1->5->4->3->NULL    In this list 5 is at 2nd index.

Solution explanation:

解决方案说明:

In this question, we define a temp pointer and equating it with head of the Linked List. We have to keep an integer index keeping the track of the temp node. We keep on traversing while temp != NULL. On each traversal, we check whether temp's data and the user's data. If they are equal, we return the index. Else we increment index by 1 and temp to temp-> next.

在这个问题中,我们定义了一个临时指针,并将其与“链表”的头部相等。 我们必须保留一个整数索引来跟踪临时节点。 当temp!= NULL时,我们继续遍历。 在每次遍历时,我们都会检查temp的数据和用户的数据。 如果它们相等,则返回index 。 否则,我们将index递增1,并将temp递增到temp-> next 。

At last if we don’t find the element, we return -1.

最后,如果找不到该元素,则返回-1。

Algorithm:

算法:

  • Step 1: Declare the recursive function with parameters (Node * head, int data)

    步骤1:使用参数(Node * head,int数据)声明递归函数

  • Step 2: Put Node *temp = head, int index = 0;

    步骤2:将Node * temp = head , int index = 0;

  • Step 3: Repeat Step 4 and 5 while (temp!= NULL)

    步骤3: 在(temp!= NULL)的同时重复步骤4和5 。

  • Step 4: if(temp -> data == data) return index

    步骤4: if(temp-> data == data)返回索引

  • Step 5: else index++ and temp = temp->next;

    步骤5:else index ++和temp = temp-> next;

  • Step 6: return -1

    步骤6:传回-1

Function:

功能:

//Function for finding the nodeint findNodeInLL(Node* head, int data){	//Used to keep track of the Node Index	int index = 0;              	Node * temp = head;	//LinkedList traversal for finding the node	while(temp!=NULL){		if(temp->data == data){         			//If element found return index			return index;               		}		temp = temp->next;		index++;	}   	//If element not found	return -1;                  }

C++ code

C ++代码

#include 
using namespace std;struct Node{
// linked list Node int data; Node * next;};Node *newNode(int k){
//defining new node Node *temp = (Node*)malloc(sizeof(Node)); temp->data = k; temp->next = NULL; return temp; }//Used to add new node at the end of the listNode *addNode(Node* head, int k){
if(head == NULL){
head = newNode(k); } else{
Node * temp = head; Node * node = newNode(k); while(temp->next!= NULL){
temp = temp->next; } temp-> next = node; } return head;}// Used to create new linked list and return headNode *createNewLL(){
int cont = 1; int data; Node* head = NULL; while(cont){
cout<<"Enter the data of the Node"<
>data; head = addNode(head,data); cout<<"Do you want to continue?(0/1)"<
>cont; } return head;}//Function for finding the nodeint findNodeInLL(Node* head, int data){
//Used to keep track of the Node Index int index = 0; Node * temp = head; //LinkedList traversal for finding the node while(temp!=NULL){
if(temp->data == data){
//If element found return index return index; } temp = temp->next; index++; } //If element not found return -1; }//Driver Mainint main(){
Node * head = createNewLL(); int data; cout<<"Enter the data of the linked list to be found."<
>data; int index = findNodeInLL(head,data); cout<<"It is present at "<
<< endl; return 0;}

Output

输出量

Enter the data of the Node5Do you want to continue?(0/1)1Enter the data of the Node6Do you want to continue?(0/1)1Enter the data of the Node7Do you want to continue?(0/1)1Enter the data of the Node8Do you want to continue?(0/1)1Enter the data of the Node9Do you want to continue?(0/1)0Enter the data of the linked list to be found.8It is present at 3

翻译自:

微信小程序 查找兄弟节点

转载地址:http://erxzd.baihongyu.com/

你可能感兴趣的文章
Spring - DI
查看>>
微软自己的官网介绍 SSL 参数相关
查看>>
Composite UI Application Block (CAB) 概念和术语
查看>>
64位MATLAB和C混合编程以及联合调试
查看>>
原生js大总结二
查看>>
PHP基础
查看>>
UVa 11488 超级前缀集合(Trie的应用)
查看>>
Django 翻译与 LANGUAGE_CODE
查看>>
[转]iOS教程:SQLite的创建数据库,表,插入查看数据
查看>>
【转载】OmniGraffle (一)从工具栏开始
查看>>
初识ionic
查看>>
java 中打印调用栈
查看>>
开发 笔记
查看>>
ajax跨域,携带cookie
查看>>
阶段3 2.Spring_01.Spring框架简介_03.spring概述
查看>>
阶段3 2.Spring_02.程序间耦合_1 编写jdbc的工程代码用于分析程序的耦合
查看>>
阶段3 2.Spring_01.Spring框架简介_04.spring发展历程
查看>>
阶段3 2.Spring_02.程序间耦合_3 程序的耦合和解耦的思路分析1
查看>>
阶段3 2.Spring_02.程序间耦合_5 编写工厂类和配置文件
查看>>
阶段3 2.Spring_01.Spring框架简介_05.spring的优势
查看>>