notepad++节点_在C ++中删除链接列表的中间节点

notepad++节点

Given a single Linked List and we have to delete the middle the element of the Linked List.

给定一个链表,我们必须删除链表中间的元素。

If the length of the linked list is odd then delete (( n+1)/2)th term of the linked list and if the list is of even length then delete the (n/2+1)th term of the liked list.

如果链接列表的长度为奇数,则删除链接列表的第((n + 1)/ 2)项;如果列表的长度为偶数,则删除喜欢列表的第(n / 2 + 1)项。

Example 1:

范例1:

    If we have a Linked List : 1 → 2 → 3 → 4 → 5 → 6 → 7
After deleting the middle node the linked list will be: 
1 → 2 → 3 → 5 → 6 → 7 
4 is the middle node

Example 2:

范例2:

    If we have a Linked List : 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8
After deleting the middle node the linked list will be: 
1 → 2 → 3 → 4 → 6 → 7 → 8
5 is the middle node

Algorithm:

算法:

To solve the problem we follow the following procedure,

为了解决该问题,我们遵循以下过程,

  1. We initiate the two node pointer name as slow, fast. (like Floyd's tortoise algo, refer the link to my article of merge sort in the linked list).

    我们将两个节点的指针名称初始化为慢,快。 (就像弗洛伊德(Floyd)的乌龟算法一样,请在链接列表中引用我的合并排序文章的链接)。

  2. Each time we increment the slow by one whereas increment the fast pointer by two.

    每次我们将慢速指针加一,而快速指针加二。

  3. Repeat step 2 until the fast pointer goes to the end of the linked list.

    重复步骤2,直到快速指针移到链接列表的末尾。

  4. When fast pointer goes to the end of the list at the same time slow pointer points to the middle of the linked list.

    当快速指针同时到达列表末尾时,慢速指针指向链接列表的中间。

  5. Then delete the middle node.

    然后删除中间节点。

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;
}
//Delete the middle node from the linked list
void delete_node(node** head){
if((*head)->next==NULL){
*head=NULL;
return;
}
struct node* fast=(*head)->next;
struct node* slow=*head;
while(fast && fast->next && fast->next->next){
slow=slow->next;
fast=fast->next->next;
}
slow->next=slow->next->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);
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 5 6

翻译自: https://www.includehelp.com/cpp-programs/delete-the-middle-node-of-a-linked-list-in-cpp.aspx

notepad++节点

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

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

相关文章

SET ANSI_NULLS ON

指定在与 Null 值一起使用等于 () 和不等于 (<>) 比较运算符时采用符合 ISO 标准的行为。 当 SET ANSI_NULLS 为 ON 时&#xff0c;即使 column_name 中包含空值&#xff0c;使用 WHERE column_name NULL 的 SELECT 语句仍返回零行。即使 column_name 中包含非空值&…

Eclipse项目左上角出现大红色感叹号怎么办?

出现大红色感叹号是因为环境不匹配 解决方法&#xff1a; 右击出现大红色感叹号的项目 点击 Libraries&#xff0c;将有叉号的给Remove掉 然后再点击 Add Library —> JRE System Library —> Next 勾选第二个即可 之后&#xff0c;就不会出现大红色感叹号了。

PCB---STM32最小系统制作过程

PCB 制作过程STM32核心模块连接外部电源晶振OSC_IN(8MHz)OSC32_IN(32.768MHz&#xff09;复位下载口BOOT模式电源模块添加功能UARTWKUPSTM32核心模块 这里我们以STM32F103C8T6为列&#xff0c;先将芯片的原理图放到原理图中 对于STM32&#xff0c;有几个模块是核心&#xff0…

scala 随机生成整数_如何在Scala中以整数形式获取当前年份?

scala 随机生成整数In Scala programming language, there is an option for the programmer to use libraries of java because of its interoperability with java. 在Scala编程语言中&#xff0c;程序员可以选择使用Java库&#xff0c;因为它可以与Java互操作。 There are …

转载:glut.h 与 stdlib.h中 的exit()重定义问题的解决

遇到的问题&#xff0c;来自&#xff1a;http://blog.sina.com.cn/s/blog_629c53bd0100f5li.html 出现&#xff1a; c:\codeprogram\microsoft visual studio 10.0\vc\include\stdlib.h(353): error C2381: “exit”: 重定义&#xff1b;__declspec(noreturn) 不同1> c:\pro…

括号配对问题(C++栈)

题目描述: 现在&#xff0c;有一行括号序列&#xff0c;请你检查这行括号是否配对。 输入描述: 第一行输入一个数N&#xff08;0<N<100&#xff09;,表示有N组测试数据。后面的N行输入多组输入数据&#xff0c;每组输入数据都是一个字符串S(S的长度小于10000&#xff0c;…

FreeRTOS---堆内存管理(一)

FreeRTOS的堆内存管理简介动态内存分配及其与 FreeRTOS 的相关性动态内存分配选项内存分配方案Heap_1heap_2Heap_3Heap_4设置heap_4的起始地址Heap_5vPortDefineHeapRegions()堆相关的函数xPortGetFreeHeapSizexPortGetMinimumEverFreeHeapSizeMalloc调用失败的Hook函数这篇文章…

python中生成随机整数_在Python中生成0到9之间的随机整数

python中生成随机整数Following are the few explanatory illustrations using different python modules, on how to generate random integers? Consider the scenario of generating the random numbers between 0 and 9 (both inclusive). 以下是使用不同的python模块的一…

愚人节恶搞网站谨防遭黑客攻击

金山毒霸云安全中心日前发出预警&#xff0c;在近期拦截的大量“挂马”、钓鱼等恶意网页中&#xff0c;与“愚人节”相关的&#xff0c;在近一周数量急剧增加。 愚人节将至&#xff0c;怎么整人好玩?近期许多恶搞网站、相关的网络论坛的流量不断攀升。金山毒霸云安全中心日前发…

JavaScript中的String()函数与示例

String()函数 (String() function) String() function is a predefined global function in JavaScript, it is used to convert an object to the string. String()函数是JavaScript中预定义的全局函数&#xff0c;用于将对象转换为字符串。 Example: 例&#xff1a; In thi…

ASCII码排序(C++)

题目描述: 输入三个字符&#xff08;可以重复&#xff09;后&#xff0c;按各字符的ASCII码从小到大的顺序输出这三个字符。 输入描述: 第一行输入一个数N,表示有N组测试数据。后面的N行输入多组数据&#xff0c;每组输入数据都是占一行&#xff0c;有三个字符组成&#xff0c;…

FreeRTOS--堆内存管理(二)

堆内存管理代码具体实现heap_1内存申请函数内存释放函数heap_2内存块内存堆初始化函数内存块插入函数内存申请函数判断是不是第一次申请内存开始分配内存内存释放函数heap_3heap_4内存堆初始化函数内存块插入函数heap_5上一篇文章说了FreeRTOS实现堆内存的原理&#xff0c;这一…

在查询的结果中添加自增列 两种方法

解决办法《一》&#xff1a; 在SQL Server数据库中表信息会用到Identity关键字来设置自增列。但是当有数据被删除的话&#xff0c;自增列就不连续了。如果想查询出这个表的信息&#xff0c;并添 加一列连续自增的ID&#xff0c;可用如下查询语句&#xff1a; SELECT Row_Nu…

一个非常简单的C#面试题

怎样实现对所有类可读但是在同一个assembly可写那&#xff1f; 答案&#xff1a; 同一个assembly namespace ClassLibrary1 { public class Class1 { public string Name { get; internal set; } } public class Class2 { public void GS() { Class1 cc new Class1(); cc.Name…

css中的node.js_在Node App中使用基本HTML,CSS和JavaScript

css中的node.jsYou may think this is not important, but it is!. As a beginner in node.js, most coding exercises are always server sided. 您可能认为这并不重要&#xff0c;但确实如此&#xff01; 作为node.js的初学者&#xff0c;大多数编码练习始终都是服务器端的。…

Binary String Matching(C++)

题目描述: Given two strings A and B, whose alphabet consist only ‘0’ and ‘1’. Your task is only to tell how many times does A appear as a substring of B? For example, the text string B is ‘1001110110’ while the pattern string A is ‘11’, you should…

由一次代码优化想到的Js 数据类型

引子&#xff1a; 上周三进行了代码优化&#xff0c;其中有一个很普遍的代码&#xff0c;例如&#xff1a; if(test "") {dothis();}else{dothat()} ----->可以简化为 !test ? dothis():dothat(); if(test "") {dothis()}     ----->可以简化为…

VisualStudio2019配置OpenCV

VisualStudio2019配置OpenCV配置0x01 准备0x02 配置系统环境0x03 复制文件0x04 配置VisualStudio2019测试配置 0x01 准备 下载opencv&#xff0c;官网地址&#xff1a;https://opencv.org/releases/# 下载之后&#xff0c;自行安装 0x02 配置系统环境 找到高级系统设置 …

转载 Javascript的IE和Firefox兼容性汇编

微软关于IE、Firefox、Opera和Safari的JavaScript兼容性研究曾经发表过一份草案,可以点击下载《JScript Deviations from ES3》 以下为网上的一些搜集和整理(FF代表Firefox) 集合类对象问题现有代码中存在许多 document.form.item("itemName") 这样的语句&#xff0c…

存储器间接寻址方式_8086微处理器的程序存储器寻址模式

存储器间接寻址方式The Program Memory Addressing mode is used in branch instructions. These branch instructions are instructions which are responsible for changing the regular flow of the instruction execution and shifting the control to some other location…