📜  双向循环链表|设置 2(删除)

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

双向循环链表|设置 2(删除)

我们已经讨论了双循环链表的引入及其插入。
让我们制定问题陈述来理解删除过程。给定一个'key' ,删除循环双向链表中该键的第一次出现。

算法

案例 1:空列表(开始 = NULL)

  • 如果列表为空,只需返回它。

情况二: List最初包含一些节点,起点在List的第一个节点

  1. 如果链表不为空,那么我们定义两个指针currprev_1并初始化指针curr指向链表的第一个节点,prev_1 = NULL。
  2. 在从curr移动到下一个节点之前,使用curr指针遍历列表以找到要删除的节点,每次设置prev_1 = curr
  3. 如果找到该节点,则检查它是否是列表中的唯一节点。如果是,设置 start = NULL 并释放curr指向的节点。
  4. 如果链表有多个节点,检查它是否是链表的第一个节点。检查这个的条件是 (curr == start)。如果是,则将 prev_1 移动到最后一个节点(prev_1 = start -> prev)。在 prev_1 到达最后一个节点后,设置 start = start -> next and prev_1 -> next = start and start -> prev = prev_1。释放由 curr 指向的节点。
  5. 如果curr不是第一个节点,我们检查它是否是列表中的最后一个节点。检查这个的条件是 (curr -> next == start)。如果是,设置 prev_1 -> next = start 和 start -> prev = prev_1。释放由 curr 指向的节点。
  6. 如果要被删除的节点既不是第一也不节点的最后一个节点,声明一个更指针温度和初始化指针临时指向下一CURR指针的(温度= curr->下)。现在设置,prev_1 -> next = temp 和 temp ->prev = prev_1。释放由 curr 指向的节点。
  • 如果给定的(比如 4)与列表的第一个节点匹配(步骤 4):

删除第一个节点



  • 如果给定的(比如 8)与列表的最后一个节点匹配(步骤 5):

删除最后一个节点

  • 如果给定的(比如 6)与列表的中间节点匹配(步骤 6):

删除中间节点

C++
// C++ program to delete a given key from
// circular doubly linked list.
#include 
using namespace std;
 
// Structure of a Node
struct Node {
    int data;
    struct Node* next;
    struct Node* prev;
};
 
// Function to insert node in the list
void insert(struct Node** start, int value)
{
    // If the list is empty, create a single node
    // circular and doubly list
    if (*start == NULL) {
        struct Node* new_node = new Node;
        new_node->data = value;
        new_node->next = new_node->prev = new_node;
        *start = new_node;
        return;
    }
 
    // If list is not empty
 
    /* Find last node */
    Node* last = (*start)->prev;
 
    // Create Node dynamically
    struct Node* new_node = new Node;
    new_node->data = value;
 
    // Start is going to be next of new_node
    new_node->next = *start;
 
    // Make new node previous of start
    (*start)->prev = new_node;
 
    // Make last previous of new node
    new_node->prev = last;
 
    // Make new node next of old last
    last->next = new_node;
}
 
// Function to delete a given node from the list
void deleteNode(struct Node** start, int key)
{
    // If list is empty
    if (*start == NULL)
        return;
 
    // Find the required node
    // Declare two pointers and initialize them
    struct Node *curr = *start, *prev_1 = NULL;
    while (curr->data != key) {
        // If node is not present in the list
        if (curr->next == *start) {
            printf("\nList doesn't have node with value = %d", key);
            return;
        }
 
        prev_1 = curr;
        curr = curr->next;
    }
 
    // Check if node is the only node in list
    if (curr->next == *start && prev_1 == NULL) {
        (*start) = NULL;
        free(curr);
        return;
    }
 
    // If list has more than one node,
    // check if it is the first node
    if (curr == *start) {
        // Move prev_1 to last node
        prev_1 = (*start)->prev;
 
        // Move start ahead
        *start = (*start)->next;
 
        // Adjust the pointers of prev_1 and start node
        prev_1->next = *start;
        (*start)->prev = prev_1;
        free(curr);
    }
 
    // check if it is the last node
    else if (curr->next == *start) {
        // Adjust the pointers of prev_1 and start node
        prev_1->next = *start;
        (*start)->prev = prev_1;
        free(curr);
    }
    else {
        // create new pointer, points to next of curr node
        struct Node* temp = curr->next;
 
        // Adjust the pointers of prev_1 and temp node
        prev_1->next = temp;
        temp->prev = prev_1;
        free(curr);
    }
}
 
// Function to display list elements
void display(struct Node* start)
{
    struct Node* temp = start;
 
    while (temp->next != start) {
        printf("%d ", temp->data);
        temp = temp->next;
    }
    printf("%d ", temp->data);
}
 
// Driver program to test above functions
int main()
{
    // Start with the empty list
    struct Node* start = NULL;
 
    // Created linked list will be 4->5->6->7->8
    insert(&start, 4);
    insert(&start, 5);
    insert(&start, 6);
    insert(&start, 7);
    insert(&start, 8);
 
    printf("List Before Deletion: ");
    display(start);
 
    // Delete the node which is not present in list
    deleteNode(&start, 9);
    printf("\nList After Deletion: ");
    display(start);
 
    // Delete the first node
    deleteNode(&start, 4);
    printf("\nList After Deleting %d: ", 4);
    display(start);
 
    // Delete the last node
    deleteNode(&start, 8);
    printf("\nList After Deleting %d: ", 8);
    display(start);
 
    // Delete the middle node
    deleteNode(&start, 6);
    printf("\nList After Deleting %d: ", 6);
    display(start);
 
    return 0;
}


Java
// Java program to delete a given key from
// circular doubly linked list.
class GFG {
 
    // structure of a Node
    static class Node {
        int data;
        Node next;
        Node prev;
    };
 
    // Function to insert node in the list
    static Node insert(Node start, int value)
    {
        // If the list is empty, create a single node
        // circular and doubly list
        if (start == null) {
            Node new_node = new Node();
            new_node.data = value;
            new_node.next = new_node.prev = new_node;
            start = new_node;
            return start;
        }
 
        // If list is not empty
 
        // Find last node /
        Node last = (start).prev;
 
        // Create Node dynamically
        Node new_node = new Node();
        new_node.data = value;
 
        // Start is going to be next of new_node
        new_node.next = start;
 
        // Make new node previous of start
        (start).prev = new_node;
 
        // Make last previous of new node
        new_node.prev = last;
 
        // Make new node next of old last
        last.next = new_node;
        return start;
    }
 
    // Function to delete a given node from the list
    static Node deleteNode(Node start, int key)
    {
        // If list is empty
        if (start == null)
            return null;
 
        // Find the required node
        // Declare two pointers and initialize them
        Node curr = start, prev_1 = null;
        while (curr.data != key) {
            // If node is not present in the list
            if (curr.next == start) {
                System.out.printf("\nList doesn't have node with value = %d", key);
                return start;
            }
 
            prev_1 = curr;
            curr = curr.next;
        }
 
        // Check if node is the only node in list
        if (curr.next == start && prev_1 == null) {
            (start) = null;
            return start;
        }
 
        // If list has more than one node,
        // check if it is the first node
        if (curr == start) {
            // Move prev_1 to last node
            prev_1 = (start).prev;
 
            // Move start ahead
            start = (start).next;
 
            // Adjust the pointers of prev_1 and start node
            prev_1.next = start;
            (start).prev = prev_1;
        }
 
        // check if it is the last node
        else if (curr.next == start) {
            // Adjust the pointers of prev_1 and start node
            prev_1.next = start;
            (start).prev = prev_1;
        }
        else {
            // create new pointer, points to next of curr node
            Node temp = curr.next;
 
            // Adjust the pointers of prev_1 and temp node
            prev_1.next = temp;
            temp.prev = prev_1;
        }
        return start;
    }
 
    // Function to display list elements
    static void display(Node start)
    {
        Node temp = start;
 
        while (temp.next != start) {
            System.out.printf("%d ", temp.data);
            temp = temp.next;
        }
        System.out.printf("%d ", temp.data);
    }
 
    // Driver program to test above functions
    public static void main(String args[])
    {
        // Start with the empty list
        Node start = null;
 
        // Created linked list will be 4.5.6.7.8
        start = insert(start, 4);
        start = insert(start, 5);
        start = insert(start, 6);
        start = insert(start, 7);
        start = insert(start, 8);
 
        System.out.printf("List Before Deletion: ");
        display(start);
 
        // Delete the node which is not present in list
        start = deleteNode(start, 9);
        System.out.printf("\nList After Deletion: ");
        display(start);
 
        // Delete the first node
        start = deleteNode(start, 4);
        System.out.printf("\nList After Deleting %d: ", 4);
        display(start);
 
        // Delete the last node
        start = deleteNode(start, 8);
        System.out.printf("\nList After Deleting %d: ", 8);
        display(start);
 
        // Delete the middle node
        start = deleteNode(start, 6);
        System.out.printf("\nList After Deleting %d: ", 6);
        display(start);
    }
}
 
// This code is contributed by Arnab Kundu


Python3
# Python3 program to delete a given key from
# circular doubly linked list.
 
# structure of a node of linked list
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None
        self.prev = None
 
def insert( start, value):
     
    # If the list is empty, create a single node
    # circular and doubly list
    if (start == None):
        new_node = Node(0)
        new_node.data = value
        new_node.next = new_node.prev = new_node
        start = new_node
        return start
         
    # If list is not empty
 
    # Find last node /
    last = (start).prev
 
    # Create Node dynamically
    new_node = Node(0)
    new_node.data = value
 
    # Start is going to be next of new_node
    new_node.next = start
 
    # Make new node previous of start
    (start).prev = new_node
 
    # Make last previous of new node
    new_node.prev = last
 
    # Make new node next of old last
    last.next = new_node
    return start
     
# Function to delete a given node
# from the list
def deleteNode(start, key):
     
    # If list is empty
    if (start == None):
        return None
 
    # Find the required node
    # Declare two pointers and initialize them
    curr = start
    prev_1 = None
    while (curr.data != key) :
         
        # If node is not present in the list
        if (curr.next == start) :
            print ("List doesn't have node",
                       "with value = ", key)
            return start
             
        prev_1 = curr
        curr = curr.next
         
    # Check if node is the only node in list
    if (curr.next == start and prev_1 == None) :
        (start) = None
        return start
         
    # If list has more than one node,
    # check if it is the first node
    if (curr == start) :
         
        # Move prev_1 to last node
        prev_1 = (start).prev
 
        # Move start ahead
        start = (start).next
 
        # Adjust the pointers of prev_1
        # and start node
        prev_1.next = start
        (start).prev = prev_1
         
    # check if it is the last node
    elif (curr.next == start) :
         
        # Adjust the pointers of prev_1
        # and start node
        prev_1.next = start
        (start).prev = prev_1
         
    else :
         
        # create new pointer,
        # points to next of curr node
        temp = curr.next
 
        # Adjust the pointers of prev_1
        # and temp node
        prev_1.next = temp
        temp.prev = prev_1
         
    return start
     
# Function to display list elements
def display(start):
     
    temp = start
 
    while (temp.next != start) :
        print (temp.data, end = " ")
        temp = temp.next
         
    print (temp.data)
     
# Driver Code
if __name__=='__main__':
     
    # Start with the empty list
    start = None
 
    # Created linked list will be 4.5.6.7.8
    start = insert(start, 4)
    start = insert(start, 5)
    start = insert(start, 6)
    start = insert(start, 7)
    start = insert(start, 8)
 
    print ("List Before Deletion: ")
    display(start)
 
    # Delete the node which is not present in list
    start = deleteNode(start, 9)
    print ("List After Deletion: ")
    display(start)
 
    # Delete the first node
    start = deleteNode(start, 4)
    print ("List After Deleting", 4)
    display(start)
 
    # Delete the last node
    start = deleteNode(start, 8)
    print ("List After Deleting ", 8)
    display(start)
 
    # Delete the middle node
    start = deleteNode(start, 6)
    print ("List After Deleting ", 6)
    display(start)
     
# This code is contributed by Arnab Kundu


C#
// C# program to delete a given key from
// circular doubly linked list.
using System;
 
class GFG {
 
    // structure of a Node
    public class Node {
        public int data;
        public Node next;
        public Node prev;
    };
 
    // Function to insert node in the list
    static Node insert(Node start, int value)
    {
        // If the list is empty, create a single node
        // circular and doubly list
        Node new_node = new Node();
        if (start == null) {
 
            new_node.data = value;
            new_node.next = new_node.prev = new_node;
            start = new_node;
            return start;
        }
 
        // If list is not empty
 
        // Find last node /
        Node last = (start).prev;
 
        // Create Node dynamically
        new_node = new Node();
        new_node.data = value;
 
        // Start is going to be next of new_node
        new_node.next = start;
 
        // Make new node previous of start
        (start).prev = new_node;
 
        // Make last previous of new node
        new_node.prev = last;
 
        // Make new node next of old last
        last.next = new_node;
        return start;
    }
 
    // Function to delete a given node from the list
    static Node deleteNode(Node start, int key)
    {
        // If list is empty
        if (start == null)
            return null;
 
        // Find the required node
        // Declare two pointers and initialize them
        Node curr = start, prev_1 = null;
        while (curr.data != key) {
            // If node is not present in the list
            if (curr.next == start) {
                Console.Write("\nList doesn't have node with value = {0}", key);
                return start;
            }
 
            prev_1 = curr;
            curr = curr.next;
        }
 
        // Check if node is the only node in list
        if (curr.next == start && prev_1 == null) {
            (start) = null;
            return start;
        }
 
        // If list has more than one node,
        // check if it is the first node
        if (curr == start) {
            // Move prev_1 to last node
            prev_1 = (start).prev;
 
            // Move start ahead
            start = (start).next;
 
            // Adjust the pointers of prev_1 and start node
            prev_1.next = start;
            (start).prev = prev_1;
        }
 
        // check if it is the last node
        else if (curr.next == start) {
            // Adjust the pointers of prev_1 and start node
            prev_1.next = start;
            (start).prev = prev_1;
        }
        else {
            // create new pointer, points to next of curr node
            Node temp = curr.next;
 
            // Adjust the pointers of prev_1 and temp node
            prev_1.next = temp;
            temp.prev = prev_1;
        }
        return start;
    }
 
    // Function to display list elements
    static void display(Node start)
    {
        Node temp = start;
 
        while (temp.next != start) {
            Console.Write("{0} ", temp.data);
            temp = temp.next;
        }
        Console.Write("{0} ", temp.data);
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        // Start with the empty list
        Node start = null;
 
        // Created linked list will be 4.5.6.7.8
        start = insert(start, 4);
        start = insert(start, 5);
        start = insert(start, 6);
        start = insert(start, 7);
        start = insert(start, 8);
 
        Console.Write("List Before Deletion: ");
        display(start);
 
        // Delete the node which is not present in list
        start = deleteNode(start, 9);
        Console.Write("\nList After Deletion: ");
        display(start);
 
        // Delete the first node
        start = deleteNode(start, 4);
        Console.Write("\nList After Deleting {0}: ", 4);
        display(start);
 
        // Delete the last node
        start = deleteNode(start, 8);
        Console.Write("\nList After Deleting {0}: ", 8);
        display(start);
 
        // Delete the middle node
        start = deleteNode(start, 6);
        Console.Write("\nList After Deleting {0}: ", 6);
        display(start);
    }
}
 
// This code has been contributed by 29AjayKumar


Javascript


输出:

List Before Deletion: 4 5 6 7 8 
List doesn't have node with value = 9
List After Deletion: 4 5 6 7 8 
List After Deleting 4: 5 6 7 8 
List After Deleting 8: 5 6 7 
List After Deleting 6: 5 7 

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程