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

n个节点的二叉树n+1

Problem statement:

问题陈述:

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

给定一个链表,我们必须在M个节点之后删除N个节点。

Example:

例:

    Input: 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → 9 → 10
N=2, M=3
Output: 1 → 2 → 3 → 6 → 7 → 8
Input: 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → 9 → 10 → 11
N=3, M=3
Output: 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方法 。 我们从头节点遍历链表,并在遍历M个节点后删除N个节点 。 如果要删除的节点少于N个,则我们只需删除该节点并停止遍历即可。

C++ implementation:

C ++实现:

#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个节点的二叉树n+1

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

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

相关文章

EAX、EBX、ECX、EDX

前面学习了8086的基本知识&#xff0c;今天正式开始学习win32平台的汇编 EAX&#xff1a;累加寄存器 32位&#xff0c;功能和8086中ax相同 mov eax,dword ptr [ebp-4] add eax,1 mov dword ptr [ebp-4],eaxEBX&#xff1a;基址寄存器 32位&#xff0c;功能和8086中bx相同 mov…

aspnet_Membership_GetUserByEmail////aspnet_Membership_GetUserByName

ALTERPROCEDUREdbo.aspnet_Membership_GetUserByEmail --通过邮箱获取用户名ApplicationNameNVARCHAR(256), EmailNVARCHAR(256)ASBEGINIF( EmailISNULL) SELECTu.UserName FROMdbo.aspnet_Applications a, dbo.aspnet_Users u, dbo.aspnet_Membership m …

php delete和truncate,TRUNCATE 删除表,无法回退。默认选择为整个表的内容,所以不能加条件。...

TRUNCATE 删除表&#xff0c;无法回退。默认选择为整个表的内容&#xff0c;所以不能加条件。DELETE 删除表&#xff0c;可以回退。可以带where 条件。建议使用delete。但是TRUNCATE 删除表数据比delete要快。使用TRUNCATE TABLE语句TRUNCATE TABLE语句提供了一种删除表中所有记…

【转】在Excel中如何输入平方米符号,输入立方米符号?

按住Alt键&#xff0c;在小键盘上输入178&#xff0c;然后放开Alt健&#xff0c;可以得到平方符号。 按住Alt键&#xff0c;在小键盘上输入179&#xff0c;然后放开Alt健&#xff0c;可以得到立方符号。转载于:https://www.cnblogs.com/temptation/archive/2011/04/25/2028587.…

dates.format_在SQL中使用DATES及其不同的内置函数NOW(),FORMAT()

dates.formatBasically, when we are working with dates, we need to be sure that the format of the date, we are trying to insert in the database is in format, and matches the format of the date column in the database. 基本上&#xff0c;当我们使用日期时&#…

砂.随笔.三

刚在整理电脑中的照片,把朋友们的照片都整进了"众生相",当点到他的照片时,迟疑了,没拉进去当剪切粘贴时,不知出了什么故障,"铛"地一声,"众生相"成了一空文件夹我不是一个容易觉得可惜的人,只是居然不禁窃喜适才没将他的照片放进去敲了敲脑袋,按住…

标志寄存器EFLAGES

EFLAGES是32位&#xff0c;对我们比较有用的是低16位 OF&#xff1a;溢出标志。溢出为1&#xff0c;否则为1DF&#xff1a;方向标志。串处理指令的方向IF&#xff1a;中断标志AF&#xff1a;辅助进位标志。进位时为1&#xff0c;否则为0ZF&#xff1a;零标志。运算结果为0时为1…

去除对象中的类型集合

一般来说&#xff0c;当某一个对象有多个集合类型的子元素时&#xff0c;我们都会给每个子元素创建一个集合对象来承载子元素&#xff0c;类似于&#xff1a; publicclassProcess {publicvar isExecutable:Boolean;publicvar refLaneSet:LaneSet;publicvar startEventList:Arra…

服务器安装centos6 linux,CentOS6.5安装详细教程,手把手教你安装Linux操作系统(CentOS6.5)...

CentOS6.5安装详细教程1、准备好CentOS-6.5.iso文件&#xff0c;并刻录到光盘中&#xff0c;放入光驱&#xff0c;重启服务器&#xff0c;修改BIOS为光驱启动&#xff0c;之后开始进入安装&#xff1a;2、按任意键&#xff0c;进入引导菜单。按上下键&#xff0c;移动光标&…

Vestigium-Google CodeJam 2020资格回合问题1解决方案

Problem statement: 问题陈述&#xff1a; Vestigium means "trace" in Latin. In this problem we work with Latin squares and matrix traces. Vestigium在拉丁语中表示“痕迹”。 在此问题中&#xff0c;我们使用拉丁方和矩阵迹线。 The trace of a square mat…

装修(43天,安装新家具啦)

装修(43天,安装新家具啦) 昨天保洁公司的来做了保洁.说好是9点钟让我去开门的,可他们却10点才到,原来听错地点了.三个人,从早上10开始做,到下午六点我去的时候,还没有做完,大概要做到七点.才150元钱,也真难为他们,还有一部分是公司的.效果觉得还是一般,有些地方还是不干净.可能…

dbms和sql_DBMS | 并发控制和各种并发控制方法

dbms和sql并发控制 (Concurrency Control) The concurrency control is the coordination of the simultaneous execution of a transaction in a multiuser database system. The concurrency control can ensure the serializability of the transaction in a multiuser data…

修改窗口图标 AfxRegisterWndClass()

LPCTSTR AFXAPI AfxRegisterWndClass(UINT nClassStyle,HCURSOR hCursor 0,HBRUSH hbrBackground 0,HICON hIcon 0 ); 可以简单修改一个窗口的注册类&#xff0c;当然也可以用SetWindowsLong()来实现。转载于:https://www.cnblogs.com/magic-cube/archive/2011/05/05/2038…

linux停止ssh服务的命令,开启、关闭、查看SSH服务

一、临时启用SSH服务1、通过SSH服务器的启动脚本文件启动SSH服务通过OpenSSH服务器的脚本文件“/etc/rc.d/init.d/sshd”启动SSH服务&#xff0c;命令执行如下。/etc/rc.d/init.d/sshd start命令执行后&#xff0c; SSH服务开始运行。2、使用Linux下的service命令启动SSH服务使…

XCHE命令

功能&#xff1a; 交换数据&#xff0c;交换两个操作数内容 形式&#xff1a; xche reg,reg xche reg,mem xche mem,regreg&#xff1a;寄存器&#xff1b;mem&#xff1a;内存单元 比如&#xff1a; xche eax&#xff0c;ebx;交换eax和ebx的内容 xche eax,[ebx]

软件测试经典网站(转)

软件测试经典网站网址简介http://bdonline.sqe.com/一个关于网站测试方面的网页,对这方面感兴趣的人可以参考http://citeseer.nj.nec.com/一个丰富的电子书库,内容很多,而且提供著作的相关文档参考和下载,是作者非常推荐的一个资料参考网站http://groups.yahoo.com/group/LoadR…

ai人工智能在手机的应用_强化学习在人工智能中的应用

ai人工智能在手机的应用The reinforcement learning is being used in many Intelligent Systems and the developers are seeing a great scope in it for current and future developments in the field of computers and robotics. This is because, the Reinforcement Lear…

LEA指令

功能&#xff1a;取偏移地址 格式&#xff1a; LEA reg&#xff0c;memreg&#xff1a;寄存器&#xff1b;mem&#xff1a;内存单元 例如&#xff1a; LEA AX,[1000H]将1000源操作数[1000H]的偏移地址1000H送到AX中。 理解的时候可将[]去掉&#xff0c;等同于&#xff1a; m…

参考站点

26个杰出的jQuery幻灯片插件http://woshao.com/article/6807a76a43d611e081e1000c2959fd2a/周公的专栏http://blog.csdn.net/zhoufoxcn/ W3SCHOOL在线教程http://www.w3school.com.cn/jQuery API 中文版http://www.css88.com/jqueryapi在ASP.NET中使用Highcharts js图表http://…

linux多进程通过中断实现,Linux驱动中断上下文中会发生什么结果实验测试

一、前言每一个Linux驱动工程师都知道这样一个准则&#xff1a;在中断上下文中不能睡眠。但是为什么interrupt context中不能调用导致睡眠的kernel API呢&#xff1f;如果驱动这么做会导致什么样的后果呢&#xff1f;这就是本文探讨的主题。为了理解这个主题&#xff0c;我们设…