Question

LinkedListBox Patterned after the ArrayBox assignment, please create your own implementation of LinkedList called LinkedListBox as...

LinkedListBox

Patterned after the ArrayBox assignment, please create your own implementation of LinkedList called LinkedListBox as explained in class. Refer to the class slides on linked lists. Specifically, implement your generic classes as:

public class LinkedListNode
{
public E element;
public LinkedListNode next;
}
public class LinkedListBox
{
public LinkedListNode head;
public LinkedListNode tail;
public int count = 0;
}

Within the LinkedListBox class, implement the following methods:

public boolean add(E e). Returns true after adding an element to the end of the list.

public E get(int idx). Returns element at index position idx.

public E set(int idx, E e). Changes the element at position idx to e. Returns old element.

public E remove(int idx). Removes element at position idx from the list and returns it.

public int size(). Self-explanatory

public boolean isEmpty(). Self-explanatory

public Integer find(E e). Search for element e in the linked list and return its index position or return null if not found.

public E removeFirst(). Remove and return the element at the head of the list.

public E removeLast(). Remove and return the element at the tail of the list.

public void addFirst(E e). Add an element to the front of the list.

public void insert(int idx, E e). Insert an element right after the index idx position.

Create a driver class called LinkedListBoxDriver. Provide the user a menu:

=[==[==[=[=================================]=]==]==]=
=[==[==[=[ Welcome to LinkBox ]=]==]==]=
=[==[==[=[=================================]=]==]==]=

Choose...

1. Add an element(String) to our box.
2. Remove an element(String) from our box.
3. Replace(set) an element(String) from our box.
4. Insert an element(String) after index.
5. List the contents of the box.
6. Exit program

please don't answer the same solution what already exist in chegg. Thanks

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;
class LinkedListNode<E>{
   public E element;
   public LinkedListNode<E> next;
   LinkedListNode(E e)
{
element = e;
next = null;
}
LinkedListNode(E data, LinkedListNode<E> next) {
this.element = data;
this.next = next;
}
}

class LinkedListBox<E>{
   public LinkedListNode<E> head;
   public LinkedListNode<E> tail;
   public int count=0;

   public boolean add(E e){
       LinkedListNode<E> node = new LinkedListNode<E>(e);
       node.next=null;
       if(head==null)
           head=node;
       else{
           tail = head;
           while(tail.next != null)
               tail = tail.next;
           tail.next=node;
       }
       count++;
       return true;
   }

   public E get(int idx){
       int i=0;
       LinkedListNode<E> node = head;      
       while(node.next!=null){
           if(i==idx){
               return node.element;
           }
           i++;
           node = node.next;
       }
       return node.element;
   }

   private LinkedListNode<E> goTo(int index) {
LinkedListNode<E> current = head;
for (int i = 0; i < index; i++) {
current = current.next;
}
return current;
    }

   public E set(int index, E value) {
       LinkedListNode<E> current = goTo(index);
       E e = current.element;
       current.element = value;
       return e;
    }

   public E remove(int idx){
       E ele = head.element;
       if (idx == 0) {
           head = head.next;
       } else {
           LinkedListNode<E> current = head;
           for(int i = 0; i < idx - 1; i++) {
               current = current.next;
           }
           ele = current.next.element;
           current.next = current.next.next;
       }
       count--;
       return ele;
   }

   public int size(){
       return count;
   }

   public boolean isEmpty(){
       if(head==null)
           return true;
       return false;
   }

   public int find(E e){
       LinkedListNode<E> node = head;
       int i=0;
       while(node.next != null){
           if(node.element==e){
               return i;
           }
           i++;
           node = node.next;
       }
       return -1;
   }

   public E removeFirst(){
       E ele = head.element;
       head = head.next;
       count--;
       return ele;
   }

   public E removeLast(){
       E ele = head.element;
       tail=head;
       while(tail.next.next!=null)
           tail = tail.next;
       ele=tail.element;
       tail.next=null;
       count--;
       return ele;
   }

   public void addFirst(E e){
       LinkedListNode<E> node=null;
       node.element=e;
       node.next=head;
       head=node;
       count++;
   }

   public void insert(int index, E value){
       if (index == 0) {
head = new LinkedListNode<E>(value,head);
} else {
LinkedListNode<E> current = head;
for(int i = 0; i < index - 1; i++) {
current = current.next;
}
current.next = new LinkedListNode<E>(value, current.next);
}
       count++;
   }

   public void print(){
       LinkedListNode<E> current = head;
       while(current.next != null){
           System.out.print(current.element+"->");
           current=current.next;
       }
       System.out.println(current.element);
   }

}

class LinkedListBoxDriver{
   public static void main(String[] args) {
       LinkedListBox lst = new LinkedListBox();
       System.out.println("Welcome to LinkBox");
       String s;
       int idx;
       do{
           System.out.println("Choose");
           System.out.println("1. Add an element(String) to our box");
           System.out.println("2. Remove an element(String) from our box");
           System.out.println("3..Replace(set) an element(String) from our box");
           System.out.println("4. Insert an element(String) after index");
           System.out.println("5. List the contents of the box");
           System.out.println("6. Exit");
           int ch;
           Scanner sc = new Scanner(System.in);
           ch = sc.nextInt();
           switch(ch){
               case 1:
                   System.out.println("Enter String");
                   s = sc.next();
                   System.out.println(lst.add(s));
                   break;
               case 2:
                   System.out.println("Enter index");
                   idx = sc.nextInt();
                   System.out.println(lst.remove(idx));
                   break;
               case 3:              
                   System.out.println("Enter index");
                   idx = sc.nextInt();
                   System.out.println("Enter String");
                   s = sc.next();
                   System.out.println(lst.set(idx,s));
                   break;
               case 4:                  
                   System.out.println("Enter index");
                   idx = sc.nextInt();
                   System.out.println("Enter String");
                   s = sc.next();
                   lst.insert(idx,s);
                   break;
               case 5:
                   lst.print();
                   break;
               case 6:
                   System.exit(0);
               default: System.out.println("Invalid Input");
           }
       }while(true);
   }
}

Add a comment
Know the answer?
Add Answer to:
LinkedListBox Patterned after the ArrayBox assignment, please create your own implementation of LinkedList called LinkedListBox as...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a complete LinkedList class which implements all of the methods listed below using dynamic memory...

    Create a complete LinkedList class which implements all of the methods listed below using dynamic memory allocation. You will need a Node class to represent each node in the list. The list should store String values. Include a main method which tests all the methods of your list. Also answer these questions: What is the time complexity (using big-O notation) of these operations in your linked list? add(String val) add(int index, String val) get(int index) remove(String val) Submit three files:...

  • Plz help me with the code. And here are the requirement. Thanks!! You are required to...

    Plz help me with the code. And here are the requirement. Thanks!! You are required to design and implement a circular list using provided class and interface. Please filling the blank in CircularList.java. This circular list has a tail node which points to the end of the list and a number indicating how many elements in the list. And fill out the blank of the code below. public class CircularList<T> implements ListInterface<T> { protected CLNode<T> tail; // tail node that...

  • Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to...

    Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to both classes: public void reverseThisList(), This method will reverse the lists. When testing the method: print out the original list, call the new method, then print out the list again ------------------------------------------------------------------------- //ARRAY LIST class: public class ArrayList<E> implements List<E> { /** Array of elements in this List. */ private E[] data; /** Number of elements currently in this List. */ private int size; /**...

  • JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has...

    JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has you display a pessimistic poem from a list of phrases. Next, this program has you reverse the phrases to find another more optimistic poem. Use the following algorithm. 1.   You are given a list of phrases each ending with a pound sign: ‘#’. 2.   Create a single String object from this list. 3.   Then, split the String of phrases into an array of phrases...

  • Currently working on a Java Assignment. I have written most codes for swap, reverse and insert....

    Currently working on a Java Assignment. I have written most codes for swap, reverse and insert. Just need a. itemCount receives a value and returns a count of the number of times this item is found in the list. c. sublist receives two indexes and returns an ArrayList of node values from the first index to the second index, provided the indexes are valid. d. select receives a variable number of indexes, and returns an ArrayList of node values corresponding...

  • JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public...

    JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public E previous() { // Returns the previous Element return null; } Explanation: We have this class with these two implemented inferfaces: The interfaces are: package edu.ics211.h04; /** * Interface for a List211. * * @author Cam Moore * @param the generic type of the Lists. */ public interface IList211 { /** * Gets the item at the given index. * @param index the index....

  • //LinkedList import java.util.Scanner; public class PoD {    public static void main( String [] args )...

    //LinkedList import java.util.Scanner; public class PoD {    public static void main( String [] args ) { Scanner in = new Scanner( System.in ); LinkedList teamList = new LinkedList(); final int TEAM_SIZE = Integer.valueOf(in.nextLine()); for (int i=0; i<TEAM_SIZE; i++) { String newTeamMember = in.nextLine(); teamList.append(newTeamMember); } while (in.hasNext()) { String removeMember = in.nextLine(); teamList.remove(removeMember); }    System.out.println("FINAL TEAM:"); System.out.println(teamList); in.close(); System.out.print("END OF OUTPUT"); } } =========================================================================================== //PoD import java.util.NoSuchElementException; /** * A listnked list is a sequence of nodes with...

  • Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...

    Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...

  • NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT...

    NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT WOULD CAUSE THE HW1.java TO PRINT THE RIGHT DATA.!!! The LinkedList class implements both the List interface and the Stack interface, but several methods (listed below) are missing bodies. Write the code so it works correctly. You should submit one file, LinkedList.java. Do not change the interfaces. Do not change the public method headers. Do not rename the LinkedList class. None of your methods...

  • Complete an Array-Based implementation of the ADT List including a main method and show that the...

    Complete an Array-Based implementation of the ADT List including a main method and show that the ADT List works. Draw a class diagram of the ADT List __________________________________________ public interface IntegerListInterface{ public boolean isEmpty(); //Determines whether a list is empty. //Precondition: None. //Postcondition: Returns true if the list is empty, //otherwise returns false. //Throws: None. public int size(); // Determines the length of a list. // Precondition: None. // Postcondition: Returns the number of items in this IntegerList. //Throws: None....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT