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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

n个节点的二叉树n+1_使用C ++程序删除链接列表的M个节点后的N个节点

發(fā)布時(shí)間:2023/12/1 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 n个节点的二叉树n+1_使用C ++程序删除链接列表的M个节点后的N个节点 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

n個(gè)節(jié)點(diǎn)的二叉樹n+1

Problem statement:

問題陳述:

Given a Linked List, we have to delete N numbers of nodes after the M numbers of nodes.

給定一個(gè)鏈表,我們必須在M個(gè)節(jié)點(diǎn)之后刪除N個(gè)節(jié)點(diǎn)。

Example:

例:

Input: 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → 9 → 10N=2, M=3Output: 1 → 2 → 3 → 6 → 7 → 8Input: 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → 9 → 10 → 11N=3, M=3Output: 1 → 2 → 3 → 7 → 8 → 9

Algorithm:

算法:

To solve that problem we simply use the Brute-Force method. We traverse the linked list from the head node and delete N nodes after traversing M nodes. If there are less than N nodes to delete then we simply delete that nodes and stop traversing.

為了解決該問題,我們僅使用Brute-Force方法 。 我們從頭節(jié)點(diǎn)遍歷鏈表,并在遍歷M個(gè)節(jié)點(diǎn)后刪除N個(gè)節(jié)點(diǎn) 。 如果要?jiǎng)h除的節(jié)點(diǎn)少于N個(gè),則我們只需刪除該節(jié)點(diǎn)并停止遍歷即可。

C++ implementation:

C ++實(shí)現(xiàn):

#include <bits/stdc++.h> using namespace std;struct node {int data;node* next; };//Create a new node struct node* create_node(int x) {struct node* temp = new node;temp->data = x;temp->next = NULL;return temp; }//Enter the node into the linked list void push(node** head, int x) {struct node* store = create_node(x);if (*head == NULL) {*head = store;return;}struct node* temp = *head;while (temp->next) {temp = temp->next;}temp->next = store; }//Reverse the linked list void delete_node(node* head, int m, int n) {struct node* temp = head;int count = 1;while (1) {while (temp && count < m) {temp = temp->next;count++;}if (temp == NULL || temp->next == NULL) {return;}count = 1;struct node* store = temp;temp = temp->next;while (temp && count < n) {temp = temp->next;count++;}if (temp == NULL) {return;}store->next = temp->next;temp = temp->next;} }//Print the list void print(node* head) {struct node* temp = head;while (temp) {cout << temp->data << " ";temp = temp->next;} }int main() {struct node* l = NULL;push(&l, 1);push(&l, 2);push(&l, 3);push(&l, 4);push(&l, 5);push(&l, 6);cout << "Before the delete operation" << endl;print(l);delete_node(l, 3, 2);cout << "\nAfter the delete operation" << endl;print(l);return 0; }

Output

輸出量

Before the delete operation 1 2 3 4 5 6 After the delete operation 1 2 3 6

翻譯自: https://www.includehelp.com/cpp-programs/delete-n-nodes-after-m-nodes-of-a-linked-list-using-cpp-program.aspx

n個(gè)節(jié)點(diǎn)的二叉樹n+1

總結(jié)

以上是生活随笔為你收集整理的n个节点的二叉树n+1_使用C ++程序删除链接列表的M个节点后的N个节点的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。