日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

          歡迎訪問 生活随笔!

          生活随笔

          當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

          编程问答

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

          發布時間:2023/12/1 编程问答 31 豆豆
          生活随笔 收集整理的這篇文章主要介紹了 微信小程序 查找兄弟节点_使用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 -15Sample Output 1:2Sample Input 2:3 4 5 2 6 1 9 -16Sample 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->NULLIn 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 Indexint index = 0; Node * temp = head;//LinkedList traversal for finding the nodewhile(temp!=NULL){if(temp->data == data){ //If element found return indexreturn index; }temp = temp->next;index++;} //If element not foundreturn -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 Nodeint data;Node * next; };Node *newNode(int k){ //defining new nodeNode *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 Indexint index = 0; Node * temp = head;//LinkedList traversal for finding the nodewhile(temp!=NULL){if(temp->data == data){ //If element found return indexreturn index; }temp = temp->next;index++;} //If element not foundreturn -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 ++程序在链接列表中查找节点的全部內容,希望文章能夠幫你解決所遇到的問題。

          如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。