Question

In: Computer Science

I need to create a linked list that contains a fixed arraylist. Each new entry is...

I need to create a linked list that contains a fixed arraylist. Each new entry is added to array list. If the arraylist is full, create a new arraylist and add to the linklist. In java please.

Solutions

Expert Solution

Note:

here the arraylist size is considered as 3. You can set any limit you wanted. Following code may be difficult to understand but after seeing it 7 to 8 times and reading comments ,you can understand it easily

Code


import java.util.*;
public class LinkedList
 {
   Node head;
   static class Node
   {
     int a=0; 
   public int get()
   {
     return a;
   }
   public void set()
   {
     a=a+1;
   }
   public void reset()
   {
     a=0;
   }
     ArrayList <Integer> al = new ArrayList<Integer>(3);
     Node next;
     Node(int d)
     {
       al.add(d);
       next=null;
     }
     void add(int d)
     {
       al.add(d);
     }
   }
 public static LinkedList insert(LinkedList list,int data)
   {
     if(list.head==null) //if head is null  create head
     {
       Node node= new Node(data);
       node.set(); //arraylist size is 1
       node.next=null;
       list.head=node;
     }
     else
     {
       Node last=list.head;
       while(last.next!=null)
       {
         last=last.next; //reach the last node
       }
       if(last.get()==3) //if areaylist size reached the limit create new node
       {
         Node node=new Node(data);
         node.reset(); //set reset get all of them uses to check limit of list size
         node.set();
         node.next=null;
         last.next=node;
       }
       else//if arraylist limit not reached add elements in the same node
       {
         last.add(data);
         last.set(); //increase arraylist size after insert elements in linkedlist
       }
     }
     return list;
   }
   public static void print(LinkedList list)
   {
     Node cur=list.head;
     while(cur!=null)
     {
       System.out.print(cur.al+" ");
       cur=cur.next;
     }
   }
  public static void main(String[] args) 
  {
        LinkedList l=new LinkedList();
        l = insert(l, 1); 
        l = insert(l, 2); 
        l = insert(l, 3); 
        l = insert(l, 4); 
        l = insert(l, 5); 
        l = insert(l, 6);
        l = insert(l, 6); 
        l = insert(l, 7); 
        l = insert(l, 8);
        print(l);
  }

}

​​​​Terminal Work

.


Related Solutions

I need to create Create a new empty ArrayList, Ask the user for 5 items to...
I need to create Create a new empty ArrayList, Ask the user for 5 items to add to a shopping list and add them to the ArrayList (get from user via the keyboard). Prompt the user for an item to search for in the list. Output a message to the user letting them know whether the item exists in the shopping list. Use a method that is part of the ArrayList class to do the search. Prompt the user for...
I need this written in Java, it is a Linked List and each of it's Methods....
I need this written in Java, it is a Linked List and each of it's Methods. I am having trouble and would appreciate code written to specifications and shown how to check if each method is working with an example of one method being checked. Thank you. public interface Sequence <T> { /** * Inserts the given element at the specified index position within the sequence. The element currently at that * index position (and all subsequent elements) are shifted...
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...
public List<Item> findItems(Lookup query) { ArrayList<Item> matches = new ArrayList<Item>(); for (int i = 0 ;...
public List<Item> findItems(Lookup query) { ArrayList<Item> matches = new ArrayList<Item>(); for (int i = 0 ; i < numItems ; i++ ) { Item item = items[i]; if (query.matches(item)) { matches.add(item); } } return matches; } "To improve readability, each line of code should be indented under its parent (i.e. methods should be indented under the class, code in methods should be indented under the method, code in if statements should be indented under the if, etc.). This if rcurly...
In python I have a linked list. I want to create one function that takes in...
In python I have a linked list. I want to create one function that takes in one parameter, head. In the function, cur = head and next_cur = head.next. I want to return head and next_cur, except at the end of the function they will return alternating values from head. For example, if the these are the values in the linked list: 2, 3, 5, 7, 11 after the function head should return: 2, 5, 11 and next_cur should return:...
(Java) Create a new linked list from two given arrays with the greater element from each...
(Java) Create a new linked list from two given arrays with the greater element from each corresponding array element placed into the linked list. Given two arrays of varying size initialized with integers of varying values, the task is to create a new linked list using those arrays. The condition is that the greater element value from each corresponding array element will be added to the new linked list in the list position that maintains the integers in ascending order....
In python I want to create a function that takes in a linked list. Using recursion...
In python I want to create a function that takes in a linked list. Using recursion only, I want to check if the linked list is sorted. How do i do this?
I have the function call showGroceries(list); I need to create a new prototype above int main...
I have the function call showGroceries(list); I need to create a new prototype above int main that provides: return type function name parameter(s) type(s) Then copy-and-paste the prototype below int main. Give the parameter a name and then implement the function so that it takes the vector of strings and displays it so that a vector with the values: {"milk", "bread", "corn"} would display as: Grocery list 1. milk 2. bread 3. corn However, if there is nothing in the...
I need a MIPS Assembly program that "Display the elements of the linked list in reverse...
I need a MIPS Assembly program that "Display the elements of the linked list in reverse order." It needs subprogram and those subprogram does not have t registers.
I need an example of how to swap and index within a doubly linked list with...
I need an example of how to swap and index within a doubly linked list with index + 1. This is written in java. 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   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT