Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need this lab for computer science done please! I've included all the java files below :) List.java: /** List ADT */ public interface List {

Need this lab for computer science done please! I've included all the java files below :)

image text in transcribedimage text in transcribed

List.java:

/** List ADT */ public interface List { /** Remove all contents from the list, so it is once again empty. */ public void clear(); /** Insert an element at the given location. * allows you to insert after the tail * @param item The element to be inserted. */ public void insert(int index, E item); /** Append an element at the end of the list. * @param item The element to be appended. */ public void add(E item); /** * Remove the element at the given location. */ public void remove(int index); /** * Get the element in the position to one step left. * @return element in the node to the left of the node at the index, * null if at the head. */ public E prev(int index); /** Get the element in the position one step right. * @return the element in the node to the right of * the node at the index, null if at the end. */ public E next(int index); /** @return The number of elements in the list. */ public int length(); /** Turn the contents of the Nodes to a string in order from head to end. * @return The String representation of the * elements in the list from head to end. */ public String toString(); /** Reverse the content of the list. * if list is A => B => C it becomes C => B => A */ public void reverse(); /** @return The element at given position. */ public E getValue(int index); }

Node.java

 /** * Node class for Linked List */ public class Node { private E element; // Value for this node private Node next; // reference to next node in list /** Constructor * @param item the element to be stored in Node * @param nextVal the next Node that this is pointing to */ public Node(E item, Node nextval) { element = item; next = nextval; } /** Constructor * @param item the element to be stored in Node */ public Node(E item){ element = item; next = null; } //other constructors public Node(){ element = null; next = null; } public Node(Node nextval) { next = nextval; } /** *@return the Node that is next to this */ public Node getNext() { return next; } /** * Sets this next to the given Node * @param nextNal the Node that is to be set to this Node's next */ public void setNext(Node nextval){ next = nextval; } /** * returns the element in the Node *@return element in the Node */ public E getElement() { return element; } /** * sets the element stored in Node to the element given *@param item the element to be stored in Node. */ public E setElement(E item) { return element = item; } }

MakeItASingle.java

import java.util.*; import java.lang.*; /**This is an ugly driver that is just checking methods*/ public class MakeItASingle{ public static void main(String[] args){ System.out.println("Single Linked"); playWithSingleString(); System.out.println("-----------------------------------"); } public static void playWithSingleString(){ SLList StringList = new SLList(); System.out.println("Empty list:"); System.out.println(StringList.toString()); StringList.add("A"); System.out.println("Added one thing"); System.out.println(StringList.toString()); System.out.println("The head of the list has: "+ StringList.getHead().getElement()); System.out.println("The tail of the list has: "+ StringList.getLast().getElement()); StringList.add("C"); System.out.println("Added another thing"); System.out.println(StringList.toString()); System.out.println("The head of the list has: "+ StringList.getHead().getElement()); System.out.println("The tail of the list has: "+ StringList.getLast().getElement()); StringList.insert(1, "B"); System.out.println("inserting something in index 1"); System.out.println(StringList.toString()); System.out.println("The head of the list has: "+ StringList.getHead().getElement()); System.out.println("The tail of the list has: "+ StringList.getLast().getElement()); StringList.insert(0, "This is the alphabet"); System.out.println("inserting something in index 0 - so a new head"); System.out.println(StringList.toString()); System.out.println("inserting something at the end - so a new tail"); StringList.insert(StringList.length(),"this is the end"); System.out.println(StringList.toString()); String whatIsThere = StringList.getValue(1); System.out.println("What is in index 1: " + whatIsThere); String whatCameBefore = StringList.prev(1); System.out.println("What came before index 1: "+ whatCameBefore); String whatCameAfter = StringList.next(1); System.out.println("What came after index 1: "+ whatCameAfter); System.out.println("The length of your list is: "+ StringList.length()); StringList.reverse(); System.out.println("reversed: "+StringList.toString()); System.out.println("the length of the list is: "+ StringList.length()); SLList StringList2 = new SLList(); StringList2.add("*"); StringList2.add("*"); System.out.println("The list to insert looks like: "+StringList2.toString()); System.out.println("the length of the inserted list is: "+ StringList2.length()); StringList.insertList(StringList2, 1); System.out.println("inserted another list at after index 1"); System.out.println(StringList.toString()); System.out.println("the length of the list is: "+ StringList.length()); System.out.println("The head of the list has: "+ StringList.getHead().getElement()); System.out.println("The tail of the list has: "+ StringList.getLast().getElement()); SLList StringList3 = new SLList(); System.out.println("the length of the inserted list is: "+ StringList3.length()); StringList.insertList(StringList3, 1); System.out.println("List after trying to insert an empty list: "+StringList.toString()); System.out.println("Removing the thing at index 3"); StringList.remove(3); System.out.println(StringList.toString()); StringList3.add("Q"); StringList3.add("U"); System.out.println("The list to insert looks like: "+StringList3.toString()); StringList.insertList(StringList3, StringList.length()-1); System.out.println("Adding a list to the end"); System.out.println(StringList.toString()); System.out.println("The head of the list has: "+ StringList.getHead().getElement()); System.out.println("The tail of the list has: "+ StringList.getLast().getElement()); StringList.clear(); System.out.println("clear the list and print"); System.out.println(StringList.toString()); System.out.println("The length of your list is: "+ StringList.length()); StringList.add("Z"); StringList.add("X"); System.out.println("Added two things"); System.out.println(StringList.toString()); } } 
Implement a List Interface for a Single-Linked List. Download List.java, Node.java, and Make ItSingle.java Open the file List.java. This file contains an interface that describes the List ADT that you will implement. You will need to use this interface to implement a single-linked list. So you will need to implement the following class: public class SLList implements List{ //your implementation } You will need to call your class file this: SLList.java A corresponding Node class has been given to you, Node.java. Additional methods that you will need to write on your own: SLList.java: insert a list after given index @param list the list to be inserted @param index the index after which the list should be inserted within this list */ public void insertList (SLList list, int index) /** @return the head of the list*/ public Node getHead(); /** @return the last node in the list*/ public Node getLast(); I have created a file that you can use to check your work. It is called Make ItSingle.java. When it runs you will see: Single Linked Empty list: Added one thing A The head of the list has: A The tail of the list has: A Added another thing A ==> The head of the list has: A The tail of the list has: C inserting something in index i A ==> B ==> C The hand The head of the list has: A The tail of the list has: C inserting something in index 0 - so a new head This is the alphabet ==> A ==> B ==> C inserting something at the end - so a new tail This is the alphabet ==> A ==> B ==> C ==> this is the end What is in index 1: A What came before index 1: This is the alphabet What came after index 1: B The length of your list is: 5 reversed: this is the end ==> C ==> B ==> A ==> This is the alphabet the length of the list is: 5 The list to insert looks like: * ==>* the length of the inserted list is: 2 ==> ==> B ==> A inserted another list at after index 1 this is the end ==> C ==> ==> ==> B ==> A ==> This is the alphabet the length of the list is: 7 The head of the list has: this is the end The tail of the list has: This is the alphabet the length of the inserted list is: 0 List after trying to insert an empty list: this is the end ==> C ==> ==> This is the alphabet Removing the thing at index 3 this is the end ==> C ==> * ==> B ==> A ==> This is the alphabet The list to insert looks like: ==> U Adding a list to the end this is the end ==> C ==> * ==> B ==> A ==> This is the alphabet ==> The head of the list has: this is the end The tail of the list has: U clear the list and print ==> U The length of your list is: 0 Added two things Z ==> X

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David Kroenke, David Auer, Scott Vandenberg, Robert Yoder

8th Edition

013460153X, 978-0134601533

More Books

Students also viewed these Databases questions