2 Replies - 2022 Views - Last Post: 01 December 2010 - 01:15 AM Rate Topic: -----

#1 Guest_huey newton*


Reputation:

linked list in java without using java.util

Posted 30 November 2010 - 03:54 PM

The main problem that I am having is my boolean methods will not return anything in my main program.Also the program will stay in an infinite loop after it prints. I left out my main method because its me calling methods. Even if you can't help thanks for looking.

 

public class LinkList<E> {

     private Link<E> head; // first node
	 private Link<E> tail; // last node
	 private String name; // Name of list
	 
	 
	 // constructors

	 // Empty list
	 public LinkList() {
		 this("A List");
	 }

	 //Empty list number 2
	 public LinkList(String listName) {
		 name = listName;
		 head = tail = null;
	 }

	 // clear the list
	 public void clear() {
		 head = tail = null;

		}

	 // to see the list is empty (problem)
	 public Boolean isEmpty() { // (fix)
		 if (head == null)
			 return true;
		 return false;
		
	 }

	 // insert at front
	public void insertAtFront(E data) {

		if (isEmpty())
			head = tail = new Link<E>(data);

		else {
			Link <E> temp = new Link<E>(data);
			temp.nextLink = head;
			head = temp;
		}
	}

	// place in back
	public void insertAtBack(E data) {
		Link <E> temp = new Link<E>(data);
		if (isEmpty())
			head = tail = new Link<E>(data);
		else {
			tail.nextLink = temp;
			tail = temp;
		}
	}

	// Removes from back of the list
	public E removeFromBack() throws Exception {
		if (isEmpty())
			throw new Exception(name);
		E data = (E) tail.data;
		if(head == tail)
			head = tail = null;
		else {

		Link <E> current = head;

		while (current.nextLink != tail)
			current = current.nextLink;
		tail = current;
		tail.nextLink = null;
		}

		return data;
	}

	public boolean contains(E x) {	//(fix)
	  Link <E> current  = head;
		while (current != tail){
			if ( current.data == x)
				return true;
		current = current.nextLink;}
		if (tail.data == x)
			return true;
		return false;
	}
	
	public E get(int index){
		 Link <E> current  = head;
		int number = 1;
		while (current != tail){
		if (number == index)
			return current.data;
			number++;
		current = current.nextLink;
		}
		if (number == index){
			return current.data; 
		}
		
		return null;
		
	}

	public void lastIndex (Object X){
        int numbers = 0;
		Link <E> current = head;
		while (current != tail){
				numbers++;
			current = current.nextLink;	
		    }
numbers++;
		System.out.print("The last index is: " + numbers);
	}

	public void count (){
		Link <E> current = head;
		int Count = 0;
		
	   while  (current.nextLink != tail);
		Count++;

		System.out.print(Count);

	}

	public void print() {
   Link <E> current = head;
   
   if (current == null)
	   System.out.println("the list is empty");
   
   while (current != tail)
   {
	   System.out.print(" "+current.data + " ");
	   
	   current = current.nextLink;
   }
	
   System.out.print(tail.data);

	}
}


this is my node class, I feel this is okay but if I am wrong please share

public class Link <E> {
    public E data;
    public Link <E> nextLink;
	

    //Link constructors
    
    public Link(){
    	System.out.println("null");
    }
    
    
    public Link(E data) {
    	this.data = data;
    	this.nextLink = null;
    }
    
    public Link(E data, Link <E> node){
    	this.data = data;
    	this.nextLink = null;
    }

} 


Is This A Good Question/Topic? 0

Replies To: linked list in java without using java.util

#2 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12134
  • View blog
  • Posts: 45,117
  • Joined: 27-December 08

Re: linked list in java without using java.util

Posted 30 November 2010 - 11:40 PM

For your contains() method, don't compare Objects using the == operator, which compares memory locations. Use the equals() method instead.

As for your isEmpty() method, it looks fine to me. Can you post your main() method so I can take a look at that as well.
Was This Post Helpful? 0
  • +
  • -

#3 Guest_huey newton*


Reputation:

Re: linked list in java without using java.util

Posted 01 December 2010 - 01:15 AM

Thank you for pointing that out for me and I fixed that, but my main method is nothing really happens other than me making a linked list it compiles but will not show what I asked for.

 
public class Prog {
    public static void main(String[] args) throws Exception {
    
    	LinkList<Integer> List = new LinkList<Integer>();
    	

    System.out.println("Make a linked list"); 
    
    List.insertAtFront(1);
    List.insertAtFront(2);
    List.insertAtFront(3);
    System.out.println("place zero at back");
    List.insertAtBack(0);
    System.out.println("Print the list out");
    List.print(); 
    System.out.println();
    System.out.println("remove last node");
    List.removeFromBack();
   System.out.println("test contains method");
    List.contains(3);
    System.out.println("Test last inedx");
    List.lastIndex(List);
    System.out.println();
    System.out.println("test get method");
    List.get(2);
    System.out.println("test last index");
    
    List.lastIndex(List);// I think this is a logic error not too sure though 
    System.out.println();
    System.out.println("test count method");
    System.out.println();
    List.count();
 
    System.out.println("Print the list out");
    List.print();    

    
    }
}


Was This Post Helpful? 0

Page 1 of 1