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

Method in C language for inserting in descending order in a single linked list. It has...
Method in C language for inserting in descending order in a single linked list. It has a head and tail variable.
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.
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...
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...
Using the singly linked list code as a base, create a class that implements a doubly...
Using the singly linked list code as a base, create a class that implements a doubly linked list. A doubly linked list has a Previous link so you can move backwards in the list. Be sure the class is a template class so the user can create a list with any data type. Be sure to test all the member functions in your test program. c++
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;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT