Java Doubly Linked List removeLast() - java

I have a class assignment that required me to create a class DoubleList that implements a ListADT.
I've gotten to the point where the code ALMOST works correctly. The output for the code should be:
1 3 7 9 13 14 16 17 23 24
3 9 13 16
My output is:
1 3 7 9 13 14 16 17 23 24
3 9 13 16 23
The first removeLast() seems to remove the 24 but for some reason the 23 stays after the second removeLast() is called. Please help!
EDIT: If I call removeLast() another time, it removes the 23.
class DoubleList<T> implements ListADT<T>{
private int _size;
private DoubleNode _head;
private DoubleNode _tail;
public DoubleList() {
_size = 0;
_head = null;
_tail = null;
}
public T removeFirst(){
if(_size == 0){
return null;
}
DoubleNode tmp = _head;
_head = _head._next;
_head._previous = null;
_size--;
return tmp._value;
}
public T removeLast(){
if(_size == 0) {
return null;
}
T temp = _tail._value;
_tail = _tail._previous;
_tail._next = null;
_size--;
return temp;
}
public T remove(T element){
if(_size == 0){
return null;
}
DoubleNode current = _head;
DoubleNode previous = null;
T temp = null;
do{
if(current._value == element){
temp = current._value;
if(previous == null){
_head = _head._next;
_head._previous = null;
}
else{
previous._next = current._next;
}
}
previous = current;
current = current._next;
}while(current != null);
return temp;
}
public T first(){
return _head._value;
}
public T last(){
return _tail._value;
}
public boolean contains(T target){
if(_size == 0){
return false;
}
DoubleNode temp = _head;
do{
if(temp._value == target){
return true;
}
temp = temp._next;
}while(temp != null);
return false;
}
public boolean isEmpty(){
if(_size == 0){
return true;
}
return false;
}
public int size(){
return _size;
}
public void add(T element)
{
int add = 0;
DoubleNode temp = new DoubleNode();
temp._value = element;
DoubleNode point = _head;
DoubleNode placeHolder;
if(_head == null) {
_head = temp;
_tail = temp;
_size++;
return;
}
else if((Integer)element <= (Integer)_head._value){
temp._next = _head;
_head._previous = temp;
_head = temp;
_size++;
return;
}
do {
if(point._next == null){
point._next = temp;
temp._previous = point;
_tail = temp;
_size++;
return;
}
else if((Integer)point._next._value >= (Integer)element && (Integer)point._value < (Integer)element){
placeHolder = point._next;
point._next = temp;
placeHolder._previous = temp;
temp._next = placeHolder;
temp._previous = point;
_size++;
return;
}
point = point._next;
} while (point != null);
_size++;
}
public String toString(){
String returnString = "";
if(_size == 0){
return returnString;
}
DoubleNode temp = _head;
do{
returnString += temp._value + " ";
temp = temp._next;
}while(temp != null);
return returnString;
}
private class DoubleNode {
private DoubleNode _previous;
private DoubleNode _next;
private T _value;
public DoubleNode() {
_previous = null;
_next = null;
_value = null;
}
public DoubleNode(T value){
_previous = null;
_next = null;
_value = value;
}
}
}
/**
* DoubleOrderedList testing area.
*
* #author (your name), Acuna
* #version (version)
*/
class Driver {
public static void main(String [] args) {
DoubleList<Integer> list = new DoubleList<>();
//RA: These are _extremely_ simple tests - do not use them when doing
// your writeup.
list.add(23);
list.add(24);
list.add(16);
list.add(3);
list.add(7);
list.add(17);
list.add(9);
list.add(13);
list.add(14);
list.add(1);
System.out.println("\nsize = " + list.size());
System.out.println(list);
list.remove(7);
System.out.println(list);
list.removeFirst();
System.out.println(list);
list.remove(17);
System.out.println(list);
list.removeLast();
System.out.println(list);
list.remove(14);
System.out.println(list);
list.removeLast();
System.out.println(list);

I don't think your bug is in removeLast. It's in the remove function, and it corrupts your list, causing further manipulations of the list to behave incorrectly.
When you remove an item from the middle of a double linked list, you need to stitch up the item before the removed item, and the item after the removed item. You are doing the first operation, but not the second. For example, suppose I have three elements in the list: L X R. I want to remove X. I have to set L._next = R (you do that), and also set R._previous = L (you don't do that). At that point, your list becomes corrupt, because your reverse links are off.

Take a look at your loop at remove function.
do{
if(current._value == element){
temp = current._value;
if(previous == null){
_head = _head._next;
_head._previous = null;
}
else{
previous._next = current._next;
//next line is missing
previous._next._previous = previous;
}
}
previous = current;
current = current._next;
}while(current != null);
Also, it's not efficient to loop while(current != null), consider (while !found && current != null) or break statement.

Related

Reverse even numbers (Only) from given Singly Linked List

I want to reverse even numbers in a singly linked list in java, but I face some difficulty to get the correct output.
For example,
input : 2, 18, 24, 3, 5, 7, 9, 6, 12
the method should reverse the even numbers only which are {2,18,24} and {6,12}
the correct output : 24 , 18 ,2 , 3 , 5 ,7 , 9 , 12 , 6
But,my output: 24 18 3 5 7 9 12 6 which it is wrong
the main method
public static void main(String[] args) throws Exception {
SLL<Integer> p = new SLL<Integer>();
int[] e = { 2, 18, 24, 3, 5, 7, 9, 6, 12,5 ,4 ,3 ,2,6,8};
for (int i = 0; i < e.length; i++) {
p.addToHead(e[i]);
}
p = reverse(p);
p.printAll();
}
This is the method (that doesn't work correctly)
public static SLL<Integer> reverse(SLL<Integer> p) {
SLL<Integer> returnList = new SLL<Integer>();
Stack<Integer> stk = new Stack<Integer>();
for (SLLNode tmp = p.getHead(); tmp != null; tmp = tmp.next) {
if ((((Integer) tmp.info) % 2) != 0) {
returnList.addToHead((Integer) tmp.info);
p.deleteFromHead();
} else if ((((Integer) tmp.info) % 2) == 0) {
stk.push((Integer) tmp.info);
p.deleteFromHead();
}
if (stk.getLSize() >= 2) {
while (!(stk.isEmpty())) {
returnList.addToHead((Integer) stk.pop());
}
}
}
return returnList;
}
this is the SLLNode class
public class SLLNode<T> {
public T info;
public SLLNode<T> next;
public SLLNode() {
this(null,null);
}
public SLLNode(T el) {
this(el,null);
}
public SLLNode(T el, SLLNode<T> ptr) {
info = el;
next = ptr;
}
}
this is the SLL class
public class SLL<T> {
protected SLLNode<T> head, tail;
public SLL() {
head = tail = null;
}
public boolean isEmpty() {
return head == null;
}
public void addToHead(T el) {
head = new SLLNode<T>(el, head);
if (tail == null)
tail = head;
}
public SLLNode getHead(){
return head;
}
public void addToTail(T el) {
if (!isEmpty()) {
tail.next = new SLLNode<T>(el);
tail = tail.next;
} else
head = tail = new SLLNode<T>(el);
}
public T deleteFromHead() { // delete the head and return its info;
if (isEmpty())
return null;
T el = head.info;
if (head == tail) // if only one node on the list;
head = tail = null;
else
head = head.next;
return el;
}
public T deleteFromTail() { // delete the tail and return its info;
if (isEmpty())
return null;
T el = tail.info;
if (head == tail) // if only one node in the list;
head = tail = null;
else { // if more than one node in the list,
SLLNode<T> tmp; // find the predecessor of tail;
for (tmp = head; tmp.next != tail; tmp = tmp.next)
;
tail = tmp; // the predecessor of tail becomes tail;
tail.next = null;
}
return el;
}
public void delete(T el) { // delete the node with an element el;
if (!isEmpty())
if (head == tail && el.equals(head.info)) // if only one
head = tail = null; // node on the list;
else if (el.equals(head.info)) // if more than one node on the list;
head = head.next; // and el is in the head node;
else { // if more than one node in the list
SLLNode<T> pred, tmp;// and el is in a nonhead node;
for (pred = head, tmp = head.next; tmp != null
&& !tmp.info.equals(el); pred = pred.next, tmp = tmp.next)
;
if (tmp != null) { // if el was found;
pred.next = tmp.next;
if (tmp == tail) // if el is in the last node;
tail = pred;
}
}
}
public void printAll() {
for (SLLNode<T> tmp = head; tmp != null; tmp = tmp.next)
System.out.print(tmp.info + " ");
}
public boolean isInList(T el) {
SLLNode<T> tmp;
for (tmp = head; tmp != null && !tmp.info.equals(el); tmp = tmp.next)
;
return tmp != null;
}
public int length() {
int length = 0;
for (SLLNode tmp = head; tmp != null; tmp = tmp.next) {
length += 1;
}
return length;
}
Running your reverse method gives me a different output to what you see. So, I suspect you got your output from slightly different code.
I get : 24, 6, 9, 7, 5, 3, 2, 18
In your reverse method you start adding even numbers to your returnList when you have 2 on the stack. If you want to reverse all even numbers you need to wait until you have all the continuous even numbers on the stack. Or in other words, when you get an odd number, or there are no numbers left, you can pop all the even numbers back off the stack..
I think you should also use addTail rather than addHead.
So something like
public static SLL<Integer> reverse(SLL<Integer> p) {
SLL<Integer> returnList = new SLL<Integer>();
Stack<Integer> stk = new Stack<Integer>();
for (SLLNode tmp = p.getHead(); tmp != null; tmp = tmp.next) {
if ((((Integer) tmp.info) % 2) != 0) {
// add any stacked even numbers
while (!(stk.isEmpty())) {
returnList.addToTail((Integer) stk.pop());
}
// add the odd number
returnList.addToTail((Integer) tmp.info);
} else if ((((Integer) tmp.info) % 2) == 0) {
System.out.println("even " + tmp.info);
stk.push((Integer) tmp.info);
}
}
// add any remaining even numbers from the stack
while (!(stk.isEmpty())) {
returnList.addToTail((Integer) stk.pop());
}
return returnList;
}
I made it using list of python, I just wanted to know whether I could write this program without using a linked list or not. The complexities may be high, but it is just an experimental implementation which works
arr = [2,18,24,3,5,7,9,6,12]
arr_final = []
temp = []
for i in arr:
if i%2 == 0:
temp.append(i)
else:
if temp!=[]:
temp.reverse()
for j in temp:
arr_final.append(j)
temp = []
arr_final.append(i)
if temp!=[]:
temp.reverse()
for j in temp:
arr_final.append(j)
temp = []
print(arr_final)

Adding two numbers given (digits in Linked List) using Strings

I am trying to add two non negative numbers, the digits of which are stored in reverse order in two separate linked lists. The answer should also be a linked list with digits reversed and no trailing zeros.
I understand that there is a way to solve this question by adding digits and maintaining a carry each time, but I am trying to solve it by using addition operation on numbers.
Here's my code:
/**
* Definition for singly-linked list.
* class ListNode {
* public int val;
* public ListNode next;
* ListNode(int x) { val = x; next = null; }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode a, ListNode b) {
if(a==null || b==null){
return null;
}
String num1 = "";
String num2 = "";
ListNode temp1 = a;
ListNode temp2 = b;
while(temp1!=null){
num1 = num1+Integer.toString(temp1.val);
temp1 = temp1.next;
}
new StringBuilder(num1).reverse().toString();
double value1 = Double.parseDouble(num1);
while(temp2!=null){
num2 = num2+Integer.toString(temp2.val);
temp2 = temp2.next;
}
new StringBuilder(num2).reverse().toString();
double value2 = Double.parseDouble(num2);
double result = value1+value2;
String res = String.format("%.0f",result);
ListNode first_node = new ListNode(Character.getNumericValue(res.charAt(0)));
ListNode ans = first_node;
for(int j=1;j<res.length();j++){
ListNode node = new ListNode(Character.getNumericValue(res.charAt(j)));
add(node,ans);
}
return ans;
}
public void add(ListNode node, ListNode ans){
ListNode temp;
temp = ans;
ans = node;
ans.next = temp;
}
}
My code has been giving wrong answers. Can anyone point out the errors?
Your approach is not correct and indirect.
You are trying to do big numbers arithmetics using floating point operations.
As a result - computing errors.
We have:
List<Integer> firstNumber;
List<Integer> secondNumber;
Lets assume firstNumber > secondNumber.
Try this alogrithm:
List<Integer> result = new ArrayList<>();
int i = 0;
int appendix = 0;
for (; i < secondNumber.size(); i++) {
int sum = firstNumber.get(i) + secondNumber.get(i) + appendix;
result.append(sum % 10);
appendix = sum / 10;
}
for (; i < firstNumber.size(); i++) {
int sum = firstNumber.get(i) + appendix;
result.append(sum % 10);
appendix = sum / 10;
}
if (appendix != 0)
result.append(appendix);
return result;
Your add function looks incorrect. You will get you number in the reverse order than what is expected.
Also, your solution is missing the point of the question. Your approach will fail if you have a number with a lot of digits (even double has its limits ~ 2^1024 I think). A linked list representation allows for numbers even bigger.
The correct solution would just iterate through both the lists simultaneously with a carry digit while creating the solution list. If this is a question in an assignment or coding competition, your solution would be judged wrong.
Your add method is wrong, it doesn't build correctly the list. Here is how the final part of your method should look, without the add method:
for(int j=1;j<res.length();j++){
ans.next = new ListNode(Character.getNumericValue(res.charAt(j)));;
ans = ans.next;
}
return first_node;
In your approach, the variable ans is not updating. You can try this:
ans = add(node,ans);
and in your add method, change the method to return ListNode ans
Your approach is not straightforward and wouldn't give you expected results.
Here is a simple approach which wouldn't require much explanation as the addition is simple integer by integer.
Do note that i carry forward when the sum of two integers is greater than 9 else continue with the sum of next integers from both the list.
class Node {
private Object data;
private Node next;
public Object getData() { return data; }
public void setData(Object data) { this.data = data; }
public Node getNext() { return next; }
public void setNext(Node next) { this.next = next; }
public Node(final Object data, final Node next) {
this.data = data;
this.next = next;
}
#Override
public String toString() { return "Node:[Data=" + data + "]"; }
}
class SinglyLinkedList {
Node start;
public SinglyLinkedList() { start = null; }
public void addFront(final Object data) {
// create a reference to the start node with new data
Node node = new Node(data, start);
// assign our start to a new node
start = node;
}
public void addRear(final Object data) {
Node node = new Node(data, null);
Node current = start;
if (current != null) {
while (current.getNext() != null) {
current = current.getNext();
}
current.setNext(node);
} else {
addFront(data);
}
}
public void deleteNode(final Object data) {
Node previous = start;
if (previous == null) {
return;
}
Node current = previous.getNext();
if (previous != null && previous.getData().equals(data)) {
start = previous.getNext();
previous = current;
current = previous.getNext();
return;
}
while (current != null) {
if (current.getData().equals(data)) {
previous.setNext(current.getNext());
current = previous.getNext();
} else {
previous = previous.getNext();
current = previous.getNext();
}
}
}
public Object getFront() {
if (start != null) {
return start.getData();
} else {
return null;
}
}
public void print() {
Node current = start;
if (current == null) {
System.out.println("SingleLinkedList is Empty");
}
while (current != null) {
System.out.print(current);
current = current.getNext();
if (current != null) {
System.out.print(", ");
}
}
}
public int size() {
int size = 0;
Node current = start;
while (current != null) {
current = current.getNext();
size++;
}
return size;
}
public Node getStart() {
return this.start;
}
public Node getRear() {
Node current = start;
Node previous = current;
while (current != null) {
previous = current;
current = current.getNext();
}
return previous;
}
}
public class AddNumbersInSinglyLinkedList {
public static void main(String[] args) {
SinglyLinkedList listOne = new SinglyLinkedList();
SinglyLinkedList listTwo = new SinglyLinkedList();
listOne.addFront(5);
listOne.addFront(1);
listOne.addFront(3);
listOne.print();
System.out.println();
listTwo.addFront(2);
listTwo.addFront(9);
listTwo.addFront(5);
listTwo.print();
SinglyLinkedList listThree = add(listOne, listTwo);
System.out.println();
listThree.print();
}
private static SinglyLinkedList add(SinglyLinkedList listOne, SinglyLinkedList listTwo) {
SinglyLinkedList result = new SinglyLinkedList();
Node startOne = listOne.getStart();
Node startTwo = listTwo.getStart();
int carry = 0;
while (startOne != null || startTwo != null) {
int one = 0;
int two = 0;
if (startOne != null) {
one = (Integer) startOne.getData();
startOne = startOne.getNext();
}
if (startTwo != null) {
two = (Integer) startTwo.getData();
startTwo = startTwo.getNext();
}
int sum = carry + one + two;
carry = 0;
if (sum > 9) {
carry = sum / 10;
result.addRear(sum % 10);
} else {
result.addRear(sum);
}
}
return result;
}
}
Sample Run
Node:[Data=3], Node:[Data=1], Node:[Data=5]
Node:[Data=5], Node:[Data=9], Node:[Data=2]
Node:[Data=8], Node:[Data=0], Node:[Data=8]

Insertion List Linked List Java {Debug}

Hello I am trying to write an insertion list in java but I do not seem to know while I have a running while loop that does not terminate in the sortInsertion method of my class. Kindly help.
public class LinkedList {
public static void main(String[] args) {
LinkedList numbers = new LinkedList();
numbers.enqueue(12);
numbers.enqueue(4);
numbers.enqueue(11);
numbers.enqueue(8);
numbers.enqueue(10);
numbers.sortInsertion(numbers.startNode);
//System.out.println(s);
}
private int numValues;
private Node startNode;
private Node endNode;
private Node currNode;
/** Constructor for BoundedQueue
* #param inputSize maximum size of the Queue
*/
public LinkedList() {
numValues = 0;
}
/**
* #return the current number of element in Queue
* Useful for testing
*/
public int size() {
return numValues;
}
/** Adds a new value to the end of the queue
*
* #param value the integer to be added
*/
public void enqueue(int value) {
this.currNode = new Node(value);
if (this.startNode == null) {
this.startNode = this.currNode;
this.endNode = this.startNode;
} else {
this.endNode.next = this.currNode;
this.endNode = this.currNode;
}
numValues++;
}
public String toString(Node startNode) {
String s="";
Node trav = startNode;
while(trav != null) {
s += trav.value;
s+=",";
trav = trav.next;
}
return s;
}
public Node sortInsertion( Node head) {
if(head == null || head.next == null)
return head;
Node sort = null;
Node trav = null;
Node travSort = null;
Node prevSort = null;
sort = head;
trav = head.next;
sort.next = null;
while(trav != null) {
travSort = sort;
System.out.println(travSort.value);
while(travSort != null) {
if(travSort.value > trav.value) {
Node temp = trav;
temp.next = travSort;
travSort = temp;
if(prevSort != null) {
prevSort.next = travSort;
}
if(sort.value == travSort.value) {
sort = travSort;
}
break;
}
prevSort = travSort;
travSort = travSort.next;
//System.out.println(travSort.value);
}
trav = trav.next;
}
System.out.println(toString(sort));
return sort;
}
private class Node {
private int value;
private Node next;
public Node(int val) {
value = val;
next = null;
}
}
}
It looks like you create a neverending redundancy within the following lines:
Node temp = trav; (1)
temp.next = travSort; (2)
travSort = temp; (3)
So temp==trav (1) but temp.next != trav.next but temp.next == travSort (2).
However (3) sets travSort = temp
Therefore travSort.next == temp.next == travSort
which is the reason your while-loop never ends, because once entered travSort can never be null.

Queue implementation, enqueue method not working

I am trying to write my own queue class. My enqueue method is only enqueue-ing one object and then if I try to enqueue anything else, its almost as if its ignoring it. Here is my code:
public class myQueue {
private Node front;
private Node back;
private int s;
public myQueue() {
front = null;
back = null;
s = 0;
}
public void enqueue(Object x) {
if( isEmpty() )
back = front = new Node(x);
else
back = back.next = new Node(x);
s++;
}
public Object dequeue() {
Object x;
if( isEmpty() ) { System.out.println("nothing to dequeue.\nqueue empty."); }
x = front.data;
s--;
return x;
}
public boolean isEmpty() {
if(s == 0)
return true;
else
return false;
}
public void printQueue() {
if ( isEmpty() )
System.out.println("empty queue");
else {
Node temp = back;
while(temp != null) {
System.out.println(temp);
temp = temp.next;
}
}
}
}
and here is my main method where i try to enqueue some objects:
public static void main(String[] args) {
int a = 5;
String b = "yo";
Object c = 5.5;
int d = 2;
String e = "Pen";
Object f = 9.2;
myQueue q = new myQueue();
q.enqueue(a);
q.enqueue(b);
q.enqueue(c);
q.enqueue(d);
q.enqueue(e);
q.enqueue(f);
System.out.println("\n");
q.printQueue();
}
and then all i get for output is:
data: 9.2
any ideas as to why this is happening?
When you print, you are starting at the back of the queue, you should start at the front:
Node temp = front; // <<< replacing back by front
while(temp != null) {
System.out.println(temp);
temp = temp.next;
}
If you start at the back of the queue, you will only have the last element of the queue to be printed...
My result with the fix:
data : 5
data : yo
data : 5.5
data : 2
data : Pen
data : 9.2

Creating A Linked List In Java Using Generics

I've looked through the site and can't find anything that is at least comparable to what I'm doing (please do not link any stackoverflow responses or questions I've been looking for an hour), so I've decided to ask this question. I'm trying to create a linked list (not using libraries of any kind) that uses generics, here is all the relevant code.
public class LList<T>
{
MyNode<T> head = null;
MyNode<T> tail = null;
MyNode<T> temp = null;
int count = 0;;
Scanner scan = new Scanner(System.in);
//puts a new value at the edn of the list
public void add(T i)
{
if(head == null)
{
head = new MyNode<T>();
head.data = i;
head.next = tail;
tail = head;
}
else
{
tail.next = new MyNode<T>();
tail = tail.next;
tail.data = i;
}
count ++;
}
//inserts a new value at a given index
public void insert(int index, T item)
{
if(index == size())
{
add(item);
}
else if(index == 0)
{
MyNode<T> temp = new MyNode<T>();
temp.data = item;
temp.next = head;
head.previous = temp;
head = temp;
count++;
}
temp = head;
for(int i = 0; i < index-1; i++)
{
temp = temp.next;
MyNode<T> myNode = new MyNode<T>();
myNode.data = item;
myNode.next = temp.next;
temp.next = myNode;
count++;
}
}
//returns the number of values in the list
public int size()
{
return count;
}
//returns the value at a given index
public T get(int index)
{
if(head == null || index > count -1)
{
return null;
}
if(index < 0 || index >= count)
{
System.out.println("This does not exist");
}
MyNode<T> p = head;
int size = 0;
while(size < index && p.next != null)
{
p = p.next;
size++;
}
if(count != index)
{
return null;
}
else
{
return p.data;
}
}
//removes the returns the first value in the list
public T remove()
{
head = head.next;
head.previous = null;
count--;
return (T) head;
}
//removes and returns the value at a given index
public T removeAt(T elem)
{
temp = head;
MyNode<T> two = null;
if(head.data.equals(elem))
{
head = head.next;
head.previous = null;
count--;
return elem;
}
else if(tail.data.equals(elem))
{
tail = tail.previous;
tail.next = null;
count--;
return elem;
}
while(temp != null && !temp.data.equals(elem))
{
two = temp;
temp = temp.next;
}
if(temp == null)
{
return null;
}
two.next = temp.next;
T spare = temp.data;
temp = null;
count--;
return spare;
}
//removes and returns the last value in the list
public T removeLast()
{
temp = tail;
tail = tail.previous;
temp = null;
count--;
return (T) tail;
}
//creates a string representation of all the values in the list
public String toString()
{
String result = null;
for(int i = 0; i < count; i++)
{
result = i + " : " + get(i).toString();
}
return result;
}
//removes all the values in the list
public void clear()
{
for(int i = count -1; i >= 0; i++)
{
removeAt(i);
}
}
//searches for a value in the list and returns the first index of that
//value when found
public int search(T find)
{
if(head == null)
{
return -10;
}
MyNode<T> p = head;
do
{
if(find.compareTo(p.data) == 0)
{
return 0;
}
else
{
return -1;
}
p = p.next;
}while(p != null);
}
public void itemChosen(int choice, LLMenu[] menu)
{
LLMenu m = menu[choice-1];
switch(m)
{
case ADD:
System.out.println("What value would you like to add?");
T addThis = scan.nextInt();
add(addThis);
break;
case INSERT:
System.out.println("What index would you like to replace?");
T replace = scan.nextInt();
System.out.println("What number would you like to insert?");
int val = scan.nextInt();
insert(val, replace);
break;
case SIZE:
size();
break;
case GET:
System.out.println("What index would you like to look at?");
int thisOne = scan.nextInt();
get(thisOne);
break;
case REMOVE:
remove();
break;
case REMOVEAT:
System.out.println("What index would you like to remove?");
T thisHere = scan.nextInt();
removeAt(thisHere);
break;
case REMOVELAST:
removeLast();
break;
case TOSTRING:
toString();
break;
case CLEAR:
clear();
break;
case SEARCH:
System.out.println("What value would you like to search for?");
T searchForMe = scan.nextInt();
search(searchForMe);
break;
}
}
}
and MyNode:
public class MyNode<T>
{
T data;
MyNode<T> next;
MyNode<T> previous;
}
Where I'm really having problems is at the switch statement in LList where I am scanning in items that are supposed to be set to Generics and obviously there is no method using Scanners to read in Generics. So question1, how do I read in and set these to Generics, and question2, in my clear method in LList how do I send variable i when using removeAt when it is expecting a Generic? Please keep all answers relevant and thanks for your time!
Edit
One more question in my search method inside the do-while there is an if statement that says if(find.compareTo(p.data) == 0) how can I change this so that it works? I wasn't really sure of what to put there so I kind of just wrote my thought down.
#1. You can implement generic console input method on your own - here is an example: Improvement/s to my Java generic console input method?
I believe your itemChosen code should be in the main method.
You clearly need to declare the T type.
public void main(String[] args)
{
LList<Integer> myLList = new LList<>();
}
Now add your desired case-switch code.

Categories

Resources