Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please answer in Java _____________________________________________________________________________________________________________________________________________________ A Singly-Linked List is the most basic form of linked list that you will have encountered, and pairs each element

Please answer in Java

_____________________________________________________________________________________________________________________________________________________

A "Singly-Linked List" is the most basic form of linked list that you will have encountered, and pairs each element of the collection with a single pointer or reference to the subsequent element. For this question you must implement an SLList class by completing the implementation provided in the zip file. The implementation should have the following functionality and performance characteristics: a) get(i), set(i, x), add(i, x), and remove(i) operations, each running in O(1 + i) time. b) a reverse() operation that reverses the order of the SLList. This must run in O(n) time, must not create any new nodes (n.b. local variables are ok, but you should not make any new Nodes), and must not use recursion or any type of extra working memory.

_____________________________________________________________________________________________________________________________________________________

import java.util.AbstractList; import java.util.Iterator; import java.util.Queue; import java.util.NoSuchElementException; /**  * An implementation of a FIFO Queue as a singly-linked list.  * This also includes the stack operations push and pop, which  * operate on the head of the queue  * @author morin  *  * @param <T> the class of objects stored in the queue  */ public class SLList extends AbstractList implements Queue { class Node { T x; Node next; } /**  * Front of the queue  */  Node head; /**  * Tail of the queue  */  Node tail; /**  * The number of elements in the queue  */  int n; public T get(int i) { // TODO: Implement this  if (i < 0 || i > n - 1) throw new IndexOutOfBoundsException(); return null; } public T set(int i, T x) { // TODO: Implement this  if (i < 0 || i > n - 1) throw new IndexOutOfBoundsException(); return null; } public void add(int i, T x) { // TODO: Implement this  if (i < 0 || i > n) throw new IndexOutOfBoundsException(); } public T remove(int i) { // TODO: Implement this  if (i < 0 || i > n - 1) throw new IndexOutOfBoundsException(); return null; } public void reverse() { // TODO: Implement this  } public Iterator iterator() { class SLIterator implements Iterator { protected Node p; public SLIterator() { p = head; } public boolean hasNext() { return p != null; } public T next() { T x = p.x; p = p.next; return x; } public void remove() { throw new UnsupportedOperationException(); } } return new SLIterator(); } public int size() { return n; } public boolean add(T x) { Node u = new Node(); u.x = x; if (n == 0) { head = u; } else { tail.next = u; } tail = u; n++; return true; } public boolean offer(T x) { return add(x); } public T peek() { if (n == 0) return null; return head.x; } public T element() { if (n == 0) throw new NoSuchElementException(); return head.x; } public T poll() { if (n == 0) return null; T x = head.x; head = head.next; if (--n == 0) tail = null; return x; } /**  * Stack push operation - push x onto the head of the list  * @param x the element to push onto the stack  * @return x  */  public T push(T x) { Node u = new Node(); u.x = x; u.next = head; head = u; if (n == 0) tail = u; n++; return x; } protected void deleteNext(Node u) { if (u.next == tail) tail = u; u.next = u.next.next; } protected void addAfter(Node u, Node v) { v.next = u.next; u.next = v; if (u == tail) tail = v; } protected Node getNode(int i) { Node u = head; for (int j = 0; j < i; j++) u = u.next; return u; } /**  * Stack pop operation - pop off the head of the list  * @return the element popped off  */  public T remove() { if (n == 0) return null; T x = head.x; head = head.next; if (--n == 0) tail = null; return x; } public T pop() { if (n == 0) return null; T x = head.x; head = head.next; if (--n == 0) tail = null; return x; } public static void main(String[] args) { } } 

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

More Books

Students also viewed these Databases questions

Question

What is the difference between a stock dividend and a stock split?

Answered: 1 week ago

Question

Describe how to train managers to coach employees. page 404

Answered: 1 week ago

Question

Discuss the steps in the development planning process. page 381

Answered: 1 week ago