Question

public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node...

public class DoublyLinkedList {
    Node Head; // head of Doubly Linked List

    //Doubly Linked list Node
    class Node {
        int value;
        Node prev;
        Node next;

        // Constructor to create a new node







        Node(int d) { value = d; }
    }

    // Inserting a node at the front of the list
    public void add(int newData)
    {
        // allocate node and put in the data
        Node newNode = new Node(newData);

        // Make the next of new node as head
        // and previous as NULL
        newNode.next = Head;
        newNode.prev = null;

        //change prev of head node to the new node
        if (Head != null)
            Head.prev = newNode;

        //move the head to point to the new node
        Head = newNode;
    }

    // Function to delete a node in a Doubly Linked List.
    void remove(Node del)
    {

        // Base case
        if (Head == null || del == null) {
            return;
        }

        // If node to be deleted is head node
        if (Head == del) {
            Head = del.next;
        }

        // When node to be deleted is not
        // the last node, change next.
        if (del.next != null) {
            del.next.prev = del.prev;
        }

        // When node to be removed is not the first node,
        // change prev.
        if (del.prev != null) {
            del.prev.next = del.next;
        }

        return;
    }
    //Function to display Doubly Linked List content
    public void display(Node node)
    {
        Node last = null;

        while (node != null) {
            System.out.print(node.value + " ");
            last = node;
            node = node.next;
        }

        System.out.println();
    }



    public static void main(String[] args)
    {
        // Initiallize the Doubly Linked List
        DoublyLinkedList d = new DoublyLinkedList();

        // add 6.
        d.add(6);

        // add 19.
        d.add(19);

        // add 2.
        d.add(2);

        // add 15.
        d.add(15);

        System.out.print("Doubly Linked List ");
        d.display(d.Head);

        // Removing first node
        d.remove(d.Head);

        // List after removing first node
        // 2->19->6
        System.out.print("\nDoubly Linked List after removing first node: ");
        d.display(d.Head);

        // Removing middle node from 2->19->6
        d.remove(d.Head.next);

        System.out.print("\nDoubly Linked List after removing middle node: ");
        d.display(d.Head);
    }
}

WRITE THE UML DIAGRAM AND PSEUDO CODE

Homework Answers

Answer #1

The UML Class diagram is:

The pseudocode is given as:

Pseudocode

Each Node of the doubly linked list consists of the data value, a pointer pointing to the previous element and a next pointer pointing to the next element.

// add method inserts a node at the front of the list

function add (newData) {

newNode = Node(newData); // create a new node with the given data

newNode.next = Head

newNode.prev = null

if Head is not null then

Head.prev = newNode

end if

Head = newNode

}

// this function deletes a node in a Doubly Linked List.

function remove(del){

if (Head is null or del is null) then

return

end if

if Head is same as del) then

Head = del.next

end if

if (del.next is not null) then

del.next.prev = del.prev

end if

if (del.prev is not null) then

del.prev.next = del.next

end if

}

// display() displays Doubly Linked List content

function display(node) {

last = null

run till (node is not null) do

print node.value

last = node

node = node.next

end loop   

}

System.out.println();

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In this code, I build a single-linked list using a node class that has been created....
In this code, I build a single-linked list using a node class that has been created. How could I change this code to take data of type T, rather than int. (PS: ignore the fact that IOHelper.getInt won't work for the type T... ie second half of main). Here's my code right now: public class SLList { public SLNode head = null; public SLNode tail = null; public void add(int a) {// add() method present for testing purposes SLNode newNode...
C++ Assumption and Terminology: Forward Linked List FLL has "head" Doubly Linked List DLL has "head"...
C++ Assumption and Terminology: Forward Linked List FLL has "head" Doubly Linked List DLL has "head" and "tail" Each node in FLL has "next" Each node in DLL has "next" and "prev" Q1: Complete the following constructor code for FLL FLL::FLL() { Q2: Complete the FLL insert-front code (which insert node new at the front of FLL) FLL::insert-front( node * new) { Q3: Complete the DLL insert-after code (which insert node new after node p) DLL::insert-after(node *p, node *new) {
based on the code below, answer the questions Question 1: The LinkedList class uses another class...
based on the code below, answer the questions Question 1: The LinkedList class uses another class called Node (which is defined inside LinkedList.java). What are the fields in the Node class? Question 2: The Node class uses generics. Why? Question 3: The linkFirst method contains the following lines of code: if (f == null) last = newNode; else f.prev = newNode; What is the value of the linked list’s size attribute at this point in the code if f ==...
1. BQUEUE.h #pragma once #include class bqnode { public: int time; bqnode *prev, *next; }; class...
1. BQUEUE.h #pragma once #include class bqnode { public: int time; bqnode *prev, *next; }; class BQUEUE { public: BQUEUE(); ~BQUEUE(); BQUEUE(const BQUEUE &); void Enqueue(int); void Dequeue(); void Print(); private: bqnode * front; //use ONLY one pointer }; 2. BQUEUE.cpp #include "BQUEUE.h" using namespace std; BQUEUE::BQUEUE() { } BQUEUE::~BQUEUE() { } BQUEUE::BQUEUE(const BQUEUE & otherList) { if (otherList.front == NULL) return; front = new bqnode(); bqnode *curr = front; bqnode *oldnode = otherList.front; curr->time = oldnode->time; curr->next = NULL;...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using pointers correctly and deleting memory properly? #ifndef DYNAMICARRAY_H #define DYNAMICARRAY_H #include <cstdlib> #include <iostream> using namespace std; // Node class class Node { int data; Node* next; Node* prev; public: Node(); Node(int); void SetData(int newData) { data = newData; }; void SetNext(Node* newNext) { next = newNext; }; void SetPrev(Node* newPrev) { prev = newPrev; }; int getData() { return data; }; Node* getNext()...
1. Design and implement a CircularLinkedList, which is essentially a linked list in which the next...
1. Design and implement a CircularLinkedList, which is essentially a linked list in which the next reference of the tail node is set to refer back to the head of the list (rather than null) . Start from the SinglyLinkedList provided in the Lecture (not the built-in Java LinkedList class). 2. Starting from  SinglyLinkedList you will need to modify or complete methods: first(), last(), addFirst(), addLast(), removeFirst(), toString(), and also create a new rotate() method which will move the tail to...
Given this definition of a generic Linked List node: public class LLNode {     private T...
Given this definition of a generic Linked List node: public class LLNode {     private T data;     private LLNode next;     public LLNode(T data, LLNode next) {           this.data = data;           this.next = next;     }     public void setNext(LLNode newNext){ next = newNext; }     public LLNode getNext(){ return next; }     public T getData() {return data;} } Write the findMinimumNode method body. This method returns the linked list node that contains the minimum value in the...
Write in Java (Not Javascript) Provide an implementation of priority queue using double-ended doubly linked lists....
Write in Java (Not Javascript) Provide an implementation of priority queue using double-ended doubly linked lists. Recall that double-ended means keeping first and last references and doubly linked feature allows us to go backwards, using a prev reference at each Link. Also, note that the greater the number, the lower the priority. For instance, 2 is of higher priority compared to 5. Specifically, write a class LinkedListPriorityQ which implements the priority queue methods: boolean isEmpty() void enqueue(int item) int dequeue()...
c++ data structures linked list delete node bool deleteNode(int); pass this method an id to delete....
c++ data structures linked list delete node bool deleteNode(int); pass this method an id to delete. Return true or false to indicate success or failure. Delete the memory the node was using The algorithm for deleting a Node to a Linked List (general case): ● Begin at the head. ● Compare the id to the current node. ● If search id > current id, stop. ● Detach the current Node ○ current->previous->next = current->next ○ current->next->previous = current->previous ● Deallocate...