微信小程序 查找兄弟節點_使用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.pswp.cn/news/378728.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/378728.shtml
英文地址,請注明出處:http://en.pswp.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;讓…