Question

In: Computer Science

9.7 LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class,...

9.7 LAB: Inserting an integer in descending order (doubly-linked list)

Given main() and an IntNode class, complete the IntList class (a linked list of IntNodes) by writing the insertInDescendingOrder() method to insert new IntNodes into the IntList in descending order.

Ex. If the input is:

3 4 2 5 1 6 7 9 8 -1

the output is:

9 8 7 6 5 4 3 2 1

Sortedlist.java

import java.util.Scanner;

public class SortedList {

public static void main (String[] args) {
Scanner scnr = new Scanner(System.in);
IntList intList = new IntList();
IntNode curNode;
int num;

num = scnr.nextInt();

while (num != -1) {
// Insert into linked list in descending order   
curNode = new IntNode(num);
intList.insertInDescendingOrder(curNode);
num = scnr.nextInt();
}
  
intList.printIntList();
}
}

intlist.java

import java.util.Scanner;

public class SortedList {

public static void main (String[] args) {
Scanner scnr = new Scanner(System.in);
IntList intList = new IntList();
IntNode curNode;
int num;

num = scnr.nextInt();

while (num != -1) {
// Insert into linked list in descending order   
curNode = new IntNode(num);
intList.insertInDescendingOrder(curNode);
num = scnr.nextInt();
}
  
intList.printIntList();
}
}

intnode.java

public class IntNode {
public int dataVal;
public IntNode prevNode; // Reference to the previous node   
public IntNode nextNode; // Reference to the next node   

public IntNode() {
dataVal = 0;
prevNode = null;
nextNode = null;
}

// Constructor   
public IntNode(int dataInit) {
this.dataVal = dataInit;
this.prevNode = null;
this.nextNode = null;
}

// Constructor   
public IntNode(int dataInit, IntNode prevNode, IntNode newNextNode) {
this.dataVal = dataInit;
this.prevNode = prevNode;
this.nextNode = newNextNode;
}

// Print node information
public void printNodeData() {
System.out.print(this.dataVal);
}
}

Solutions

Expert Solution

//IntNode.java

public class IntNode {

      

       public int dataVal;

       public IntNode prevNode; // Reference to the previous node  

       public IntNode nextNode; // Reference to the next node  

       public IntNode() {

             dataVal = 0;

             prevNode = null;

             nextNode = null;

       }

      

       // Constructor  

       public IntNode(int dataInit) {

             this.dataVal = dataInit;

             this.prevNode = null;

             this.nextNode = null;

       }

       // Constructor  

       public IntNode(int dataInit, IntNode prevNode, IntNode newNextNode) {

             this.dataVal = dataInit;

             this.prevNode = prevNode;

             this.nextNode = newNextNode;

       }

      

       // Print node information

       public void printNodeData() {

             System.out.print(this.dataVal);

       }

}

//end of IntNode.java

//IntList.java

public class IntList {

      

       private IntNode head;

      

       // constructor to create an empty list

       public IntList()

       {

             head = null;

       }

      

       // method to insert the nodes in descending order

       public void insertInDescendingOrder(IntNode node)

       {

             // if empty list, make the node the first element

             if(head == null )

                    head = node;

             // if value of node > value at head, insert it at the front

             else if(head.dataVal < node.dataVal)

             {

                    node.nextNode = head;

                    head.prevNode = node;

                    head = node;

             }else

             {

                    IntNode temp = head;

                    // loop to continue till we get the position of the insert or we reach the element at the end of the list

                    while(temp.nextNode != null)

                    {

                           // check if this is the position of insert, then insert the node

                           if(temp.dataVal < node.dataVal)

                           {

                                 temp.prevNode.nextNode = node;

                                 node.prevNode = temp.prevNode;

                                 node.nextNode = temp;

                                 temp.prevNode= node;

                                 return;

                           }

                          

                           temp = temp.nextNode;

                    }

                   

                    // if last value is less than node value

                    if(temp.dataVal < node.dataVal)

                    {

                           node.prevNode = temp.prevNode;

                           temp.prevNode.nextNode = node;

                           node.nextNode = temp;

                           temp.prevNode= node;

                    }else // insert at the last

                    {

                           node.prevNode = temp;

                           temp.nextNode = node;

                           node.nextNode = null;

                    }

             }

       }

      

       // method to print the list

       public void printIntList()

       {

             if(head == null) // empty list

             {

                    System.out.println("Empty List");

             }else // non-empty list

             {

                    IntNode curr = head;

                    // loop to print the elements of the list

                    while(curr != null)

                    {

                           System.out.print(curr.dataVal+" ");

                           curr = curr.nextNode;

                    }

             }

       }

}

//end of IntList.java

//SortedList.java

import java.util.Scanner;

public class SortedList {

public static void main (String[] args) {
Scanner scnr = new Scanner(System.in);
IntList intList = new IntList();
IntNode curNode;
int num;

num = scnr.nextInt();

while (num != -1) {
// Insert into linked list in descending order   
curNode = new IntNode(num);
intList.insertInDescendingOrder(curNode);
num = scnr.nextInt();
}
  
intList.printIntList();
}
}

//end of SortedList.java

Output:


Related Solutions

9.8 LAB: Finding the first and last occurrence of a value (doubly-linked list) Given main() and...
9.8 LAB: Finding the first and last occurrence of a value (doubly-linked list) Given main() and a PeopleNode class, complete the PeopleList class by writing findFirst() and findLast() methods. The findFirst() method should find the first occurrence of an age value in the linked list and return the corresponding node. Similarly, the findLast() method should find the last occurrence of the age value in the linked list and return the corresponding node. For both methods, if the age value is...
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]
Write in C++: create a Doubly Linked List class that holds a struct with an integer...
Write in C++: create a Doubly Linked List class that holds a struct with an integer and a string. It must have append, insert, remove, find, and clear.
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...
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...
Develop a C++ "doubly" linked list class of your own that can hold a series of...
Develop a C++ "doubly" linked list class of your own that can hold a series of signed shorts Develop the following functionality: Develop a linked list node struct/class You can use it as a subclass like in the book (Class contained inside a class) You can use it as its own separate class Your choice Maintain a private pointer to a node class pointer (head) Constructor Initialize head pointer to null Destructor Make sure to properly delete every node in...
Using doubly linked list in c++ with class constructor: DNode(){    song = Song();    prev...
Using doubly linked list in c++ with class constructor: DNode(){    song = Song();    prev = NULL;    next = NULL; } DNode(string s, string a, int lenmin, int lensec){    song = Song(s,a,lenmin,lensec);    prev = NULL;    next = NULL; } for each node. Write the method: void moveUp(string t); This method moves a song up one in the playlist. For instance, if you had the following: Punching in a Dream, The Naked And Famous................3:58 Harder To...
Given a doubly linked list in c++, how do I create a function that returns the...
Given a doubly linked list in c++, how do I create a function that returns the pointer to first node in the given pattern, For example, given mainList (a -> b -> c -> d) and sublist  (b -> c), our function should return a Node pointer that points to first node of the sublist in the mainList. If the pattern doesn't exist in the mainList, we should return a nullptr, there are multiple of the same sublist in the mainList,...
Author code /** * LinkedList class implements a doubly-linked list. */ public class MyLinkedList<AnyType> implements Iterable<AnyType>...
Author code /** * LinkedList class implements a doubly-linked list. */ public class MyLinkedList<AnyType> implements Iterable<AnyType> { /** * Construct an empty LinkedList. */ public MyLinkedList( ) { doClear( ); } private void clear( ) { doClear( ); } /** * Change the size of this collection to zero. */ public void doClear( ) { beginMarker = new Node<>( null, null, null ); endMarker = new Node<>( null, beginMarker, null ); beginMarker.next = endMarker; theSize = 0; modCount++; } /**...
//LinkNode is a class for storing a single node of a linked list storing integer values....
//LinkNode is a class for storing a single node of a linked list storing integer values. It has two public data fields for the data and the link to //the next node in the list and has three constructors: public class LinkNode { public int data;       public LinkNode next; // post: constructs a node with data 0 and null link public ListNode() {      this(0, null); } // post: constructs a node with given data and null link public LinkNode (int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT