微信小程序 查找兄弟节点

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 node
int 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;
}
.minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } } .minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } }

C++ code

C ++代码

#include <bits/stdc++.h>
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 list
Node *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 head
Node *createNewLL(){int cont = 1;
int data;
Node* head = NULL;
while(cont){cout<<"Enter the data of the Node"<<endl;
cin>>data;
head = addNode(head,data);
cout<<"Do you want to continue?(0/1)"<<endl;
cin>>cont;
}
return head;
}
//Function for finding the node
int 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 Main
int main(){Node * head = createNewLL();
int data;
cout<<"Enter the data of the linked list to be found."<<endl;
cin>>data;
int index = findNodeInLL(head,data);
cout<<"It is present at "<<index<< endl;
return 0;
}

Output

输出量

Enter the data of the Node
5
Do you want to continue?(0/1)
1
Enter the data of the Node
6
Do you want to continue?(0/1)
1
Enter the data of the Node
7
Do you want to continue?(0/1)
1
Enter the data of the Node
8
Do you want to continue?(0/1)
1
Enter the data of the Node
9
Do you want to continue?(0/1)
0
Enter the data of the linked list to be found.
8
It is present at 3

翻译自: https://www.includehelp.com/cpp-programs/find-a-node-in-linked-list.aspx

微信小程序 查找兄弟节点

微信小程序 查找兄弟节点_使用C ++程序在链接列表中查找节点相关推荐

  1. java 字符串 查找 多个_初学者求教,如何在字符串中查找多个子字符串的位置...

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 鲁镇的网吧的格局,是和别处不同的:都是门口一个曲尺形的大柜台,柜里面预备着热水,可以随时泡奶茶.上学的人,中午傍晚放了学,每每花三元钱,上两个小时,--这 ...

  2. linux 查找文件夹命令_如何从命令行在Linux中查找文件,文件夹和目录?

    linux 查找文件夹命令 Linux provides different ways to find and locate files and folders. We can use GUI too ...

  3. notepad++节点_在C ++中删除链接列表的中间节点

    notepad++节点 Given a single Linked List and we have to delete the middle the element of the Linked Li ...

  4. Python 列表查找,如何在列表中查找项目或者元素索引【翻译】

    Python 列表查找,如何在列表中查找项目或者元素索引 在本文中,你将学习如何在Python中查找列表中包含元素的索引. 有几种方法可以实现这一点,在本文中,你将学习三种不同的方式用于查找列表元素的 ...

  5. python字符串\列表中查找出某个值且对应的下标

    1.字符串中查找值和对应的下标 a = "123yui78y8y67tuy" print re.findall("y",a) #查找出在a包含的字符串中所有的y ...

  6. 数组重复次数最多的元素递归_使用递归计算链接列表中元素的出现次数

    数组重复次数最多的元素递归 Solution: 解: Required function: 所需功能: func_occurence ( node *temp) //recursive functio ...

  7. 文件列表中查找同一批次号中批次号最大的文件名称

    文件列表中查找同一批次号中批次号最大的文件名称 There is no nutrition in the blog content. After reading it, you will not on ...

  8. 文件列表中查找同一批次号中批次号最小的文件名称

    文件列表中查找同一批次号中批次号最小的文件名称 There is no nutrition in the blog content. After reading it, you will not on ...

  9. Python:在列表中查找

    本文翻译自:Python: Find in list I have come across this: 我遇到了这个: item = someSortOfSelection() if item in ...

最新文章

  1. 各种编程语言的深度学习库整理
  2. 元宇宙和游戏赚钱的兴趣正与日俱增
  3. php设置东京时区,php设置时区方法介绍
  4. mac下卸载jdk1.7
  5. 一图看懂边缘计算整体架构
  6. 坐标轨迹计算_机器人的轨迹规划与自动导引
  7. nodejs下载与安装
  8. 影刀RPA实操指南丨90%用户都会陷入的excel自动化误区
  9. 信任危机在蔓延,会不会产生雪崩效应
  10. 03 HTML_网页中的表格
  11. 学计算机进中央电视台,厉害了,我们的计算机!
  12. autoit3模拟鼠标点击之MouseClick
  13. 虚拟机或公有云搭建wordpress博客
  14. windbg分页机制实验
  15. 在Firefox中以电影院风格观看YouTube视频
  16. Git 学会git,探索GitHub,掌握新知识 (二)
  17. platform-tools
  18. 开发简单Android聊天软件(7)
  19. BootLoader有什么作用?
  20. 计算机虚拟化技术背景,互联网+背景下的虚拟现实技术

热门文章

  1. java调用wvsc.exe_c语言 函数的调用方法
  2. HTML邮件制作规范
  3. BEM思想之彻底弄清BEM语法
  4. java读取文件方法
  5. BZOJ2809 dispatching 【可并堆】
  6. 启动tomcat时 一闪而过解决方法
  7. 拖拽功能-jquery
  8. scala akka通信机制
  9. 恋爱Linux(Fedora20)2——安装Java运行环境(JDK)
  10. LPWSTR 类型的实参与const.char *类型形参不兼容