Question

In: Computer Science

In JAVA, students will create a linked list structure that will be used to support a...

In JAVA, students will create a linked list structure that will be used to support a role playing game. The linked list will represent the main character inventory. The setting is a main character archeologist that is traveling around the jungle in search of an ancient tomb. The user can add items in inventory by priority as they travel around (pickup, buy, find), drop items when their bag is full, and use items (eat, spend, use), view their inventory as a whole or by category, request the number of items in inventory, and request to see values for specific inventory items. The categories for items are food, coins, archeology tools, artifacts, and documents. Each item should have an item number (unique integer value in priority order), category (string or integer), a description (string), and a value (integer). The program will use the linked list structure to allow the user to do the functions mentioned above; add (beginning/end), remove (from end), insert (based on ID number), delete (based on ID number), print inventory, print inventory by category, tell if the inventory is empty, tell how many items are in inventory, tell how many items in each category, and search for an item by name or ID and return its value. The code must validate the user input for valid choices and valid input type and throw exceptions that result in an appropriate message to the user. The code must also throw exceptions for trying to retrieve/print a node from an empty list. Please help ASAP

Solutions

Expert Solution

Like arrays, Linked List is a linear data structure. Unlike arrays, linked list elements are not stored at the contiguous location, the elements are linked using pointers as shown below.

In Java, LinkedList can be represented as a class and a Node as a separate class. The LinkedList class contains a reference of Node class type.

filter_none

brightness_4

class LinkedList {

    Node head; // head of list

  

    /* Linked list Node*/

    class Node {

        int data;

        Node next;

  

        // Constructor to create a new node

        // Next is by default initialized

        // as null

        Node(int d) { data = d; }

    }

}

Creation and Insertion

In this article, insertion in the list is done at the end, that is the new node is added after the last node of the given Linked List. For example, if the given Linked List is 5->10->15->20->25 and 30 is to be inserted, then the Linked List becomes 5->10->15->20->25->30.

Since a Linked List is typically represented by the head pointer of it, it is required to traverse the list till the last node and then change the next of last node to new node.

import java.io.*;

  

// Java program to implement

// a Singly Linked List

public class LinkedList {

  

    Node head; // head of list

  

    // Linked list Node.

    // This inner class is made static

    // so that main() can access it

    static class Node {

  

        int data;

        Node next;

  

        // Constructor

        Node(int d)

        {

            data = d;

            next = null;

        }

    }

  

    // Method to insert a new node

public static LinkedList insert(LinkedList list, int data)

    {

        // Create a new node with given data

        Node new_node = new Node(data);

        new_node.next = null;

  

        // If the Linked List is empty,

        // then make the new node as head

        if (list.head == null) {

            list.head = new_node;

        }

        else {

            // Else traverse till the last node

            // and insert the new_node there

            Node last = list.head;

            while (last.next != null) {

                last = last.next;

            }

  

            // Insert the new_node at last node

            last.next = new_node;

        }

  

        // Return the list by head

        return list;

}

  1. Search the key for its first occurrence in the list
  2. Now, Any of the 3 conditions can be there:
    • Case 1: The key is found at head
      1. In this case, Change the head of the node to the next node of current head.
      2. Free the memory of replaced head node.
    • Case 2: The key is found at in the middle or last, except at head
      1. In this case, Find previous node of the node to be deleted.
      2. Change the next of previous node to the next node of current node.
      3. Free the memory of replaced node.
    • Case 3: The key is not found in the list
      1. In this case, No operation needs to be done.


Related Solutions

Create a generic Linked List that does NOT use the Java library linked list. Make sure...
Create a generic Linked List that does NOT use the Java library linked list. Make sure it contains or access a subclass named Node (also Generic). And has the methods: addFirst(), addLast(), add(), removeFirst(), removeLast() and getHead(). In a separate Java class provide a main that creates an instance of your LinkedList class that creates an instance of your LinkedList that contains String types. Add the five names (you pick them) to the list and then iterate through the list...
Introduction: In this project you will create a generic linked list using Java Generics. Description: Create...
Introduction: In this project you will create a generic linked list using Java Generics. Description: Create a generic class called GenLinkedList. GenLinkedList will use nodes that store a value of the generic type to store its contents. It should have the following methods. The methods should all operate on the object making the call (none are static). Perform checking of the parameters and throw exceptions where appropriate. The linked list should be singly-linked. It should not use sentinel nodes (empty...
**JAVA** Create a Linked List and conduct the following operations. Portion of the program is given....
**JAVA** Create a Linked List and conduct the following operations. Portion of the program is given. The operations are: Add an “H” to the list Add an “I” to the list Add “100” to the list Print the content of the list and its size Add a “H” to the first place of the list Add a “R” to the last place of the list Get the element of position 3 and print it Get the last element and print...
Given an array of Student type and size 10, create a linked list of students by...
Given an array of Student type and size 10, create a linked list of students by linking students with an odd index first and then linking students with an even index. Write a loop to print out the students in the linked list #include<iostream> #include<string> #include<fstream> using namespace std; const int NUM = 10; struct Student{ string fName; string lName; Student * next; }; int main() {        Student stuArr[NUM];        ifstream myfile;        myfile.open("Test.txt");        for(int i = 0;...
Given an array of Student type and size 10, create a linked list of students by...
Given an array of Student type and size 10, create a linked list of students by linking students with an odd index first and then linking students with an even index. Write a loop to print out the students in the linked list. #include #include #include using namespace std; const int NUM = 10; struct Student{ string fName; string lName; Student * next; }; int main() { Student stuArr[NUM]; ifstream myfile; myfile.open("Test.txt"); for(int i = 0; i < NUM; i++)...
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...
Java Generic 2D Linked List Problem How to convert a 1D linked List into multiple linked...
Java Generic 2D Linked List Problem How to convert a 1D linked List into multiple linked lists with sequential values together? //Example 1: [1,1,2,3,3] becomes [[1,1],[2],[3,3]] //Example 1: [1,1,2,1,1,2,2,2,2] becomes [[1,1],[2],[1,1],[2,2,2,2]] //Example 3: [1,2,3,4,5] becomes [[1],[2],[3],[4],[5]] public <T> List<List<T>> convert2D(List<T> list) { // Given a 1D, need to combine sequential values together. }
Create a program that implements a singly linked list of Students. Each node must contain the...
Create a program that implements a singly linked list of Students. Each node must contain the following variables: Student_Name Student_ID In main(): Create the following list using addHead(). The list must be in the order shown below. Student_ID Student_Name 00235 Mohammad 00662 Ahmed 00999 Ali 00171 Fahad Print the complete list using toString() method. Create another list using AddTail(). The list must be in the order shown below. Student_ID Student_Name 00236 Salman 00663 Suliman 00998 Abdulrahman Print the complete list...
Create a program that implements a singly linked list of Students. Each node must contain the...
Create a program that implements a singly linked list of Students. Each node must contain the following variables: Student_Name Student_ID In main(): Create the following list using addHead(). The list must be in the order shown below. Student_ID Student_Name 00235 Mohammad 00662 Ahmed 00999 Ali 00171 Fahad Print the complete list using toString() method. Create another list using AddTail(). The list must be in the order shown below. Print the complete list using toString() method. Delete head note from both...
Java Data Structure Doubly Linked List /* * Complete the swap(int index) method * No other...
Java Data Structure Doubly Linked List /* * Complete the swap(int index) method * No other methods/variables should be added/modified */ public class A3DoubleLL<E> {    /*    * Grading:    * Swapped nodes without modifying values - 2pt    * Works for all special cases - 1pt    */    public void swap(int index) {        //swap the nodes at index and index+1        //change the next/prev connections, do not modify the values        //do not...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT