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

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter9: Advanced Array Concepts
Section: Chapter Questions
Problem 18RQ
icon
Related questions
Question

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
 

public 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);
}
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 1 images

Blurred answer
Knowledge Booster
Operations of Linked List
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT