Question: For the class SimpleLinkedList2 given to you in Lec#7 (folder: Lec7-Code/linkedlist_demo), write a method: public int getTotal() The method sums up all integer values in

For the class SimpleLinkedList2 given to you in Lec#7 (folder: Lec7-Code/linkedlist_demo), write a method: public int getTotal() The method sums up all integer values in the calling object of SimpleLinkedList2 and returns the sum. Your code CANNOT call any other methods in this class or any other class. It means that you have to implement it from scratch and walk down the list of Nodes in-person. Hint: Your code is inside the class SimpleLinkedList2 and has direct access to the data fields in the calling object of SimpleLinkedList2.

/* ConnectNodesDemo.java * Demo how to connect nodes indirectly outside class SingleLinkedList. */ package linkedlist_generic_demo;

import linkedlist_generic.SingleLinkedList;

public class ConnectNodesDemo { public static void main(String[] args) { SingleLinkedList names = new SingleLinkedList(); names.add("Tom"); names.add("Henry"); //this connects nodes indirectly names.add("Harry"); //this connects nodes indirectly names.add("Sam"); //this connects nodes indirectly } }

/* This file is what I demoed in class. * The gneneric single linked list class. */ package linkedlist_generic_demo;

public class SingleLinkedList { //private nested class Node private static class Node { private E data; //a reference to an object of any type private Node next; //a reference to the neighbor node immediately after

//create a new standalone node without anything following the node public Node(E data) { this.data = data; next = null; }

//create a new node and place it immediately before the node referenced by variable next public Node(E data, Node next) { this.data = data; this.next = next; }

} private Node head = null; //the reference to the first node in the list private int size = 0; //the number of nodes in the list //demo: how to connect nodes directly inside class SingleLinkedList. public static void main(String[] args) { Node tom = new Node("Tom"); Node henry = new Node("Henry"); Node harry = new Node("Harry"); Node sam = new Node("Sam");

//connect Node objects direcctly tom.next = henry; henry.next = harry; harry.next = sam;

System.out.println("pause"); //connect Node objects directly Node number1 = new Node(10); Node number2 = new Node(20); number1.next = number2; } //add a new item immediately after the node referenced by variable node private void addAfter(E item, Node node) { //Solution 1: node.next = new Node(item, node.next); //Wrong: Node temp2 = new Node(item, node.next); //Solution 2: //Node temp = new Node(item); //temp.next = node.next; //node.next = temp; size++; } //add the item so that it's the new first item in the list private void addFirst(E item) { //Solution 1: //head = new Node(item, head); //Solution 2: Node temp = new Node(item); temp.next = head; head = temp; size++; } //remove the first item in the list if there is one. //this version does not return the removed item. //The book version returns the removed item. private void removeFirst() { if (head != null) { head = head.next; size--; } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!