Create a new java class called ListNode Implement ListNode a

Create a new java class called ListNode. Implement ListNode as a generic version of IntNode. public class ListNode { }

Create a generic class called Queue, which is a linked list implementation of a queue data type. Queue must have the following methods:

• public void enqueue(T value){} This method should add a new node to the back of the queue.

• public T dequeue(){} This method should remove a node from the front of the queue and return its value

• public T peek(){} This method should return the value of the front node without removing it from the queue

Create a new generic class called Stack, which is a linked list implementation of a stack data type. Stack must have the following methods:

• public void push(T value){} This method should add a new node to the top of the stack.

• public T pop(){} This method should remove a node from the top of the stack and return its integer value

• public T peek(){} This method should return the integer value of the top node without removing it from the stack

Solution

import java.util.Iterator;

public class LinkedList<E> extends AbstractList<E> {

                private ListNode front;   // refers to first node in list (null if empty)

               

                                public LinkedList() {

                                front = null; // null front means empty

                }

               

               

                public void add(int index, E value) {

                                if (index == 0) {

                                                // insert at the front

                                                front = new ListNode(value, front);

                                } else {

                                                                                                ListNode current = goTo(index - 1);

                                                ListNode newNode = new ListNode(value, current.next);

                                                current.next = newNode;

                                               

                                }

                }

               

               

                public E get(int index) {

                                ListNode current = goTo(index);

                                return current.data;

                }

               

               

                public int indexOf(E value) {

                                int index = 0;

                                ListNode current = front;

                                while (current != null) {

                                                if (current.data == value) {

                                                                return index;

                                                }

                                                index++;

                                                current = current.next;

                                }

                                return -1;

                }

               

                                public Iterator<E> iterator() {

                                return new LinkedListIterator();

                }

               

               

                public void remove(int index) {

                                if (index == 0) {

                                                // removing from the front

                                                front = front.next;

                                } else {

                                                ListNode current = goTo(index - 1);

                                                current.next = current.next.next;   // deletes the node

                                }

                }

               

               

                public void set(int index, E value) {

                                ListNode current = goTo(index);

                                current.data = value;

                }

               

                                public int size() {

                                int count = 0;

                                ListNode current = front;

                                while (current != null) {

                                                current = current.next;

                                                count++;

                                }

                                return count;

                }

               

               

                public String toString() {

                                if (front == null) {

                                                return \"[]\";

                                } else {

                                                String result = \"[\" + front.data;

                                                ListNode current = front.next;

                                                while (current != null) {

                                                                result += \", \" + current.data;

                                                                current = current.next;

                                                }

                                                result += \"]\";

                                                return result;

                                }

                }

               

                private ListNode goTo(int index) {

                                ListNode current = front;

                                for (int i = 0; i < index; i++) {

                                                current = current.next;

                                }

                                return current;

                }

               

               

                public class ListNode {

                                public E data;

                                public ListNode next;

  

                                public ListNode(E data) {

                                                this.data = data;

                                                this.next = null;

                                }

  

                                public ListNode(E data, ListNode next) {

                                                this.data = data;

                                                this.next = next;

                                }

                }

               

                private class LinkedListIterator implements Iterator<E> {

                                private ListNode current;   // current position in list

                                public LinkedListIterator() {

                                                current = front;

                                }

                                public boolean hasNext() {

                                                return current != null;

                                }

                                public E next() {

                                                E result = current.data;

                                                current = current.next;

                                                return result;

                                }

                                                                public void remove() {

                                                throw new UnsupportedOperationException();

                                }

                }              

}

Create a new java class called ListNode. Implement ListNode as a generic version of IntNode. public class ListNode { } Create a generic class called Queue, whic
Create a new java class called ListNode. Implement ListNode as a generic version of IntNode. public class ListNode { } Create a generic class called Queue, whic
Create a new java class called ListNode. Implement ListNode as a generic version of IntNode. public class ListNode { } Create a generic class called Queue, whic
Create a new java class called ListNode. Implement ListNode as a generic version of IntNode. public class ListNode { } Create a generic class called Queue, whic
Create a new java class called ListNode. Implement ListNode as a generic version of IntNode. public class ListNode { } Create a generic class called Queue, whic

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site