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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

n个节点的二叉树n+1_使用C ++程序将链接列表中的最后N个节点附加到第一个

發布時間:2023/12/1 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 n个节点的二叉树n+1_使用C ++程序将链接列表中的最后N个节点附加到第一个 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

n個節點的二叉樹n+1

Given a linked list and an integer n, append the last n elements of the LL to front. Assume given n will be smaller than length of LL.

給定一個鏈表和一個整數n,將LL的最后n個元素附加到前面。 假設給定的n將小于LL的長度。

Input format: Line 1: Linked list elements (separated by space and terminated by -1

輸入格式:第1行:鏈接的列表元素(以空格分隔并以-1終止

Sample Input 1 :1 2 3 4 5 -13Sample Output 1 :3 4 5 1 2

Description:

描述:

The question asks us to append the last N nodes to front, i.e the new linked list should first start from those N nodes and then traverse the rest of the nodes through the head of the old linked list.

這個問題要求我們將最后的N個節點附加到前面,即新的鏈表應首先從這N個節點開始,然后再通過舊鏈表的頭遍歷其余節點。

Example:

例:

For Linked List 1->2->3->4->5->6->NULLTo append the last 2 nodes, the new linked list should be:5->6->1->2->3->4->NULL

Solution Explanation:

解決方案說明:

To solve this problem, we take two pointers temp and t and point both of them to the head of the linked list. We take another variable i and equate it to – n. This i is used for finding out the head of the new linked list. Then we traverse the loop while temp != NULL. In the loop we check that if(i>=0) i.e temp is now n nodes away from t, t = t-> next. We will update i++ and temp = temp->next on each traversal. At last, we update temp-> next = head, head = t -> next and t-> next = NULL.

為了解決這個問題,我們使用兩個指針temp和t并將它們都指向鏈接列表的開頭。 我們采用另一個變量i并將其等于– n 。 我用于查找新鏈表的標題。 然后,我們在temp!= NULL時遍歷循環。 在循環中,我們檢查if(i> = 0),即temp現在距離t距離n個節點, t = t-> next 。 我們將在每次遍歷時更新i ++和temp = temp-> next 。 最后,我們更新temp-> next = head , head = t-> next和t-> next = NULL 。

Algorithm:

算法:

  • STEP 1: Declare the function appendNNode with parameters (Node* head, int n)

    步驟1:使用參數聲明函數appendNNode (Node * head,int n)

  • STEP 2: Declare two variables Node * temp , t and point both of them to head.

    步驟2:聲明兩個變量Node * temp , t并將它們都指向head。

  • STEP 3: Declare int i = -n

    步驟3:聲明int i = -n

  • STEP 4: Repeat Step 5 and 6, while(temp->next != NULL)

    步驟4:重復步驟5和6, 同時(temp-> next!= NULL)

  • STEP 5: if(i>=0) t = t-> next.

    步驟5: if(i> = 0)t = t-> next 。

  • STEP 6: temp = temp-> next, i++.

    步驟6: temp = temp->接下來,i ++ 。

  • STEP 7: temp->next = head, head = t->next, and t-> next =NULL

    步驟7: temp-> next = head , head = t-> next和t-> next = NULL

  • STEP 8: return head

    步驟8:返回頭

Steps:

腳步:

At first: 1->2->3->4->5->6->NULL, t->1 and temp->1.After complete traversal: 1->2->3->4->5->6->NULL, t->4 and temp->6.So, temp->next = head and head = t->nexti.e 5->6->1->2->3->4 --- (reconnecting to 5)Atlast, t-> next = NULLi.e 5->6->1->2->3->4->NULL .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}

Function:

功能:

Node *appendNNodes(Node* head, int n){// Two pointers, one for traversal and // other for finding the new head of LLNode *temp = head, *t = head; //index maintained for finding new headint i = -n;while(temp->next!=NULL){//When temp went forward n nodes from tif(i>=0){ t = t->next;}temp = temp ->next;i++;}//Connecting the tail to headtemp->next = head; //Assigning the new nodehead = t->next; //Deleting the previous connectiont->next = NULL; return head; }

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; }//To print the Linked List void *printLL(Node * head){while(head!= NULL){cout<<head->data<<"->";head = head-> next;}cout<<"NULL"<<endl; }//Function Node *appendNNodes(Node* head, int n){// Two pointers, one for traversal and // other for finding the new head of LLNode *temp = head, *t = head; //index maintained for finding new headint i = -n; while(temp->next!=NULL){//When temp went forward n nodes from tif(i>=0){ t = t->next;}temp = temp ->next;i++;}//Connecting the tail to headtemp->next = head; //Assigning the new nodehead = t->next; //Deleting the previous connectiont->next = NULL; return head; }//Driver Main int main(){Node * head = createNewLL();cout<<"The linked list is"<<endl;printLL(head);int data;cout<<"Enter the number of nodes you want to append."<<endl;cin>>data;head = appendNNodes(head,data);cout<<"The new Linked List is" <<endl;printLL(head);return 0; } .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}

Output

輸出量

Enter the data of the Node 1 Do you want to continue?(0/1) 1 Enter the data of the Node 2 Do you want to continue?(0/1) 1 Enter the data of the Node 3 Do you want to continue?(0/1) 1 Enter the data of the Node 4 Do you want to continue?(0/1) 1 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) 0 The linked list is 1->2->3->4->5->6->7->NULL Enter the number of nodes you want to append. 3 The new Linked List is 5->6->7->1->2->3->4->NULL

翻譯自: https://www.includehelp.com/cpp-programs/append-last-n-nodes-to-first-in-the-linked-list.aspx

n個節點的二叉樹n+1

總結

以上是生活随笔為你收集整理的n个节点的二叉树n+1_使用C ++程序将链接列表中的最后N个节点附加到第一个的全部內容,希望文章能夠幫你解決所遇到的問題。

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