Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Linked List Help

  1. #1
    Member
    Join Date
    Feb 2010
    Posts
    30
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Linked List Help

    So, I have to implement a linked-list to create a collection. I have to create the add, remove, union, equals, contains, isEmpty, size, and iterator method. I also have to implement an interface. Here is my code so far. I need help getting started. I just did this same assignment with an Array-List, but now with an Linked-List. Should the code be different?

    I have to use the fields that I have, my teacher gave them to me. contents and count.

    import java.util.Iterator;
     
     
    public class LinkedSet<T> implements SetADT<T>{
     
    	private LinearNode<T> contents;
    	//private T[] contents;
    	private int count;
     
    	// Constructs an empty set
    	public LinkedSet(){
    		this.count = 0;
    		this.contents = null;
    	}
     
     
    	@Override
    	public void add(T element) {
    		//TODO: put in branch to handle empty and not empty differently
    		LinearNode<T> node = new LinearNode<T>();
    		node.setElement(element);
    		this.count++;
    		//TODO: by def of set, check to see if element already contained
     
    	}
     
    	@Override
    	public T removeRandom() throws Exception {
    		// TODO Auto-generated method stub
    		return null;
    	}
     
    	@Override
    	public void remove(T element) {
    		// TODO Auto-generated method stub
     
    	}
     
    	@Override
    	public SetADT<T> union(SetADT<T> set) {
    		// TODO Auto-generated method stub
    		return null;
    	}
     
    	@Override
    	public boolean contains(T element) {
     
    		for (int i = 0; i < count; i++) {
    			if (contents.equals(element)) {
    				return true;
    			}
    		}
    		return false;
    	}
     
    	@Override
    	public boolean equals(SetADT<T> set) {
    		Iterator<T> t = set.iterator();
     
    		while (t.hasNext()) {
    			if (!this.contains(t.next()))
    				return false;
    		}
     
    		return true;
    	}
     
    	@Override
    	public boolean isEmpty() {
    		// TODO Auto-generated method stub
    		return false;
    	}
     
    	@Override
    	public int size() {
    		return count;
    	}
     
    	@Override
    	public Iterator<T> iterator() {
    		// TODO Auto-generated method stub
    		return null;
    	}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Linked List Help

    Can you post your questions or the problems you are having.

  3. #3
    Member
    Join Date
    Feb 2010
    Posts
    30
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Linked List Help

    I'm not sure if the methods I have written are correct. I also don't know how to solve the others.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Linked List Help

    I'm not sure if the methods I have written are correct.
    How have you tested them? Besides writing the code you have to test it.

    Write a main method that uses the class and its methods to test the code.

Similar Threads

  1. linked list help
    By tjoney in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 3rd, 2011, 06:54 PM
  2. what's wrong with my linked list
    By amr in forum What's Wrong With My Code?
    Replies: 10
    Last Post: February 13th, 2011, 02:55 PM
  3. [SOLVED] Linked List Help
    By lieles in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 4th, 2011, 10:32 PM
  4. Help with linked list
    By joecool594 in forum Collections and Generics
    Replies: 3
    Last Post: November 28th, 2010, 12:33 PM
  5. Recursive function based on Linked list
    By rosh72851 in forum Collections and Generics
    Replies: 1
    Last Post: March 9th, 2009, 06:23 PM