Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone please review my code? I'm struggling with Java, specifically linked listed. It keeps giving me items that don't match errors for the pop

Can someone please review my code? I'm struggling with Java, specifically linked listed. It keeps giving me items that don't match errors for the pop methods and will not print the results.

image text in transcribedimage text in transcribed

1 import java.util.NoSuchElementException; 2 3 public class NamenDequesItem> implements Deque { 4 5 private genNodesItem> head; 6 private genNode tail; 7 private int n; 8 private class genNode { //builds the node objects private Item element; private genNode next; private genNode prev; public genNode(Item element) { this.element = element; = next = null; prev = null; } public NameDequel) { //constructor this.head = null; this.tail = null; this.n = 0; } public void enqueueFront(Item element){ //push head genNode(element); if(isEmpty()) { tail = pushHead; } else { head.prev = pushHead; } pushHead.next = head; head = pushHead; n nH; 10@ 11 12 13 14 15 160 17 18 19 20 21 22 23 24 25 256 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 740 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 } public void enqueueBack(Item element) { //push tail genNode Item> pushTail = new genNode(element); if(isEmpty()) { head = pushTail; } else { tail.next = pushtail; } } pushTail.prev = tail; tail = pushTail; nu; } public Item dequeueFront() throws NoSuchelementException { //pop head if(isEmpty()) { throw new NoSuchElementException(); } genNode tailPointer = tail; if (head -- tail) { head = null; } else { tail.prev. next = null; } tail = tail.prev; tailPointer.prev = null; n--; return tailPointer; // return the value stored in the memory refernce the node holds public Item first() throws NoSuchelementException { //peek head if(isEmpty()) { throw NoSuchElementException(); } return head.element; public Item last() throws NoSuchelementException { //peek tail if(isEmpty()) { throw new No SuchelementException(); return tail.element; } public boolean isEmpty() { //returns true or false based if it is empty return head == null; public int size() { //size or "length" of the linked list return n; } public String toString() { String memval = ""; if(isEmpty()) { System.out.println("empty"); } else { for (genNode i = head; i !=null; i = i.next) { memval += i.element + " "; return memval

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

Students also viewed these Databases questions