5 Replies - 411 Views - Last Post: 31 July 2013 - 08:48 AM Rate Topic: -----

#1 Razorman678  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 04-July 13

Trouble deleting from LinkedList

Posted 30 July 2013 - 08:37 PM

Sorry to be making a new topic so fast, but I have a problem with another of my programs. I'm trying to make a linkedlist of aircraft, and I'm having trouble deleting an aircraft from my list at times. If my aircraft is at the head of the list, it removed just fine, but anything else is still on the display list. Can anyone notice anything wrong in the delete method (or perhaps elsewhere)?

package lab4;
  
public class ATC {
    private static Aircraft head;
    private static Aircraft tail;
    public static Aircraft other;
    public ATC()
    {
        head = null;
        tail = null;
        other=null;       
    }
    public static void resetCursor(){
        other=head;
    }
    public static void advanceCursor(){
        other.setNext(other.getNext());
    }
    public static void rewindCursor(){
        other.setPrev(other.getPrev());
    }
    public static void addAircaft(String name, String type, double speed, double alt)
    {                    
            Aircraft temp = new Aircraft(name, type, speed, alt);
            //to insert first element
            if(head==null){
                head=temp;
                tail=temp;
                other=temp;
            }
            //to insert the 2nd to nth element
            else{
                other=tail;
                other.setNext(temp);
                temp.setPrev(other);
                tail=temp;
                advanceCursor();
                }
             
           System.out.println("One aircraft was added to your problem.");
    }
    public static void displayAll()
    {
            other = head;
            System.out.println("Aircraft in your airspace: ");
            //for emtpy list
            if (other == null)
            {
                    System.out.println("No aircraft to display.");
            }
            while(other!=null){
                String display = other.getName();
                System.out.println(display);
                other=other.getNext();
            }  
    }
    public static void showAircraft(String a)
    {
            other=head;
           try{ 
               while (!other.getName().equals(a))
               {    
                   other=other.getNext();
               }
               System.out.println(other.getInfo());        
           }
           catch (NullPointerException e){
               System.out.println("That element does not exist or the list is empty.");
            }
    }        
    public static void deleteAircaft(String rem)
    {
            Aircraft temp=null;
            other=head;
            boolean go=true;
            try{
                while(go){
                    if(other.getName().equals(rem)){
                        temp=other;
                        go=false;
                    }
                    else{
                        other=other.getNext();
                    }
                }
                other=temp;
                if(other==head){
                    head=head.getNext();
                    System.out.println("Aircraft was removed from your problem.");
  
                }
                else if(other==tail){
                    tail=tail.getPrev();
                    System.out.println("Aircraft was removed from your problem.");
                }
                else if(other==head&&other==tail){
                    head=null;
                    tail=null;
                    other=null;
                    System.out.println("Aircraft was removed from your problem.");
                }
                else{
                    other=other.getPrev();
                    other.setNext(other.getNext());
                    other.getNext().setPrev(other);
                    System.out.println("Aircraft was removed from your problem.");
                }
            }
            catch (NullPointerException e){
                System.out.println("That element does not exist or the list is empty.\n Back to menu");
            }      
    }
    public static void deleteAll()
    {
            head = null;
    }
} 


Is This A Good Question/Topic? 0
  • +

Replies To: Trouble deleting from LinkedList

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 8034
  • View blog
  • Posts: 13,747
  • Joined: 19-March 11

Re: Trouble deleting from LinkedList

Posted 30 July 2013 - 08:49 PM

else if(other==head&&other==tail){


This branch will never fire. Can you see why?


But here's your problem, I think:

103	                    other=other.getPrev();
104	                    other.setNext(other.getNext());
105	                    other.getNext().setPrev(other);


Look at what happens here. You set other to its predecessor, then you set its next equal to... what? And then, what is the result of line 105?


Work it through with a simple list, A -> B-> C

We've scanned down the list and now other is set to B, and we're going to try to delete it. What happens?

This post has been edited by jon.kiparsky: 30 July 2013 - 08:50 PM

Was This Post Helpful? 1
  • +
  • -

#3 Razorman678  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 04-July 13

Re: Trouble deleting from LinkedList

Posted 31 July 2013 - 04:40 AM

Yeah, I get what you're saying. What puzzles me as well is the conditional for removing the tail doesn't work either, so I guess 73-85 might have an issue as well.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5940
  • View blog
  • Posts: 12,866
  • Joined: 16-October 07

Re: Trouble deleting from LinkedList

Posted 31 July 2013 - 06:52 AM

Considering all the static, it's likely public ATC() will also never get called.

Why all the static? Why would something called Aircraft behave like a node? other?

Perhaps:
public class ATC {
	private Aircraft head, tail;

	public ATC()  { head  = tail = null; }
	public void addAircaft(String name, String type, double speed, double alt) { /* your code here */ }
	public boolean isEmpy() { return head==null; }
	public void displayAll() {
		if (isEmpty()) { 
			System.out.println("No aircraft to display.");
		} else {
			System.out.println("Aircraft in your airspace: ");
			Aircraft p = head;
			while(p!=null){
				System.out.println(other.getName());
				p=p.getNext();
			} 
		}
	}
	
	private Aircraft find(String a) { /* your code here */ }
    
	public void showAircraft(String a) {
		Aircraft found = find(a);
		if (found==null) {
			System.out.println("That element does not exist or the list is empty.");
		} else {
			System.out.println(found.getInfo());        
		}
	}
	
	private boolean deleteAircaft(Aircaft item) { /* your code here */ }
	
	public boolean deleteAircaft(String a)  { return deleteAircaft(find(a)); }
	public static void deleteAll() { head = tail = null; }
}


Was This Post Helpful? 1
  • +
  • -

#5 Razorman678  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 04-July 13

Re: Trouble deleting from LinkedList

Posted 31 July 2013 - 07:59 AM

What would I return for the boolean deleteaircraft method?

And if it makes it any easier to identify the problem, here are the other two classes I had with the program:

package lab4;
 
public class Aircraft {
        
    private double alt;
    private double speed;
    private String name;
    private String type;
    private String info;
    public Aircraft next;
    public Aircraft previous;
    
    public Aircraft(String name2, String type2, double speed2, double alt2)
    {
            alt = alt2;
            speed = speed2;
            name = name2;
            type = type2;
            info = name +"  Type: "+  type +"  Altitude: "+ alt+"  Speed: "+ speed;
    }
    
    public String getInfo()
    {
            return info;
    }
    
    public String getName(){
        return name;
    }
    public void setNext(Aircraft node){
        next=node;
    }
    public void setPrev(Aircraft node){
        previous=node;
    }
    public Aircraft getNext(){
        return next;
    }
    public Aircraft getPrev(){
        return previous;
    }
}




package lab4;
import java.util.*;
 
public class Main {
    public static void main(String[] args){
        String type="";
        double speed=0;
        double alt=0;
        String name="";
        String cmd="";
        boolean state=true;
        boolean next=true;
        boolean forward=true;
        while(state){
            Scanner in= new Scanner(System.in);
            System.out.println("Input a command: \n\ta.Entering\n\tb.Leaving\n\tc.Display\n\td.Show\n\tq.Quit");
            cmd=in.nextLine();
            while(cmd.matches("[A-Da-d]||q||Q")==false){
                System.out.println("Invalid input. Enter either a, b, c, d or q.\tTry again:");
                cmd=in.nextLine();
            }
            //Get aircraft info from user
            //Add the aircraft to list
            if(cmd.compareToIgnoreCase("a")==0){   
                System.out.println("Choose the type of the aircraft:\n\ta.Wide-body airliner\n\tb.Regional airliner\n\tc.Private plane\n\td.Military\n\te.Cargo only\n\tf.Unknown");
                String choice=in.next();
                while(choice.matches("[A-Fa-f]")==false){
                    System.out.println("Invalid input. Enter a letter from a-f\tTry again:");
                    choice=in.nextLine();
                }
                if(choice.compareToIgnoreCase("a")==0){
                	type="Wide-body airliner";
                	choice=in.nextLine();
                }
                else if(choice.compareToIgnoreCase("b")==0){
                	type="Regional airliner";
                	choice=in.nextLine();
                }
                else if(choice.compareToIgnoreCase("c")==0){
                	type="Private plane";
                	choice=in.nextLine();
                }
                else if(choice.compareToIgnoreCase("d")==0){
                	type="Military";
                	choice=in.nextLine();
                }
                else if(choice.compareToIgnoreCase("e")==0){
                	type="Cargo only";
                	choice=in.nextLine();
                }
                else if(choice.compareToIgnoreCase("f")==0){
                	type="Unknown";
                	choice=in.nextLine();
                }
                while(next){
                try{
                System.out.println("Enter the altitude in feet:");
                alt=in.nextDouble();
                next=false;
                }
                catch(InputMismatchException e){
                    System.out.println(e+"You need to enter a number for altitude.\nTry again.");
                    in.nextLine();
                    next=true;
                }
                }
                while(forward){
                    try{
                        System.out.println("Enter the speed in miles per hour:");
                        speed=in.nextDouble();
                        forward=false;
                    }
                    catch(InputMismatchException e){
                        System.out.println(e+"You have to use numbers for speed.\nTry again.");
                        in.nextLine();
                        forward=true;
                    }
                    }
                System.out.println("Enter the name:");
                name=in.next();
                ATC.addAircaft(name, type, speed, alt);
            }
            //Get aircraft info from user
            //Remove the aircraft from the list
            if(cmd.compareToIgnoreCase("b")==0){   
                System.out.println("Enter name of departing aircaft:");
                name=in.nextLine();
                ATC.deleteAircaft(name);
                //Delete aircaft from list
                 
            }
            //Display all the aircraft in the list
            if(cmd.compareToIgnoreCase("c")==0){   
                ATC.displayAll();               
            }
            //get aircaft name from user
            //show aircraft info
            if(cmd.compareToIgnoreCase("d")==0){   
                System.out.println("Enter the name of the aircaft:");
                name=in.nextLine();
                ATC.showAircraft(name);
            }
            if(cmd.compareToIgnoreCase("q")==0){   
                System.exit(0);
            }
        }
        //delete all remaining aircraft when quitting
        ATC.deleteAll();     
    }
} 

Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5940
  • View blog
  • Posts: 12,866
  • Joined: 16-October 07

Re: Trouble deleting from LinkedList

Posted 31 July 2013 - 08:48 AM

Well, a boolean is either true or false. You'd return one to save you putting all that UI code in your container class.

e.g.
public static void main(String[] args){
	ATC list = new ATC();
	//...
	System.out.println("Enter name of departing aircaft:");
	String name=in.nextLine();
	System.out.printl"Aircraft " + name+ " ");
	if (list.deleteAircaft(name)) {
		System.out.println("was removed from your problem.");
	} else {
		System.out.println("was NOT removed.");
	}



I would keep your Aircraft separate from your list node. e.g.
public class Aircraft {
	private double alt, speed;
	private String name, type;

	public Aircraft(String name, String type, double speed, double alt) {
		this.name = name;
		this.type = type;
		this.speed = speed;
		this.alt = alt;
	
	}
	public String getInfo() { return name +"  Type: "+  type +"  Altitude: "+ alt+"  Speed: "+ speed; }
	public String getName() { return name; }
	// you want a few more getters here
}

public class AircraftNode {
    public Aircraft data;
    public AircraftNode prev, next;
	public AircraftNode(Aircraft data, AircraftNode prev, AircraftNode next) {
		this.data = data;
		this.prev = prev;
		this.next = next;
	}
}



Now, you could make your find public:
public class AircraftList {
	private AircraftNode head, tail;

	public AircraftList()  { head  = tail = null; }
	public void add(Aircaft item) { /* your code here */ }
	public Aircraft findByName(String name) { /* your code here */ }
	public Aircraft removeByName(String name) { /* your code here */ }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1