Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Add a method size to our implementation of the LinkedList class that computes the number of elements in the list by following links and counting
Add a method size to our implementation of the LinkedList class that computes the number of elements in the list by following links and counting the elements until the end of the list is reached. please sent me full program. the one in the solution book and this one doesn't work....!!!
Error: Main method not found in class Node, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application Command exited with non-zero status 1? Error: Could not find or load main class undefined Not Working class Node{ Node next; int data; Node(){ data = 0; next = null; } Node(int b){ this.data = b; } public int getValue(){ return data; } public void setNext(Node b){ next = b; } public Node getNext(){ return next; } @Override public String toString(){ return data+""; } } class MyLinkedList{ private Node head; private int numberOfItems; MyLinkedList(){ head= null; numberOfItems = 0; } public int numberOfItems(){ return numberOfItems; } public void addNode(int b){ Node a = new Node(b); Node current = head; if(head==null){ head = a; numberOfItems++; return; } a.setNext(head); head = a; numberOfItems++; return; } public void addLast(int b){ Node a = new Node(b); Node current = head; if(head==null){ head = a; numberOfItems++; return; } while(current.getNext()!=null){ current = current.getNext(); } current.setNext(a); numberOfItems++; return; } public int deleteNode(int b){ if(head.getValue()==(b)){ head = head.getNext(); numberOfItems--; return b; } Node temp = head.getNext(); Node prev = head; for(int i=1;i<numberOfItems;i++){ //System.out.println(i+" "+temp.getBook()); if(temp.getValue()==(b)){ int ans = b; prev.setNext(temp.getNext()); numberOfItems--; return b; } prev = temp; temp = temp.getNext(); } return -1; } public void printNode(Node n){ System.out.println(n); } public void printList(){ Node temp = head; while(temp!=null){ System.out.print(temp.getValue()+"->"); temp = temp.getNext(); } System.out.println("-->NULL"); } public String toString(){ String res = ""; for(Node pos = head;pos!=null;pos = pos.getNext()){ if(pos.getValue()==-1){ res += "null"; } else{ res += pos.getValue(); } if(pos.getNext()!=null)res += " "; } return res; } public int getSize(){ Node temp = head; int ans=0; while(temp!=null){ ans++; temp = temp.getNext(); } return ans; } } class Tester { public static void main(String[] args) { MyLinkedList list = new MyLinkedList(); System.out.println("Adding to first"); list.addNode(3); list.addNode(4); list.addNode(5); System.out.println(list); System.out.println("Adding to last"); list.addLast(6); list.addLast(7); list.addLast(8); System.out.println(list); System.out.println("Deleting 7 and 4"); list.deleteNode(7); list.deleteNode(4); System.out.println(list); System.out.println("Size "+list.getSize()); } }
Step 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