I am having a lot of trouble understanding what to do with this assignment. I have tried to tackle the whole problem aside from printing the actual whole list because I was going to do that last after I had adding and removing working.
I created a “LinkedListDeque” per the project instructions, which contains within it a vtNode which is just a generic node that contains its item and next and prev pointers.
Currently my program is weird because it will add things to the front and the back of the list but what I am particularly noticing is that if I add an item to the front AND and item to the back, the front and back nodes are not connected to one another even though they both seem to be properly connected to the sentinal node.
Question: Can you help me understand why my sentinal.next does not link up to sentinal.prev correctly and vice versa?
/*This appends to the end of the list*/
public void addLast(vT itemToAdd) {
if (this.sentinel.next == null)
{
sentinel.next = new vNode<>(itemToAdd, sentinel, sentinel);
sentinel.prev = sentinel.next;
} else {
sentinel.prev = new vNode<>(itemToAdd, sentinel.next, sentinel.prev);//<--it should append correctly right? I used to have this say sentinal.next but then it appended to the end...
}
size += 1;
}
//an attempt to construct my own SLList for generics
import java.io.Console;
import java.util.Scanner;
/*Main data structure, a "double-ended-queue" or "deck" allows access at both ends*/
public class LinkedListDeque<vT> {
private vNode<vT> sentinel;
private int size;
/*Empty constructor*/
public LinkedListDeque() {
sentinel = new vNode("", null, null);
size = 0;
}
/*Constructor with input*/
private LinkedListDeque(vNode<vT> first) {
sentinel = new vNode(0, null, null);
sentinel.next = new vNode(first, sentinel, sentinel);
sentinel.prev = sentinel.next;
size = 1;
}
/*vNode class definition, nested naked data structure.*/
private static class vNode<vT> {
public vT item;
public vNode<vT> next;
public vNode<vT> prev;
public vNode (vT v, vNode<vT> n, vNode<vT> p)
{
item = v;
next = n;
prev = p;
}
/*I literally got this off the internet and am not using it but it looks good*/
public vNode remove(vNode v) {
v.next.prev = v.prev;
v.prev.next = v.next;
v.next = null;
return v;
}
}
public boolean isEmpty() {
if (size == 0)
return true;
return false;
}
/*Retrieves item at index*/
public vNode get(int index) {
if (index == 0)
{
return this.sentinel.next;
} else {
if (index > 0)
{
this.sentinel = this.sentinel.next;
return this.get(index-1);
}
}
return null;
}
/*This appends to the end of the list*/
public void addLast(vT itemToAdd) {
if (this.sentinel.next == null)
{
sentinel.next = new vNode<>(itemToAdd, sentinel, sentinel);
sentinel.prev = sentinel.next;
} else {
sentinel.prev = new vNode<>(itemToAdd, sentinel.next, sentinel.prev);
}
size += 1;
}
/*This appends to the front of the list*/
public void addFirst(vT itemToAdd) {
int n = this.size();
if (this.sentinel.next == null) {
sentinel.next = new vNode<>(itemToAdd, sentinel, sentinel);
sentinel.prev = sentinel.next;
} else {
sentinel.next = new vNode<>(itemToAdd, sentinel.next, sentinel.prev);
}
size +=1 ;
}
public LinkedListDeque<vT> copy() {
return new LinkedListDeque<>(this.sentinel);
}
public int size() {
return size;
}
public void removeFirst ()
{
if(this.sentinel.next != null) {
this.sentinel.next = this.sentinel.next.next;
this.size -= 1;
}
}
public void removeLast ()
{
if(this.sentinel.prev != null) {
this.sentinel.prev = this.sentinel.prev.prev;
this.size -= 1;
}
}
/*I just added this - a print method*/
public void printDeque () {
int n = size();
while (n > 0) {
System.out.print(this.sentinel.next.item + ", ");
sentinel = sentinel.next;
n -= 1;
}
System.out.print("n");
}
public static void main(String[] args) {
boolean on = true;
LinkedListDeque<String> instanceVarListJava = new LinkedListDeque<>();
int n = instanceVarListJava.size();
while(on) {
Scanner s = new Scanner(System.in);
System.out.println("Enter choice: ");
String c = s.nextLine();
System.out.println("Choice: "+ c);
switch(c) {
case "addLast":
System.out.println("Enter what you want to add: ");
String end = s.nextLine();
instanceVarListJava.addLast(end);
//instanceVarListJava.sentinel = instanceVarListJava.sentinel.next;
//System.out.println(instanceVarListJava.sentinel.item);
do {
System.out.println("The FIRST item: " + instanceVarListJava.sentinel.next.item);
System.out.println("The SECOND item: " + instanceVarListJava.sentinel.next.next.item);
System.out.println("The Penultimate item: " + instanceVarListJava.sentinel.prev.prev.item);
System.out.println("The LAST item: " + instanceVarListJava.sentinel.prev.item);
n--;
}while (n > 0);
System.out.println(instanceVarListJava.size());
break;
case "get":
System.out.println("What index?: ");
int index = Integer.parseInt(s.nextLine());
System.out.println(instanceVarListJava.get(index).item);
break;
case "q":
System.exit(0);
case "removeFirst":
instanceVarListJava.removeFirst();
System.out.println("The FIRST item: " + instanceVarListJava.sentinel.next.item);
System.out.println("The SECOND item: " + instanceVarListJava.sentinel.next.next.item);
System.out.println("The Penultimate item: " + instanceVarListJava.sentinel.prev.prev.item);
System.out.println("The LAST item: " + instanceVarListJava.sentinel.prev.item);
System.out.println(instanceVarListJava.size());
break;
case "addFirst":
System.out.println("Enter what you want to add: ");
String start = s.nextLine();
instanceVarListJava.addFirst(start);
//instanceVarListJava.sentinel = instanceVarListJava.sentinel.next;
//System.out.println(instanceVarListJava.sentinel.item);
do {
System.out.println("The FIRST item: " + instanceVarListJava.sentinel.next.item);
System.out.println("The SECOND item: " + instanceVarListJava.sentinel.next.next.item);
System.out.println("The Penultimate item: " + instanceVarListJava.sentinel.prev.prev.item);
System.out.println("The LAST item: " + instanceVarListJava.sentinel.prev.item);
n--;
}while (n > 0);
System.out.println(instanceVarListJava.size());
break;
default:
LinkedListDeque<String> instanceVarList = new LinkedListDeque<>();
instanceVarList.addLast("Check1");
instanceVarList.addLast("Check2");
System.out.println(instanceVarList.sentinel.item);
System.out.println(instanceVarList.sentinel.next.item);
instanceVarList.addFirst("Stay away from the summoner!");
System.out.println(instanceVarList.sentinel.item);
System.out.println(instanceVarList.sentinel.next.item);
LinkedListDeque<String> newList = instanceVarList.copy();
System.out.println(newList.sentinel.item);
}
}
}
}
I have been trying to use the debugger and also the java visualizer but I can’t wrap my head around this right now.
I am adding how it behaves in case that is helpful
Enter choice:
addFirst
Choice: addFirst
Enter what you want to add:
Hello
The FIRST item: Hello
The SECOND item:
The Penultimate item:
The LAST item: Hello
Size: 1
Enter choice:
addLast
Choice: addLast
Enter what you want to add:
This will work to add to the end, but I don't think it is actually linked to "Hello"
The FIRST item: Hello
The SECOND item:
The Penultimate item: Hello
The LAST item: This will work to add to the end, but I don't think it is actually linked to "Hello"
Size: 2
Enter choice:
addFirst
Choice: addFirst
Enter what you want to add:
Adding to the front again works but again I don't think it works...
The FIRST item: Adding to the front again works but again I don't think it works...
The SECOND item: Hello
The Penultimate item: Hello
The LAST item: This will work to add to the end, but I don't think it is actually linked to "Hello"
Size: 3
Enter choice:
addLast
Choice: addLast
Enter what you want to add:
I will just do this to demonstrate what it is doing, next I will remove
The FIRST item: Adding to the front again works but again I don't think it works...
The SECOND item: Hello
The Penultimate item: This will work to add to the end, but I don't think it is actually linked to "Hello"
The LAST item: I will just do this to demonstrate what it is doing, next I will remove
Size: 4
Enter choice:
removeFirst
Choice: removeFirst
The FIRST item: Hello
The SECOND item:
The Penultimate item: This will work to add to the end, but I don't think it is actually linked to "Hello"
The LAST item: I will just do this to demonstrate what it is doing, next I will remove
Size: 3
Enter choice:
removeFirst
Choice: removeFirst
The FIRST item:
The SECOND item:
The Penultimate item: This will work to add to the end, but I don't think it is actually linked to "Hello"
The LAST item: I will just do this to demonstrate what it is doing, next I will remove
Size: 2
Enter choice:
removeFirst
Choice: removeFirst
The FIRST item:
The SECOND item:
The Penultimate item: This will work to add to the end, but I don't think it is actually linked to "Hello"
The LAST item: I will just do this to demonstrate what it is doing, next I will remove
Size: 1
Enter choice:
See there is definitely something wrong with remove here but I don't know what it is...
I added a print method and this is what it is yielding me when I try to print.
choice:
I hope
The FIRST item: hello
The SECOND item:
The Penultimate item: hello
The LAST item: I hope
Size: 2
...cut some to make room for this comment...
The LAST item: I hope
Size: 3
Enter choice:
addLast
Choice: addLast
Enter what you want to add:
Let's see this
The FIRST item: It doesn't work
The SECOND item: hello
The Penultimate item: I hope
The LAST item: Let's see this
Size: 4
Enter choice:
print
Choice: print
It doesn't work, hello, , It doesn't work,