Click here to Skip to main content
15,068,164 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
am trying to delete odd numbers from queue list
but the problem that I am struggling with is that the last element which the rear is printing even if it is an odd number

this is the function for deleting odd

Java
public class queueLinked<i> {
    private Node rear;
    private Node front;
    private int siz;


    public boolean isEmpty() {//function return boolean if is empty or not
        boolean response = false;
        if (siz == 0) {
            response = true;

        }
        return response;
    }

    public void enqueue(int element) { // inserting the value type of int
        Node node = new Node(element);
        if (front == null) {
            rear = node;
            front = node;

        } else {
            rear.setNext(node);
            rear = node;
            siz++;
        }

    }

    public queueLinked() {
        front = null;
        rear = null;
        siz = 0;
    }

    public Node dequeue() { // to remove the a element in the queue
        Node response = null;
        if (front != null) ;
        if (front.getNext() != null) {
            response = new Node(front.getData());
            front = front.getNext();
            siz--;
        } else {
            response = new Node(front.getData());
            front = null;
            rear = null;
        }
        return response;
    }

    public Node peak() {
        Node response = null;
        if (!isEmpty()) {
            response = new Node(front.getData());
        }
        return response;
    }

    public int getSiz() { // to get the size
        return siz;
    }

    public void display() { // display the queue function
        System.out.print("\nQueue = ");
        if (siz == 0) {
            System.out.print("Empty\n");
            return;
        }
        Node ptr = front;
        while (ptr != rear.getNext()) {
            System.out.print(ptr.getData() + " ");
            ptr = ptr.getNext();
        }
        System.out.println();
    }


    public void deleteOdd() {
        if (siz == 0) {
            System.out.print("Empty\n");
            return;
        }
        Node ptr = front;
        Node tmp ;

        while (ptr != rear  ) {

            if (ptr.getData() % 2 != 0) {
                  tmp = ptr.getNext();
                ptr.data = tmp.getData();
                ptr.next = tmp.next;
            }

            else
                ptr = ptr.getNext();


        }
        System.out.println();
    }

}


What I have tried:

the out it like this

Queue = 1 2 3 4 2 1 

out but after deleting odd

Queue = 2 4 2 1 
Posted
Updated 16-Jun-21 2:24am
v2
Comments
SeeSharp2 16-Jun-21 8:08am
   
Have you debugged it to see what is happening?
Abdullatif Art 16-Jun-21 8:18am
   
the debug doesn't work with me because of (storage issues)
SeeSharp2 16-Jun-21 8:43am
   
Debug means putting a breakpoint in your code and then stepping through it line by line. Then you can see exactly what is happening.

Quote:
Java
while (ptr != rear) {
Your code never tests the last element in your queue.

Change your loop condition to:
Java
while (ptr != null) {
But you'll still have problems, because for the last element in your queue, tmp will be null.

You need to re-think your approach to this assignment.
   
Quote:
the debug doesn't work with me because of (storage issues)

That's unlikely - the debugger isn't usually that large, and that app is a pretty trivial size.

But ... there are online debuggers, for example: Online Java Debugger - online editor[^] which will allow you to see what is happening with your code.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
   
Comments
Abdullatif Art 16-Jun-21 16:32pm
   
thank you so much for the site and the advice
OriginalGriff 16-Jun-21 16:37pm
   
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900