Question
Get this JAVA code to compile so that it satisfies the following guidelines: GUIDELINES: CODE: /////////////////////// public class LinkList { public static void main(String[] args)
Get this JAVA code to compile so that it satisfies the following guidelines:
GUIDELINES:
CODE:
///////////////////////
public class LinkList {
public static void main(String[] args) { linkedList l = new linkedList(); l.insertAtEnd(5); l.insertAtEnd(4); l.insertAtEnd(3); System.out.println(l.size); l.set(2,8); l.display(); } }
class Node {
private int data; private Node link;
public Node() { link = null; data = 0; }
public Node(int d, Node n) { data = d; link = n; }
public void setLink(Node n) { link = n; }
public void setData(int d) { data = d; }
public Node getLink() { return link; }
public int getData() { return data; } }
class linkedList {
private Node start; private Node end; public int size;
public linkedList() { start = null; end = null; size = 0; }
public boolean isEmpty() { return start == null; }
public int getSize() { return size; }
public void insertAtStart(int val) { Node nptr = new Node(val, null); size++; if (start == null) { start = nptr; end = start; } else { nptr.setLink(start); start = nptr; } }
public void insertAtEnd(int val) { Node nptr = new Node(val, null); size++; if (start == null) { start = nptr; end = start; } else { end.setLink(nptr); end = nptr; } }
public void insertAtPos(int val, int pos) { Node nptr = new Node(val, null); Node ptr = start; pos = pos - 1; for (int i = 1; i
ptr = ptr.getLink(); } size++; }
public void deleteAtPos(int pos) { if (pos == 1) { start = start.getLink(); size--; return; } if (pos == size) { Node s = start; Node t = start; while (s != end) { t = s; s = s.getLink(); } end = t; end.setLink(null); size--; return; }
Node ptr = start; pos = pos - 1; for (int i = 1; i
ptr = ptr.getLink(); } size--; }
public void display() { System.out.println("Size: " + this.size); System.out.println("Elements: "); Node temp = start; while (temp != null) { System.out.println("\tNode:" + temp.getData()); temp = temp.getLink(); } }
public void removeFirst() { if (start == null) { return; } else { Node temp = start.getLink(); start.setLink(null); temp = start; while (temp != null) { k++; if (k == n) { temp.setData(data); return; } temp = temp.getLink(); } } } }
Update/Modify the LinkList class so that it supports the following operations: void removeFirst- remove the first node of the list. If the list is empty, do nothing. Set the next field of the removed node to null. int size ( ) -return the number of elements (nodes) in the list toString ( ) Size: . Elements: -implement so that the printout should be like the following Node: Node: String getFirst - return the first Node. Return null if empty. Do not remove the node. void addFirst (String data)- Add a node at the beginning of the list with the data passed false. valid (e.g., size is less than n or if n is negative). n is negative or n is greater or equal to size) boolean contains (String target) - return true if target is found, otherwise return . void insertNode (int n, String data) - insert data at nth position. No effect if n is not . void removeNode (int n) - remove the node at nth position. No effect if n is not valid (e.g., set (int n, String data) - set nth node with the new value Provide main() method in the LinkList.java to test your codeStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started