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
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 -1the output is:
9 8 7 6 5 4 3 2 1public class IntList {
// Linked list nodes
public IntNode headNode;
public IntNode tailNode;
public IntList() {
// Front of nodes list
headNode = null;
tailNode = null;
}
// append
public void append(IntNode newNode) {
if (headNode == null) { // List empty
headNode = newNode;
tailNode = newNode;
}
else {
tailNode.nextNode = newNode;
newNode.prevNode = tailNode;
tailNode = newNode;
}
}
// prepend
public void prepend(IntNode newNode) {
if (headNode == null) { // list empty
headNode = newNode;
tailNode = newNode;
}
else {
newNode.nextNode = headNode;
headNode.prevNode = newNode;
headNode = newNode;
}
}
// insertAfter
public void insertAfter(IntNode curNode, IntNode newNode) {
IntNode sucNode;
if (headNode == null) { // List empty
headNode = newNode;
tailNode = newNode;
}
else if (curNode == tailNode) { // Insert after tail
tailNode.nextNode = newNode;
newNode.prevNode = tailNode;
tailNode = newNode;
}
else {
sucNode = curNode.nextNode;
newNode.nextNode = sucNode;
newNode.prevNode = curNode;
curNode.nextNode = newNode;
sucNode.prevNode = newNode;
}
}
// TODO: Write insertInDescendingOrder() method
public void insertInDescendingOrder(IntNode newNode) {
}
public void printIntList() {
IntNode curNode;
curNode = headNode;
while (curNode != null) {
curNode.printNodeData();
System.out.print(" ");
curNode = curNode.nextNode;
}
}
}
_____________________________
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();
}
}
___________________________
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);
}
}
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 1 images