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

微信小程序 查找兄弟节点

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;                  
}

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

微信小程序 查找兄弟节点

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/378728.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

形态学操作——开闭运算、顶帽底(黑)帽变换

膨胀和腐蚀运算的问题&#xff1a; 边缘形状发生了变化&#xff0c;膨胀发生了扩张&#xff0c;腐蚀发生了收缩 目标物体变形&#xff0c;对识别时的特征提取会造成影响 解决方法&#xff1a; 开操作: B对A的开操作就是先B对A腐蚀&#xff0c;紧接着用B对结果进行膨胀 先腐…

java 基础实战_Java基础实战(三)

是否是否是否是否获取字符串字符数组大写?小写?数字?非字母与数字大写字母小写字母数字i结束ii1第一步 拆分字符串为字符数组&#xff1a;static void count(String str) {// 将字符串拆分为字符数组char[] charArray str.toCharArray();}第二步 定义相关变量记录结果&…

11-图像梯度-Sobel算子

图像梯度是指图像某像素在x和y两个方向上的变化率&#xff08;与相邻像素比较&#xff09;&#xff0c;是一个二维向量&#xff0c;由2个分量组成&#xff0c;X轴的变化、Y轴的变化 。 其中X轴的变化是指当前像素右侧&#xff08;X加1&#xff09;的像素值减去当前像素左侧&…

给IE有效指定编码

<title>下一站</title> <meta http-equiv"Content-Type" content"text/html; charsetutf-8" /> IE每次打开&#xff0c;均是一片空白&#xff0c;查看右键&#xff0d;编码&#xff0c;显示是GB2312。要手功改为UTF-8后才能正常显示页面…

形态学操作——击中击不中变换

操作目的 HitMiss变换是形态检测的一个工具&#xff0c;通过定义形状模板可以在图像中获取同一形状物体的位置坐标。 算法讲解 1、用击中结构去腐蚀原始图像得到击中结果X&#xff08;这个过程可以理解为在原始图像中寻找和击中结构完全匹配的模块&#xff0c;匹配上了之后&…

stack.pop()方法_C.示例中的Stack.Pop()方法

stack.pop()方法C&#xff03;Stack.Pop()方法 (C# Stack.Pop() method) Stack.Pop() method is used to remove an object from the top of the stack. The method removes and returns the object from the top. Stack.Pop()方法用于从堆栈顶部删除对象。 该方法从顶部删除并…

java list的作用_集合框架(List集合的特有功能概述和测试)

package cn.itcast_03;import java.util.ArrayList;import java.util.List;/** List集合的特有功能&#xff1a;* A:添加功能* void add(int index,Object element):在指定位置添加元素* B:获取功能* Object get(int index):获取指定位置的元素* C:列表迭代器* ListIterator li…

12-图像梯度-Scharr算子和laplacian算子

Scharr算子 cv2.Scharr(img,cv2.CV_64F,1,0) 第一个参数&#xff1a;当前的图像对象名称 第二个参数&#xff1a;当前图像的深度&#xff0c;通常情况下指定为-1&#xff0c;表示输出和输入的深度是一样的&#xff1b;cv2.CV_64F可以存6字节的大小&#xff0c;为了方便后面的取…

新的一年新希望,百忙中继续学习

公司来了一批新同事&#xff0c;我又忙于购买设备&#xff0c;布置办公桌了。 小林建议我找一份更合适的工作&#xff0c;目前的我其实是在混日子&#xff0c;因为我并不擅长沟通与销售。 暂时还是保持这样吧&#xff0c;我还需要一定时间来积蓄力量。 转载于:https://www.cnbl…

Oracle Internal Event:10200 Consistent Read诊断事件

10200(consistent read buffer status)内部诊断事件可以用于探测一致性读CR(consistent read)块的访问情况&#xff0c;虽然cr读的统计信息可以从v$sysstat或AWR/statspack中获取&#xff0c;但是10200 event还是我们研究Consistent Read一致性读的有力工具。该事件可以通过在会…

多线程循环输出abcc++_C ++循环| 查找输出程序| 套装4

多线程循环输出abccProgram 1: 程序1&#xff1a; #include <iostream>using namespace std;int A 5;int fun(){return A--;}int main(){int A 5;while (fun()) {cout << A ::A << " ";}return 0;}Output: 输出&#xff1a; 9 8 7 6 5Explana…

Opencv——图像金字塔与图像尺寸缩放

主要讲解 1、resize()函数调用 函数定义&#xff1a; 调用方式&#xff1a; resize(srcImage, dstImage, Size(64, 128)); //对图片进行修改 resize(srcImage, dstImage, Size(), 0.5, 0.5);第6个参数的含义&#xff1a; INTER_NEAREST:最邻近插值 (放大好用) INTER_ARE…

java nature_Java中BufferedReader和scanner的对比 - nature

原地址&#xff1a;http://blog.sina.com.cn/s/blog_5fd837410100rtwk.html Scanner 和BufferedReader同样能实现将键盘输入的数据送入程序&#xff0c; import java.io.*; import java.util.Scanner; public class C { public static void main(String []args) throws IOExcep…

13-Canny边缘检测

Canny边缘检测主要思路步骤如下&#xff1a; 1&#xff0c;使用高斯滤波器&#xff0c;以平滑图像&#xff0c;滤除噪声 2&#xff0c;计算图像中每个像素点的梯度强度和方向 3&#xff0c;应用非极大值抑制&#xff0c;以消除边缘检测带来的杂散响应 4&#xff0c;应用双阈值检…

c# uri.host_C#| Uri.IsHexEncoding()方法与示例

c# uri.hostUri.IsHexEncoding()方法 (Uri.IsHexEncoding() Method) Uri.IsHexEncoding() method is a static method or Uri class. Which is used to return that given string is hex-encoded or not? If the given string is hex coded then it returns true otherwise it…

一位老鸟对 23 种设计模式的有趣见解(转)

在网络上流畅很广的一篇旧文&#xff0c;暂时没找到原作者&#xff0c;目前所看到的最早转载时间是 2005 年 2 月 28 日。作者用轻松的语言&#xff0c;形象解释了 23 种模式&#xff0c;有很好的启发作用。创建型模式 1、FACTORY—追MM少不了请吃饭了&#xff0c;麦当劳的鸡翅…

微机原理——移位指令

例题 思路 选择移位语句&#xff0c;右移&#xff0c;将AL移出的送入DX左端&#xff0c;将BL移出的送入DX左端。循环八次 MOV AL,01100101B; MOV BL,11011010B; XOR DX,DX;两个值相同&#xff0c;异或结果为0。等效&#xff1a;MOV DX,0 MOV CX,8;count L1: SHR AL,1;逻辑右…

14-图像金字塔

由第一个图可知&#xff0c;图像金字塔这无非就是对图像进行放大和缩小罢了 1&#xff0c;高斯金字塔 向下采样方法(缩小)&#xff0c;越采样越小&#xff0c;即从金字塔底部向上采样 cv2.pyrDown(img) 向上采样方法(放大)&#xff0c;越采样越大&#xff0c;即从金字塔顶…

JAVA和javascrito_JAVA 和JavaScript的split方法异同

Split的方法很常用&#xff0c;除了str.split("regex")&#xff0c;其实还可以多传一个参数&#xff1a;str.split("regex", limit)。但是要注意&#xff0c;JavaScript和java的split中limit参数作用是不同的。简单说&#xff0c;JavaScript中&#xff0c;…

如果__name__ =='__main__':在Python中怎么办?

In order to understand the details of __name__ variable and the if condition, let us go through a simple exercise. Run a simple python file with just the following lines and run the file as python3 code, 为了了解__name__变量和if条件的详细信息&#xff0c;让…