// HOMEWORK 6 : LINKED LISTS // // Complete the code for the LinkedList class that is partly given // below. The last few methods are specified in javadoc comments. // You must supply the code. // You may use the main method at the end of the class to test your // solution. In that main method a node's data is always a string. // // Remember to insert your collaboration statement below /* LinkedList is a generic singly linked list of elements of type Type. * If you leave out the type parameter, the elements will be of type * Object by default. */ public class LinkedList { // The head or first node, initially null private Node head; /** * No-argument constructor creates a linked list with no elements. */ public LinkedList() { head = null; } /** * A linked list is empty if its head node is null. * @return whether the list is empty. */ public boolean isEmpty() { return (head == null); } /** * removeFirst removes the first node of the list. If the list was * empty, it does nothing. removeFirst does not return the node. To * remove the first node and return it, first call getFirst() and then * call removeFirst() */ public void removeFirst() { if (!isEmpty()) { Node toBeRemoved = head; head = head.getNext(); // Advance the head node toBeRemoved.setNext(null); // Break the connection to the list } } /** * size calculates length of the list by traversing * @return the number of nodes in the list */ public int size() { int length = 0; Node current = head; while (current != null) { current = current.getNext(); length++; } return length; } /** * toString calculates the string representation of the list by * traversing it and giving the concatenation of the string * representation of the constituent nodes * @return the string representation of the list */ public String toString() { String rep = "Linked list:\n"; Node current = head; while(current != null) { rep += "\t" + current.toString() + "\n"; current = current.getNext(); } return rep; } ///////////////////////////////////////////////////////////////// ////////////ASSIGNMENT STARTS HERE////////////////////////// //////////////////////////////////////////////////////////////// /** * getFirst finds the data corresponding to the first item in the * list (which may be null if the list is empty.) It takes in no parameter * and returns the data contained in the first node. * @return first The data in the first node of the list * */ public Type getFirst(){ } /** * addFirst adds a node at the beginning of the list. This method * takes in data of type Type. * @param Type The data to be added */ public void addFirst(Type data){ } /** * contains checks whether this list contains the target item. This method takes * in the target data being searched for in the list, and returns whether or not * the list contains that data. * @param target the target item * @return whether the item is in the list */ public boolean contains(Type target) /* Hint: Use the condition * current.getData().equals(target) * in your loop NOT * current.getData() == target */ { } /** * set sets the n'th item in the list to a specified value. If there * is no n'th node (n is negative or the list is not long enough), * this method has no effect on the list. This method takes in the the * index of the item to change (where 0 is the head of the list), and * the new data value. * * @param n the index of the item to change, * where 0 is the index of the head of the list * @param data the new value of the data */ public void set(int n, Type data) { } /** * removeNthNode removes the node at the index specified by the parameter. * This method takes in the index of the node to be removed. If there * is no n'th node (n is negative or the list is not long enough), * this method has no effect on the list. If the input index is 0 (head of the list) * simply call removeFirst(). * @param nodeNum the index of the node to be removed * */ public void removeNthNode(int nodeNum) { } /** * addNode adds a node at a position containing some * data, both specified by the parameters. * If the input position is 0 (the head of the list), simply call * addFirst(data). If there is no n'th node (n is negative or the list is not long enough), * this method has no effect on the list. * @param position the index at which the new node is added * @param data the data to be contained in the new node * */ public void addNode(int position, Type data) { } /////////////////////////////////////////////////// // main method for testing // // You can write your own main method, or comment out some lines // when you have some but not all the methods to test. // // This main method tests the LinkedList as a linked list of // nodes that contain String data. // // Hint: The linked list actually contains nodes whose data field // is a string. //////////////////////////////////////////////////// public static void main(String[] args) { LinkedList ls = new LinkedList(); System.out.println("LinkedList is empty: " + ls.isEmpty()); System.out.println("The size is: " + ls.size()); System.out.println(ls); String s1 = "I"; String s2 = "Love"; String s3 = "CS 1316"; ls.addFirst(s3); ls.addFirst(s2); ls.addFirst(s1); System.out.println("LinkedList is empty: " + ls.isEmpty()); System.out.println("The size is: " + ls.size()); System.out.println(ls); ls.set(2, "Weekends"); ls.set(0, "We"); System.out.println("LinkedList is empty: " + ls.isEmpty()); System.out.println("The size is: " + ls.size()); System.out.println(ls); ls.addNode(1, "All"); ls.addNode(0, "Yes,"); System.out.println("LinkedList is empty: " + ls.isEmpty()); System.out.println("The size is: " + ls.size()); System.out.println(ls); ls.removeNthNode(0); ls.removeNthNode(ls.size()-1); System.out.println("LinkedList is empty: " + ls.isEmpty()); System.out.println("The size is: " + ls.size()); System.out.println(ls); System.out.println(ls.contains("CS 1316")); System.out.println("The data in the first node is: " + ls.getFirst().toString()); } }