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
繁體地址,請注明出處:http://hk.pswp.cn/news/379715.shtml
英文地址,請注明出處:http://en.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…