0

Im trying to make Linked List Reverse and also remove method.

Mostly the program working properly, but It is not compatible between remove() and reverseByLink().

when I remove queue and after when I try to use reverseByLink it doesn't reverse at all. When I debug it, it is showing that while loop(cur != null) has found null value. So I don't know what to do.

void reverseBylink() { Node prev = null; Node current = this.first; Node next = null; Node temp = null; while (current != null) { next = current.next; current.next = prev; prev = current; current = next; } temp =first; first = last; last = temp; } //How to do this...;<.. int remove(Item item) { Node cur = this.first; Node prev = null; Node temp2 = null; while (cur != null) { if (cur.item.equals(item)) { if (prev != null) { prev.next = cur.next; cur = cur.next; } else { cur = cur.next; first = cur; } } else { prev = cur; cur = cur.next; } } temp2 = first; first = last; last = temp2; return 0; } /** * Unit tests the <tt>LinkedQueue</tt> data type. */ public static void main(String[] args) { LinkedQueue<String> q = new LinkedQueue<String>(); //Working properly for reverseByStack. q.enqueue("a"); q.enqueue("b"); q.enqueue("c"); q.enqueue("a"); q.enqueue("d"); q.enqueue("b"); q.enqueue("abba"); q.enqueue("a"); q.enqueue("z"); q.enqueue("a"); q.reverseBystack(); System.out.println(q); q.remove("a"); q.remove("f"); q.remove("c"); q.reverseBylink(); System.out.println(q); } } 

these are what i made.

could you please help me what I need to change?

1
  • is this on git. I will be willing to help if you put it on there Commented Feb 14, 2019 at 18:24

2 Answers 2

1

Try if that works for you:

void reverseBylink() { Node tail = null; while( this.first != null) { Node current = this.first; this.first = this.first.next; current.next = tail; tail = current; } this.first = tail; } 
Sign up to request clarification or add additional context in comments.

Comments

1

Try looking into existing collection libraries from Guava etc...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.