Question

In: Computer Science

Build a doubly linked list with these operations: AddToHead(10); AddToHead(20); AddToTail(30); AddToTail(40); Build a sorted doubly...

Build a doubly linked list with these operations:
AddToHead(10); AddToHead(20); AddToTail(30); AddToTail(40);
Build a sorted doubly linked list with these operations:
Add(30), Add(20), Add(40), Add(15), Add(35);

Solutions

Expert Solution

Build a doubly linked list with these operations:
AddToHead(10); AddToHead(20); AddToTail(30); AddToTail(40);

Code For Above Problem:(IN JAVA)


public class DoublyLinkedList {

        // Node of a doubly linked list
        private class Node {
                int data;
                Node next, prev;

                public Node(int data) {
                        this.data = data;
                        this.next = null;
                        this.prev = null;
                }
        }

        Node head;// refernce to the head node
        Node tail;// reference to the tail node

        public void AddToHead(int data) {
                /*
                 * allocate node put in the data
                 */
                Node newNode = new Node(data);

                // If list is empty
                if (head == null) {
                        // Both head and tail will point to newNode
                        head = tail = newNode;

                } else {
                        // newNode will be added before head such that head prev will point to newNode
                        head.prev = newNode;
                        // newNode's next will point to head
                        newNode.next = head;
                        // newNode will become new head
                        head = newNode;
                }

        }

        public void AddToTail(int data) {
                /*
                 * allocate node put in the data
                 */
                Node newNode = new Node(data);
                // If list is empty
                if (tail == null) {
                        // Both head and tail will point to newNode
                        head = tail = newNode;
                } else {
                        // newNode will be added after tail such that tail's next will point to newNode
                        tail.next = newNode;
                        // newNode's previous will point to tail
                        newNode.prev = tail;
                        // newNode will become new tail
                        tail = newNode;
                }
        }

        //Method to print the contents Of List
        public void print() {
                Node current = head;
                while (current != null) {
                        System.out.print(current.data + " ");
                        current = current.next;
                }
        }

        public static void main(String[] args) {

                //create new Object of DoublyLinkedList 
                DoublyLinkedList list = new DoublyLinkedList();
                //perform given operations
                list.AddToHead(10);
                list.AddToHead(20);
                list.AddToTail(30);
                list.AddToTail(40);

                //print the list
                list.print();
        }
}

Output Of Above Code:

20 10 30 40 

Images Of Code:

Image Of Output:

Build a sorted doubly linked list with these operations:
Add(30), Add(20), Add(40), Add(15), Add(35);

Code For Above Problem:(IN JAVA)


public class SortedDoublyLinkedList {

        // Node of a doubly linked list
        private class Node {
                int data;
                Node next, prev;

                public Node(int data) {
                        this.data = data;
                        this.next = null;
                        this.prev = null;
                }
        }

        Node head;// refernce to the head node
        Node tail;// reference to the tail node

        public void Add(int data) {
                
                /*
                 * allocate node put in the data
                 */
                Node newNode = new Node(data);
                Node current;

                // if list is empty
                if (head == null)
                        head = newNode;

                // if the node is to be inserted at the beginning
                // of the doubly linked list
                else if (head.data >= newNode.data) {
                        newNode.next = head;
                        newNode.next.prev = newNode;
                        head = newNode;
                }

                else {
                        current = head;

                        // locate the node after which the new node
                        // is to be inserted
                        while (current.next != null && current.next.data < newNode.data)
                                current = current.next;

                        /* Make the appropriate links */
                        newNode.next = current.next;

                        // if the new node is not inserted
                        // at the end of the list
                        if (current.next != null)
                                newNode.next.prev = newNode;

                        current.next = newNode;
                        newNode.prev = current;

                }
        }

        // Method to print the contents Of List
        public void print() {
                Node current = head;
                while (current != null) {
                        System.out.print(current.data + " ");
                        current = current.next;
                }
        }

        public static void main(String[] args) {

                //create new Object of SortedDoublyLinkedList
                SortedDoublyLinkedList list = new SortedDoublyLinkedList();
                // perform given operations
                list.Add(10);
                list.Add(20);
                list.Add(30);
                list.Add(40);
                list.print();

        }
}

Output Of Code:

10 20 30 40 

Images Of Code:

Image Of Output:


Related Solutions

!Must be written in C++! Build a doubly linked list with these operations: AddToHead(10); AddToHead(20); AddToTail(30);...
!Must be written in C++! Build a doubly linked list with these operations: AddToHead(10); AddToHead(20); AddToTail(30); AddToTail(40); Build a sorted doubly linked list with these operations: Add(30), Add(20), Add(40), Add(15), Add(35);
Can you make this singular linked list to doubly linked list Create a Doubly Linked List....
Can you make this singular linked list to doubly linked list Create a Doubly Linked List. Use this to create a Sorted Linked List, Use this to create a prioritized list by use. Bring to front those links recently queried. -----link.h------ #ifndef LINK_H #define LINK_H struct Link{ int data; Link *lnkNxt; }; #endif /* LINK_H */ ----main.cpp---- //System Level Libraries #include <iostream> //I/O Library using namespace std; //Libraries compiled under std #include"Link.h" //Global Constants - Science/Math Related //Conversions, Higher Dimensions...
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...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program that will update bank accounts stored in a master file using updates from a transaction file. The program will maintain accounts using a doubly linked list. The input data will consist of two text files: a master file and a transaction file. See data in Test section below.  The master file will contain only the current account data. For each account, it will contain account...
I was supposed to conver a singly linked list to a doubly linked list and everytime...
I was supposed to conver a singly linked list to a doubly linked list and everytime I run my program the output prints a bunch of random numbers constantly until I close the console. Here is the code. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> struct node { int data; struct node *next; struct node *prev; }; //this always points to first link struct node *head = NULL; //this always points to last link struct node *tail = NULL;...
Given two sorted linked lists, merge them into a third sorted linked list. If an element...
Given two sorted linked lists, merge them into a third sorted linked list. If an element is present in both the lists, it should occur only once in the third list. Code needed in java.
Explain and demonstrate a Linked List by adding following items into a Linked List: 10, 30,...
Explain and demonstrate a Linked List by adding following items into a Linked List: 10, 30, 15, 25 (show your work, you may write on paper and upload if you prefer)
Write the following algorithms for a Doubly Linked List Inserting an item                              
Write the following algorithms for a Doubly Linked List Inserting an item                                                                                                                              [7] Deleting an item                                                                                                                               [7] Question two Take a queue containing numbers 10, 15, 5, 25, 30 in which 30 has been inserted first. After performing the following operations, what would be the contents of the queue? Delete two elements                                                                                                                      [2] Insert 7 and then 20                                                                                                                        [2] Delete an element                                                                                                                          [2]
Question 1 For the dataset: 20, 20, 10, 10, 40, 50, 20, 30, 10, 20, 50,...
Question 1 For the dataset: 20, 20, 10, 10, 40, 50, 20, 30, 10, 20, 50, 60, 20, 30, 50, 20, 30, 40, 30, 30, 30, 50, 40 calculate the max, min, mode, median and mean.(20%) Draw a boxplot with inner and outer fence For the data in part (i), if the value 60 was replaced by 2000, what would you call this value in the dataset? What could be the explanation for such a value? How can you through...
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