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 (READ ONLY!!!)
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
public class IntList {
// Linked list nodes
public IntNode headNode;
public IntNode tailNode;
public IntList() {
// Front of nodes list
headNode = null;
tailNode = null;
}
// prepend
public void prepend(IntNode newNode) {
if (headNode == null) { // list empty
headNode = newNode;
tailNode = newNode;
} else {
newNode.nextNode = headNode;
headNode = newNode;
}
}
// insertAfter
public void insertAfter(IntNode curNode, IntNode newNode) {
if (headNode == null) { // List empty
headNode = newNode;
tailNode = newNode;
} else if (curNode == tailNode) { // Insert after tail
tailNode.nextNode = newNode;
tailNode = newNode;
} else {
newNode.nextNode = curNode.nextNode;
curNode.nextNode = newNode;
}
}
// method to insert a node in proper position so that the list will be in
// sorted order
public void insertInAscendingOrder(IntNode newNode) {
// checking if headNode is null (empty list - case 1)
if (headNode == null) {
// adding as both head node and tail node
headNode = newNode;
tailNode = newNode;
// returning from function
return;
}
// checking if new element could be added before current head node (case
// 2)
if (newNode.dataVal < headNode.dataVal) {
// adding before head node and updating head node
newNode.nextNode = headNode;
headNode = newNode;
return;
}
// taking a reference to first two nodes
IntNode current = headNode;
IntNode nextNode = headNode.nextNode;
// looping and checking if the element can be added in between any pairs
// of node (case 3)
while (nextNode != null) {
if (newNode.dataVal >= current.dataVal
&& newNode.dataVal <= nextNode.dataVal) {
// new node can be added between current and next node
newNode.nextNode = nextNode;
current.nextNode = newNode;
return;
}
// advancing to next pair of nodes
current = nextNode;
nextNode = current.nextNode;
}
// at the end, if the new node is still not added, adding to tail (case
// 4)
tailNode.nextNode = newNode;
tailNode = newNode;
}
public void printIntList() {
IntNode curNode;
curNode = headNode;
while (curNode != null) {
curNode.printNodeData();
System.out.print(" ");
curNode = curNode.nextNode;
}
}
}
____________________________________________________________________________________________________________________________________________________
IntNode.java (READ ONLY!!!!)
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);
}
}
class IntList { // Linked list nodes public IntNode headNode; public IntNode tailNode; public IntList() { // Front of nodes list headNode = null; tailNode = null; } // prepend public void prepend(IntNode newNode) { if (headNode == null) { // list empty headNode = newNode; tailNode = newNode; } else { newNode.nextNode = headNode; headNode = newNode; } } // insertAfter public void insertAfter(IntNode curNode, IntNode newNode) { if (headNode == null) { // List empty headNode = newNode; tailNode = newNode; } else if (curNode == tailNode) { // Insert after tail tailNode.nextNode = newNode; tailNode = newNode; } else { newNode.nextNode = curNode.nextNode; curNode.nextNode = newNode; } } // method to insert a node in proper position so that the list will be in // sorted order public void insertInAscendingOrder(IntNode newNode) { // checking if headNode is null (empty list - case 1) if (headNode == null) { // adding as both head node and tail node headNode = newNode; tailNode = newNode; // returning from function return; } // checking if new element could be added before current head node (case // 2) if (newNode.dataVal < headNode.dataVal) { // adding before head node and updating head node newNode.nextNode = headNode; headNode = newNode; return; } // taking a reference to first two nodes IntNode current = headNode; IntNode nextNode = headNode.nextNode; // looping and checking if the element can be added in between any pairs // of node (case 3) while (nextNode != null) { if (newNode.dataVal >= current.dataVal && newNode.dataVal <= nextNode.dataVal) { // new node can be added between current and next node newNode.nextNode = nextNode; current.nextNode = newNode; return; } // advancing to next pair of nodes current = nextNode; nextNode = current.nextNode; } // at the end, if the new node is still not added, adding to tail (case // 4) tailNode.nextNode = newNode; tailNode = newNode; } public void printIntList() { IntNode curNode; curNode = headNode; while (curNode != null) { curNode.printNodeData(); System.out.print(" "); curNode = curNode.nextNode; } } public void insertInDescendingOrder(IntNode newNode) { // checking if headNode is null (empty list - case 1) if (headNode == null) { // adding as both head node and tail node headNode = newNode; tailNode = newNode; // returning from function return; } // checking if new element could be added before current head node (case // 2) if (newNode.dataVal > headNode.dataVal) { // adding before head node and updating head node newNode.nextNode = headNode; headNode = newNode; return; } // taking a reference to first two nodes IntNode current = headNode; IntNode nextNode = headNode.nextNode; // looping and checking if the element can be added in between any pairs // of node (case 3) while (nextNode != null) { if (newNode.dataVal <= current.dataVal && newNode.dataVal >= nextNode.dataVal) { // new node can be added between current and next node newNode.nextNode = nextNode; current.nextNode = newNode; return; } // advancing to next pair of nodes current = nextNode; nextNode = current.nextNode; } // at the end, if the new node is still not added, adding to tail (case // 4) tailNode.nextNode = newNode; tailNode = newNode; } }
please upvote. if any issues, please ask in comments.
LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete...
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...
import java.util.Scanner;public class ShoppingList { public static void main (String[] args) { Scanner scnr = new Scanner(System.in); ItemNode headNode; // Create intNode objects ItemNode currNode; ItemNode lastNode; String item; int i; // Front of nodes list ...
10.16 LAB: Grocery shopping list (linked list: inserting at the end of a list)Hello, I searched for similar problems, but their InsertAtEnd() have two parameters, while my question asks for one.Given main(), define an InsertAtEnd() member function in the ItemNode class that adds an element to the end of a linked list.DO NOT print the dummy head node.Ex. if the input is:4 Kale Lettuce Carrots Peanutswhere 4 is the number of items to be inserted; Kale, Lettuce, Carrots, Peanuts are...
import java.util.Scanner;public class Inventory { public static void main (String[] args) { Scanner scnr = new Scanner(System.in); InventoryNode headNode; InventoryNode currNode; InventoryNode lastNode; String item; int numberOfItems; int i; // Front of nodes list ...
THE ENTIRE CODE SHOULD BE IN JAVA Playlist (output linked list) Given main(), complete the SongNode class to include the printSongInfo() method. Then write the Playlist class' printPlaylist() method to print all songs in the playlist. DO NOT print the dummy head node. Ex: If the input is: Stomp! 380 The Brothers Johnson The Dude 337 Quincy Jones You Don't Own Me 151 Lesley Gore -1 the output is: LIST OF SONGS ------------- Title: Stomp! Length: 380 Artist: The Brothers...
Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...
I need help and have to get this done asap: Using the following source codes provided below, create recursive functions and methods in C++ to complete the following exercises: Print the list in forward order Print the list in reverse order Print the following three lines (in this order): "The first node contains <value in first node>" "The last node contains <value in last node>" "The first node contains <value in first node>" Print the sum of all the values...
JAVA PROGRAMMING Given main(), complete the SongNode class to include the printSongInfo() method. Then write the Playlist class' printPlaylist() method to print all songs in the playlist. DO NOT print the dummy head node. Ex: If the input is: Stomp! 380 The Brothers Johnson The Dude 337 Quincy Jones You Don't Own Me 151 Lesley Gore -1 the output is: LIST OF SONGS ------------- Title: Stomp! Length: 380 Artist: The Brothers Johnson Title: The Dude Length: 337 Artist: Quincy Jones...
#include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ListNode *head; class LinkedList { public: int insertNode(float num); void deleteNode(float num); void destroyList(); void displayList(); LinkedList(void) {head = NULL;} ~LinkedList(void) {destroyList();} }; int LinkedList::insertNode(float num) { struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL; newNode = new ListNode; if(newNode == NULL) { cout << "Error allocating memory for new list member!\n"; return 1; } newNode->value = num; newNode->next = NULL; if(head==NULL) { cout << "List...