Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

We implemented SinglyLinkedList in class, now complete the implementation by adding the methods: a. find(e): Finds and returns element with value e in the list

We implemented SinglyLinkedList in class, now complete the implementation by adding the methods:

a. find(e): Finds and returns element with value e in the list

b. set(oldE,newE): Replaces the elements value oldE with value newE

c. removeFirst(): Removes and returns the first element of the list

d. removeLast(): Removes and returns the last element of the list

e. removeBetween(i): Removes and returns the i-th element

f. removeBetween(e): Removes the element with value e

/* Singly linked list * Implementation*/ public class SinglyLinkedList { private Node head; private int size; /* Constructor */ public SinglyLinkedList() { size = 0; } /* Returns a boolean indicating whether the list is empty */ public boolean isEmpty() { return size == 0; } /* Returns the number of elements in the list */ public int size() { return size; } /* Returns the first element in the list */ public T first() { if (this.isEmpty()) { return null; } else { return head.value; } } /* Returns the last element in the list */ public T last() { if (this.isEmpty()) { return null; } else { // traverse the linked list to find the tail Node currentNode = head; while(currentNode.next != null) { currentNode = currentNode.next;} return currentNode.value; } } /* Insert at head : add an element to the front of the list */ public void addAtHead(T value) { Node newest = new Node<>(); // allocate a new node newest.value = value; // insert the element newest.next = head; // have new node point to old head head = newest; // update head to point to new node size++; // linked list size increments } /* Insert at tail*/ public void addAtTail(T value) { Node newestNext = null; // Have new node point to null Node newest = new Node<>(newestNext); // allocate a new node newest.value = value; // insert the element // if there is no elements in the linked list if(size == 0) { head = newest; return; } // traverse the linked list to find the tail Node currentNode = head; while(currentNode.next != null) { currentNode = currentNode.next; } // currentNode.next = newest; // have old last node point to new node size++; // linked list size increments } /* Insert an element into the i-th element in the list */ public void addInMiddle(int index, T value) { // test index out of bounds if(index > this.size()) { System.out.println("Out of list bounds!"); return; } // if index = 1, = insert at head if(index == 1) { addAtHead(value); return; } // if index = i, = insert at the i-th element // find currentNode with index i Node currentNode = head; for(int i = 0; currentNode != null && (i+1)!= index-1; i++) { currentNode = currentNode.next; } // allocate a new node Node newNode = new Node(); // insert new element newNode.value = value; // have the new node point to the same next node newNode.next = currentNode.next; // have the current node point to the new node currentNode.next = newNode; // size++; // linked list size increments } /* Remove at head */ public T removeAtHead() { if(this.isEmpty()) { return null; } else { T removed = head.value; head = head.next; size--; return removed; } } /* Remove at tail */ public T removeAtTail() { if(this.isEmpty()) { return null; } // if linked list's size = 1, = remove from head if(this.size == 1) { T removed = removeAtHead(); } // allocate a temporary node Node currentNode = head; Node preCurrentNode = null; // keep a moving window scheme while(currentNode.next != null) { preCurrentNode = currentNode; currentNode = currentNode.next; } T removed = currentNode.value; // find the second last node then have it point to null preCurrentNode.next = null; size--; // linked list size decrements return removed; } public void printList() { Node current = head; System.out.print("List: "); while(current != null) { System.out.printf("%2d ", current.value); current = current.next; } System.out.printf(" "); } public void printList2() { System.out.print("List: "); for(Node current = head; current != null; current = current.next) { System.out.printf("%2d ", current.value); } System.out.printf(" "); } } 

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

Advances In Spatial Databases 2nd Symposium Ssd 91 Zurich Switzerland August 1991 Proceedings Lncs 525

Authors: Oliver Gunther ,Hans-Jorg Schek

1st Edition

3540544143, 978-3540544142

More Books

Students also viewed these Databases questions

Question

What can any retailer learn from this case?

Answered: 1 week ago

Question

6. What is process reengineering? Why is it relevant to training?

Answered: 1 week ago