Question

In: Computer Science

What is are the correct code implementations to remove a node from a circular doubly linked...

What is are the correct code implementations to remove a node from a circular doubly linked list from the beginning, middle, and end? Using only a dummy node and a pointer in the code implementation for the removal of the last/end node.

Solutions

Expert Solution

# Python implementation to delete

# a doubly Linked List node

# at the given position

# A node of the doubly linked list

class Node:

     

    # Constructor to create a new node

    def __init__(self, data):

        self.data = data

        self.next = None

        self.prev = None

# Function to delete a node in a Doubly Linked List.

# head_ref -. pointer to head node pointer.

# del -. pointer to node to be deleted.

def deleteNode(head_ref, del_):

    # base case

    if (head_ref == None or del_ == None):

        return

    # If node to be deleted is head node

    if (head_ref == del_):

        head_ref = del_.next

    # Change next only if node to be deleted is NOT

    # the last node

    if (del_.next != None):

        del_.next.prev = del_.prev

    # Change prev only if node to be deleted is NOT

    # the first node

    if (del_.prev != None):

        del_.prev.next = del_.next

         

    return head_ref

# Function to delete the node at the given position

# in the doubly linked list

def deleteNodeAtGivenPos(head_ref,n):

    # if list in None or invalid position is given

    if (head_ref == None or n <= 0):

        return

    current = head_ref

    i = 1

    # traverse up to the node at position 'n' from

    # the beginning

    while ( current != None and i < n ):

        current = current.next

        i = i + 1

    # if 'n' is greater than the number of nodes

    # in the doubly linked list

    if (current == None):

        return

    # delete the node pointed to by 'current'

    deleteNode(head_ref, current)

     

    return head_ref

# Function to insert a node at the beginning

# of the Doubly Linked List

def push(head_ref, new_data):

    # allocate node

    new_node = Node(0)

    # put in the data

    new_node.data = new_data

    # since we are adding at the beginning,

    #prev is always None

    new_node.prev = None

    # link the old list off the new node

    new_node.next = (head_ref)

    # change prev of head node to new node

    if ((head_ref) != None):

        (head_ref).prev = new_node

    # move the head to point to the new node

    (head_ref) = new_node

     

    return head_ref

# Function to print nodes in a given doubly

# linked list

def printList(head):

    while (head != None) :

        print( head.data ,end= " ")

        head = head.next

     

# Driver program to test above functions

# Start with the empty list

head = None

# Create the doubly linked list 10<.8<.4<.2<.5

head = push(head, 5)

head = push(head, 2)

head = push(head, 4)

head = push(head, 8)

head = push(head, 10)

print("Doubly linked list before deletion:")

printList(head)

n = 2

# delete node at the given position 'n'

head = deleteNodeAtGivenPos(head, n)

print("\nDoubly linked list after deletion:")

printList(head)

​​​​Please Give a thumps up for the answer!


Related Solutions

A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end;...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end; the nodes form a full circle. Instead of keeping track of the node at the front, we keep track of a current node instead. Write a class for a circular doubly-linked list using the attached Job class as your node objects. It should have: • A private instance variable for the current node • A getCurrent() method that returns a reference to the current...
C++ Remove every other node in a circular linked list USING RECURSION. You can only use...
C++ Remove every other node in a circular linked list USING RECURSION. You can only use the preprocessor directives <iostream> and using namespace std; You must use this prototype: int remove_every_other(node *&rear)
How do I remove a node from a linked list C++? void LinkedList::Remove(int offset){ shared_ptr<node> cursor(top_ptr_);...
How do I remove a node from a linked list C++? void LinkedList::Remove(int offset){ shared_ptr<node> cursor(top_ptr_); shared_ptr<node> temp(new node); if(cursor == NULL) { temp = cursor-> next; cursor= temp; if (temp = NULL) { temp->next = NULL; } } else if (cursor-> next != NULL) { temp = cursor->next->next; cursor-> next = temp; if (temp != NULL) { temp->next = cursor; } } }
Consider a “Remove” operation from a doubly linked list other than front and end. Explain the...
Consider a “Remove” operation from a doubly linked list other than front and end. Explain the implementation of “Remove” operation using a drawing by showing appropriate links. Using the explanation of (a) write the statements to implement the “Remove” operation. Using (b) show that the complexity of “Remove” operation is O(1).
This is the code what I have for doubly linked list for STACK. This is Python...
This is the code what I have for doubly linked list for STACK. This is Python language and I want anyone to help me with the following questions. Can you check for me if it is good Doubly Linked List? ####THIS IS THE ENTIRE ASSIGNMENT#### ADD the Following feature: Include a class attribute in the container class called name. In the implementation - Pod: You should ask the user to enter the name of the container and the program should...
Python class DLLNode: """ Class representing a node in the doubly linked list implemented below. """...
Python class DLLNode: """ Class representing a node in the doubly linked list implemented below. """ def __init__(self, value, next=None, prev=None): """ Constructor @attribute value: the value to give this node @attribute next: the next node for this node @attribute prev: the previous node for this node """ self.__next = next self.__prev = prev self.__value = value def __repr__(self): return str(self.__value) def __str__(self): return str(self.__value) def get_value(self): """ Getter for value :return: the value of the node """ return self.__value...
Description The purpose of this challenge is to implement a circular doubly-linked list using a dummy...
Description The purpose of this challenge is to implement a circular doubly-linked list using a dummy node. This challenge simulates an operating system’s window manager. Requirements Write the following struct struct Window { string appname; Window *next; Window *prev; }; Create a class called WindowManager. In this class, create a private variable Window * head. This will keep track of the location of the head node. Create private variables Window * current, * dummy. current will keep track of the...
Write PSEUDOCODE to insert a node at position 2 in a doubly-linked list (assume position follows...
Write PSEUDOCODE to insert a node at position 2 in a doubly-linked list (assume position follows classic indexing from 0 to item_count - 1)
Remove the Head element from the code below: public class LinkedList {    class Node{ int...
Remove the Head element from the code below: public class LinkedList {    class Node{ int value; Node nextElement; public Node(int value) { this.value = value; this.nextElement = null; } } public Node first = null; public Node last = null; public void addNewNode(int element) { Node newValueNode = new Node(element);    if(first == null) { first = newValueNode; } else { last.nextElement = newValueNode; } last = newValueNode; } public void displayValues() { Node recent = first; if(first ==...
I know this code takes in a set of numbers into a doubly linked list and...
I know this code takes in a set of numbers into a doubly linked list and sorts it using insertion sort. Could you explain exactly how this code is working? main.c Source Code: #include #include #include "node.h" int main() { struct mynode *head=NULL; int value; printf("Give first value: \n"); scanf("%d",&value); printf("Give next values, and input 0 to end list: \n"); do{ if(value>0){    head = pushNode(head, value);    scanf("%d",&value); } }while (value>0); printf("Before insertion sort: "); printlist(head); head=insertsort(head); printf("After insertion...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT