Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(1) public class LinkedList { private ListNode head; private ListNode tail; public LinkedList() { head = null; tail = null; int count=0; } public boolean

(1)

public class LinkedList {  private ListNode head;  private ListNode tail;  public LinkedList() {  head = null;  tail = null;   int count=0;  }  public boolean isEmpty() {  return (head==null);  }   public void addToHead(Object item) {  if (isEmpty()) {  head = tail = new ListNode(item);  } else {  head = new ListNode(item, head);  }  }  public void addToTail ( Object item ) {  if (isEmpty()) {  head = tail = new ListNode(item);  } else {  tail.next = new ListNode(item);  tail = tail.next;  }  }   public Object removeFromHead() throws EmptyListException {  Object item = null;  if (isEmpty()) {  throw new EmptyListException();  }  item = head.data;  if (head == tail) // there's only one single node  head = tail = null;  else  head = head.next;  return item;  }   public Object removeFromTail() throws EmptyListException {  if (isEmpty()) {  throw new EmptyListException();  }  Object item = tail.data;  if (head == tail) { // there is only one node  head = tail = null;  return item;  }  ListNode current = head;  while (current.next != tail)  current = current.next;  tail = current;  tail.next = null;  return item;  }   public String toString() {  String s = "[ ";  ListNode current = head;  while (current != null) {  s += current.data + " ";  current = current.next;  }  return s + "]";  } }public Object getItemAt(int n) { if (n = count) throw new IndexOutOfBoundsException(); int currentPos=0; ListNode current=head; while (currentPos= count) throw new IndexOutOfBoundsException(); if (n==0) return removeFromHead(); int currentPos=0; ListNode current=head; while (currentPos = count) {addToTail(item); return; } int currentPos=0; ListNode current=head; while (currentPos 

I've finished Object getItemAt(int n), Object removeItemAt(int n), void addItemAt(Object item, int n), please help to add int getCount().

image text in transcribed

Method (b) Add the methods below to the class LinkedList in (a). The skeletons of the methods are provided to make your work easier. int getCount() Object getItemAt (int n) Object removeItemAt (int n) void addItemAt (Object item, int n) Description Returns the number of items in the linked list. Do not use the sillyCount () approach as shown in the lecture notes. Instead, maintains a member variable count. The variable is incremented when a new item is added, and is decremented when an item is removed from the linked list. Returns the item with index n in the linked list. The first item (referenced by head) is having the index of 0. The next one by 1 and so on. If n is negative or n is larger than or equal to the number of items in the list, IndexOutOfBoundsException will be thrown by the method. Remove and return the item with index n in the linked list. If n is negative or n is larger than or equal to the number of items in the list, IndexOutOfBoundsException will be thrown by the method. Add item to the linked list so that after insertion, item will have an index of n. If the list is currently empty, item becomes the first item (with index 0) of the list. If n is larger than or equal to the number of items in the list, item becomes the last item in the list. If n is negative, IndexOutOfBoundsException will be thrown by the method.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions