EBK JAVA PROGRAMMING
9th Edition
ISBN: 9781337671385
Author: FARRELL
Publisher: CENGAGE LEARNING - CONSIGNMENT
expand_more
expand_more
format_list_bulleted
Question
You are required to complete the LinkedList class. This class is used as a linked list that has many methods to perform operations on the linked list. To create a linked list, create an object of this class and use the addFirst or addLast or add (must be completed) to add nodes to this linked list(I created one linked list in TestLinkedList).
I’ve already created the class for you and have some completed method in there, your job is to complete the empty methods.For every method you have to complete, you are provided with a header, Do not modify those headers(
method name, return type or parameters). You have to complete the body of the method.
public void add(T afterThis, T info)
The parameter info will be the information the new node should contain, and the new node will be added after the node that contains the parameter afterThis as an information
public void removeFirst()
Removes the first node in the linked list. Hint: be aware of empty lists.
public void removeLast()
Removes the last node in the linked list. Hint: be aware of empty lists.
public int size()
returns the size of the linked list.
public boolean isEmpty()
returns true if the linked list is empty, otherwise returns false.
public boolean contains(T item)
returns true if the list contains a node with the same information as the parameter of the method.
Test your method with TestLinkedList, complete one method at the time and uncomment the method calls in the main method.
package chapter02;
public class LinkedList
{
protected LLNode list;
public LinkedList()
{
list = null;
}
public void addFirst(T info)
{
LLNode node = new LLNode(info);
node.setLink(list);
list = node;
}
public void addLast(T info)
{
LLNode curr = list;
LLNode newNode = new LLNode(info);
if(curr == null)
{
list = newNode;
}
else
{
while(curr.getLink() != null)
{
curr = curr.getLink();
}
curr.setLink(newNode);
}
}
public void add(T afterThis, T info)
{
//TODO Complete this method as required in the homework instructions
}
public void removeFirst()
{
//TODO Complete this method as required in the homework instructions
}
public void removeLast()
{
//TODO Complete this method as required in the homework instructions
}
public int size()
{
//TODO Complete this method as required in the homework instructions
}
public boolean isEmpty()
{
//TODO Complete this method as required in the homework instructions
}
public boolean contains(T item)
{
//TODO Complete this method as required in the homework instructions
}
public void display()
{
LLNode currNode = list;
while(currNode != null)
{
System.out.println(currNode.getInfo());
currNode = currNode.getLink();
}
}
}
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution
Trending nowThis is a popular solution!
Step by stepSolved in 3 steps with 2 images
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- public class CustomLinkedList { public static int findMax(IntNode headObj) { /* Type your code here */ } public static void main(String[] args) { IntNode headObj; IntNode currObj; IntNode lastObj; int i; int max; // Create head node headObj = new IntNode(-1); lastObj = headObj; // Add nodes to the list for (i = 0; i < 20; ++i) { currObj = new IntNode(i); lastObj.insertAfter(currObj); lastObj = currObj; } max = findMax(headObj); System.out.println(max); }}arrow_forwardStarter code for ShoppingList.java import java.util.*;import java.util.LinkedList; public class ShoppingList{ public static void main(String[] args) { Scanner scnr=new Scanner(System.in); LinkedList<ListItem>shoppingList=new LinkedList<ListItem>();//declare LinkedList String item; int i=0,n=0;//declare variables item=scnr.nextLine();//get input from user while(item.equals("-1")!=true)//get inputuntil user not enter -1 { shoppingList.add(new ListItem(item));//add into shoppingList LinkedList n++;//increment n item=scnr.nextLine();//get item from user } for(i=0;i<n;i++) { shoppingList.get(i).printNodeData();//call printNodeData()for each object } }} class ListItem{ String item; //constructor ListItem(String item) { this.item=item; } void printNodeData() { System.out.println(item); }}arrow_forwardpackage hw5; public class LinkedIntSet {private static class Node {private int data;private Node next; public Node(int data, Node next) {this.data = data;this.next = next;}} private Node first; // Always points to the first node of the list.// THE LIST IS ALWAYS IN SORTED ORDER!private int size; // Always equal to the number of elements in the set. /*** Construts an empty set.*/public LinkedIntSet() {throw new RuntimeException("Not implemented");} /*** Returns the number of elements in the set.* * @return the number of elements in the set.*/public int size() {throw new RuntimeException("Not implemented");} /*** Tests if the set contains a number* * @param i the number to check* @return <code>true</code> if the number is in the set and <code>false</code>* otherwise.*/public boolean contains(int i) {throw new RuntimeException("Not implemented");} /*** Adds <code>element</code> to this set if it is not already present and* returns…arrow_forward
- Please fill in all the code gaps if possible: (java) public class LinkedList { privateLinkedListNode head; **Creates a new instance of LinkedList** public LinkedList () { } public LinkedListNode getHead() public void setHead (LinkedListNode head) **Add item at the front of the linked list** public void insertFront (Object item) **Remove item at the front of linked list and return the object variable** public Object removeFront() }arrow_forwardpublic LLNode secondHalf(LLNode head) { }arrow_forwardpackage DataStructures; import ADTs.StackADT;import Exceptions.EmptyCollectionException; /**** @author Qiong*/public class LinkedStack<T> implements StackADT<T> { int count;SinglyLinkedNode<T> top;public LinkedStack(){top = null;count = 0;}public LinkedStack(T data){top = new SinglyLinkedNode(data);count = 1;}@Overridepublic void push(T element) {// TODO implete the push method// The push method will insert a node with holds the given input into the top of the stack } @Overridepublic T pop() throws EmptyCollectionException {if (this.isEmpty()) throw new EmptyCollectionException();SinglyLinkedNode<T> node = top;top = top.getNext();count--;node.setNext(null);return node.getElement();} @Overridepublic T peek() throws EmptyCollectionException {//TODO: Implement this method//This should look like pop, except we aren�t changing the stack at all. Just returning the element. } @Overridepublic boolean isEmpty() {if (count == 0) {return true;}return false;}…arrow_forward
- Given the interface of the Linked-List struct Node{ int data; Node* next = nullptr; }; class LinkedList{ private: Node* head; Node* tail; public: display_at(int pos) const; ... }; Write a definition for a method display_at. The method takes as a parameter integer that indicates the node's position which data you need to display. Example: list: 5 -> 8 -> 3 -> 10 display_at(1); // will display 5 display_at(4); // will display 10 void LinkedList::display_at(int pos) const{ // your code will go here }arrow_forwardJava language the top half is the class that contains the linked list parts the bottom half is the method i need help setting up should take two parameters "index" and "element" should be an actual method not just LinkedList.set(index, element); please and thank you!arrow_forwardimport java.io.*;import java.util.stream.*; public class Solution { static class ListCell<T> { public T datum; // Data for this cellpublic ListCell<T> next; // Next cell public ListCell(T datum, ListCell<T> next) {this.datum = datum;this.next = next;}} static class LinkedList<T> { private static final String STRING = " "; Solution.ListCell<T> head; // head (first cell) of the List public LinkedList() {head = null;} public void insert(T element) {head = new ListCell<T>(element, head);} public void delete(T element) {delete(element, head);} private ListCell<T> delete(T element, ListCell<T> cell) {if (cell == null)return null;if (cell.datum.equals(element))return cell.next;cell.next = delete(element, cell.next);return cell;} public int size() {return size(head);} private int size(ListCell<T> cell) {if (cell == null)return 0;return size(cell.next) + 1;} public String toString() {return toString(head);} private String…arrow_forward
- Given the IntNode class, define the getCount() method in the CustomLinkedList class that returns the number of items in the list not including the head node. Ex: If the list contains: head -> 14 -> 19 -> 4 getCount(headObj) returns 3. Ex: If the list contains: head -> getCount(headObj) returns 0. public class IntNode { private int dataVal; // Node data private IntNode nextNodePtr; // Reference to the next node // Default constructor public IntNode() { dataVal = 0; nextNodePtr = null; } // Constructor public IntNode(int dataInit) { this.dataVal = dataInit; this.nextNodePtr = null; } // Constructor public IntNode(int dataInit, IntNode nextLoc) { this.dataVal = dataInit; this.nextNodePtr = nextLoc; } /* Insert node after this node. Before: this -- next After: this -- node -- next */ public void insertAfter(IntNode nodeLoc) { IntNode tmpNext; tmpNext = this.nextNodePtr;…arrow_forwardvoid listEmployees (void) { for (int i=0; i 10000. Make a guess about why the comparison function takes 2 struct Employee parameters (as opposed to struct Employee *) **arrow_forwardPYTHON LAB: Inserting an integer in descending order (doubly-linked list) Given main.py and an IntNode class, complete the IntList class (a linked list of IntNodes) by writing the insert_in_descending_order() method to insert new IntNodes into the IntList in descending order. Ex. If the input is: 3 4 2 5 1 6 7 9 8 the output is: 9 8 7 6 5 4 3 2 1 _____________________________________________________________________________________ Main.py: from IntNode import IntNode from IntList import IntList if __name__ == "__main__": int_list = IntList() input_line = input() input_strings = input_line.split(' ') for num_string in input_strings: # Convert from string to integer num = int(num_string) # Insert into linked list in descending order new_node = IntNode(num) int_list.insert_in_descending_order(new_node) int_list.print_int_list() IntNode.py class IntNode: def __init__(self, initial_data, next = None,…arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT