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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

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

發(fā)布時間:2023/12/1 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 notepad++节点_在C ++中删除链接列表的中间节点 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

notepad++節(jié)點

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.

如果鏈接列表的長度為奇數(shù),則刪除鏈接列表的第((n + 1)/ 2)項;如果列表的長度為偶數(shù),則刪除喜歡列表的第(n / 2 + 1)項。

Example 1:

范例1:

If we have a Linked List : 1 → 2 → 3 → 4 → 5 → 6 → 7After 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 → 8After deleting the middle node the linked list will be: 1 → 2 → 3 → 4 → 6 → 7 → 85 is the middle node

Algorithm:

算法:

To solve the problem we follow the following procedure,

為了解決該問題,我們遵循以下過程,

  • 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).

    我們將兩個節(jié)點的指針名稱初始化為慢,快。 (就像弗洛伊德(Floyd)的烏龜算法一樣,請在鏈接列表中引用我的合并排序文章的鏈接)。

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

    每次我們將慢速指針加一,而快速指針加二。

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

    重復(fù)步驟2,直到快速指針移到鏈接列表的末尾。

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

    當(dāng)快速指針同時到達列表末尾時,慢速指針指向鏈接列表的中間。

  • Then delete the middle node.

    然后刪除中間節(jié)點。

  • C++ implementation:

    C ++實現(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; }//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++節(jié)點

    總結(jié)

    以上是生活随笔為你收集整理的notepad++节点_在C ++中删除链接列表的中间节点的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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