📌  相关文章
📜  用于在链表中间插入节点的 C++ 程序

📅  最后修改于: 2022-05-13 01:57:45.248000             🧑  作者: Mango

用于在链表中间插入节点的 C++ 程序

给定一个包含n 个节点的链表。问题是在列表中间插入一个带有数据x的新节点。如果n是偶数,则在第(n/2)个节点之后插入新节点,否则在第(n+1)/2个节点之后插入新节点。

例子:

Input : list: 1->2->4->5
        x = 3
Output : 1->2->3->4->5

Input : list: 5->10->4->32->16
        x = 41
Output : 5->10->4->41->32->16

方法一(使用链表的长度):
使用一次遍历查找链接的节点数或长度。让它成为len 。计算c = (len/2),如果len是偶数,否则c = (len+1)/2,如果len是奇数。再次遍历前c个节点,在第c个节点之后插入新节点。

C++
// C++ implementation to insert node at the middle
// of the linked list
#include 
  
using namespace std;
  
// structure of a node
struct Node {
    int data;
    Node* next;
};
  
// function to create and return a node
Node* getNode(int data)
{
    // allocating space
    Node* newNode = (Node*)malloc(sizeof(Node));
  
    // inserting the required data
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}
  
// function to insert node at the middle
// of the linked list
void insertAtMid(Node** head_ref, int x)
{
    // if list is empty
    if (*head_ref == NULL)
        *head_ref = getNode(x);
    else {
  
        // get a new node
        Node* newNode = getNode(x);
  
        Node* ptr = *head_ref;
        int len = 0;
  
        // calculate length of the linked list
        //, i.e, the number of nodes
        while (ptr != NULL) {
            len++;
            ptr = ptr->next;
        }
  
        // 'count' the number of nodes after which
        //  the new node is to be inserted
        int count = ((len % 2) == 0) ? (len / 2) :
                                    (len + 1) / 2;
        ptr = *head_ref;
  
        // 'ptr' points to the node after which 
        // the new node is to be inserted
        while (count-- > 1)
            ptr = ptr->next;
  
        // insert the 'newNode' and adjust the
        // required links
        newNode->next = ptr->next;
        ptr->next = newNode;
    }
}
  
// function to display the linked list
void display(Node* head)
{
    while (head != NULL) {
        cout << head->data << " ";
        head = head->next;
    }
}
  
// Driver program to test above
int main()
{
    // Creating the list 1->2->4->5
    Node* head = NULL;
    head = getNode(1);
    head->next = getNode(2);
    head->next->next = getNode(4);
    head->next->next->next = getNode(5);
  
    cout << "Linked list before insertion: ";
    display(head);
  
    int x = 3;
    insertAtMid(&head, x);
  
    cout << "
Linked list after insertion: ";
    display(head);
  
    return 0;
}


C++
// C++ implementation to insert node at the middle
// of the linked list
#include 
  
using namespace std;
  
// structure of a node
struct Node {
    int data;
    Node* next;
};
  
// function to create and return a node
Node* getNode(int data)
{
    // allocating space
    Node* newNode = (Node*)malloc(sizeof(Node));
  
    // inserting the required data
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}
  
// function to insert node at the middle
// of the linked list
void insertAtMid(Node** head_ref, int x)
{
    // if list is empty
    if (*head_ref == NULL)
        *head_ref = getNode(x);
  
    else {
        // get a new node
        Node* newNode = getNode(x);
  
        // assign values to the slow and fast 
        // pointers
        Node* slow = *head_ref;
        Node* fast = (*head_ref)->next;
  
        while (fast && fast->next) {
  
            // move slow pointer to next node
            slow = slow->next;
  
            // move fast pointer two nodes at a time
            fast = fast->next->next;
        }
  
        // insert the 'newNode' and adjust the
        // required links
        newNode->next = slow->next;
        slow->next = newNode;
    }
}
  
// function to display the linked list
void display(Node* head)
{
    while (head != NULL) {
        cout << head->data << " ";
        head = head->next;
    }
}
  
// Driver program to test above
int main()
{
    // Creating the list 1->2->4->5
    Node* head = NULL;
    head = getNode(1);
    head->next = getNode(2);
    head->next->next = getNode(4);
    head->next->next->next = getNode(5);
  
    cout << "Linked list before insertion: ";
    display(head);
  
    int x = 3;
    insertAtMid(&head, x);
  
    cout << "
Linked list after insertion: ";
    display(head);
  
    return 0;
}


输出:

Linked list before insertion: 1 2 4 5
Linked list after insertion: 1 2 3 4 5

时间复杂度: O(n)

方法2(使用两个指针):
基于使用两个指针的龟兔算法,一个称为指针,另一个称为指针。该算法有助于找到链表的中间节点。在这篇文章的正面和黑色分裂过程中进行了解释。现在,您可以在通过上述过程获得的中间节点之后插入新节点。这种方法只需要一次遍历列表。

C++

// C++ implementation to insert node at the middle
// of the linked list
#include 
  
using namespace std;
  
// structure of a node
struct Node {
    int data;
    Node* next;
};
  
// function to create and return a node
Node* getNode(int data)
{
    // allocating space
    Node* newNode = (Node*)malloc(sizeof(Node));
  
    // inserting the required data
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}
  
// function to insert node at the middle
// of the linked list
void insertAtMid(Node** head_ref, int x)
{
    // if list is empty
    if (*head_ref == NULL)
        *head_ref = getNode(x);
  
    else {
        // get a new node
        Node* newNode = getNode(x);
  
        // assign values to the slow and fast 
        // pointers
        Node* slow = *head_ref;
        Node* fast = (*head_ref)->next;
  
        while (fast && fast->next) {
  
            // move slow pointer to next node
            slow = slow->next;
  
            // move fast pointer two nodes at a time
            fast = fast->next->next;
        }
  
        // insert the 'newNode' and adjust the
        // required links
        newNode->next = slow->next;
        slow->next = newNode;
    }
}
  
// function to display the linked list
void display(Node* head)
{
    while (head != NULL) {
        cout << head->data << " ";
        head = head->next;
    }
}
  
// Driver program to test above
int main()
{
    // Creating the list 1->2->4->5
    Node* head = NULL;
    head = getNode(1);
    head->next = getNode(2);
    head->next->next = getNode(4);
    head->next->next->next = getNode(5);
  
    cout << "Linked list before insertion: ";
    display(head);
  
    int x = 3;
    insertAtMid(&head, x);
  
    cout << "
Linked list after insertion: ";
    display(head);
  
    return 0;
}

输出:

Linked list before insertion: 1 2 4 5
Linked list after insertion: 1 2 3 4 5

时间复杂度: O(n)

详情请参考完整文章将节点插入链表中间!