CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2012
    Posts
    1

    Cool Need help adding "bucket list" implementation to my circular list!

    So right now, I have a functional doubly linked circular list. It uses nodes of type T. I also have a test class showing usage of my linked-list methods. I'll ask my questions after i paste in my code
    Here's my code:

    Code:
        class Node<T>{
    	T data;
    	Node<T> next; 
    	Node<T> prev;
    	public Node(T data){
    		this.data = data;
    		this.prev = this;
    		this.next = this;
    	}
    	public Node(T data, Node<T> next, Node<T> prev){
    		this.data = data;
    		this.prev = prev;
    		this.next = next;
    	}
        }
       
    
        public class MyLinkedList<T> implements ADTListInterface<T> {
        Node<T> head;
    	int size = 0;
    	public void add(T o) {
    		Node<T> N = new Node<T>(o);
    		if(head == null){
    			head = N;
    			size++;
    			return;
    		}
    		add(o, 10);
    	}
    
    	public void add(T o, int index) {
    	 Node<T> N = new Node<T>(o);
    		if(head == null){
    			head = N;
    			size++;
    			return;
    		}
    	 if(index <= 0){
    		N.next = head;
    		N.prev = head.prev;
    		head.prev = N;
    		N.prev.next = N;
    		head = N;
    		size++;
    		return;
    	 }
    	 if(index > size){
    		 N.next = head;
    		 N.prev = head.prev;
    		 head.prev.next = N;
    		 head.prev = N;
    		 size++;
    		 return;
    	 }
    	 Node<T> temp = new Node<T>(null);
    	 temp = getNode(index);
    	 N.next = temp.next;
    	 N.prev = temp;
    	 temp.next = N;
    	 size++;
    	} 
    
    	public void clear() {
    		head = null;
    		size = 0;
    	}
    
    	public T get(int index) {
    		return getNode(index).data;
    	}
    	
    	//returns the node at the given index
    	private Node<T> getNode(int index) {
    		Node<T> temp = head;
    		for(int i = 0; i < index; i++){
    			temp = temp.next;
    		}
    		return temp;
    		
    	}
    
    	public boolean isEmpty() {
    		return head == null;	
    	}
    
    	public Iterator<T> iterator() {
    		// TODO Auto-generated method stub
    		return null;
    	}
    
    	public T remove(int index) {
    		Node<T> temp = getNode(index);
    		System.out.println("====>" + temp.data);
    		temp.prev.next = temp.next;
    		temp.next.prev = temp.prev;
    		if(index <= 0)
    			head = temp.next;
    		size--;
    		return temp.data;
    	}
    
    	public boolean remove(T o) {
    		for(int i = 0; i < size; i++){
    			if(o.equals(getNode(i).data))
    				return true;
    		}
    		return false;
    	}
    
    	public int size() {
    		return size;
    	}
    
         }
    And this is my test class:

    Code:
          public static void main(String[] args) {
    		MyLinkedList<Integer> testList = new MyLinkedList<Integer>();
    		testList.add(1);
    		testList.add(2);
    		testList.add(3);
    		testList.add(4);
    		testList.add(5);
    		testList.add(6);
    		for(int i = 0; i < testList.size(); i++){
    			System.out.println(testList.get(i));
    		}
    		testList.remove(5);
    		testList.remove(0);
    		testList.add(6666, 5);
    		testList.add(1111, 0);
    		for(int i = 0; i < testList.size(); i++){
    			System.out.println(testList.get(i));
    		}
    	}
       }
    So i'll split this question into 2 parts:
    1. What can I do to improve/add to MyLinkedList.java? Right now, I have all the methods filled out EXCEPT the iterator method, which I have no no idea how to go about. Other than that, I'm free to any advice/suggestions.
    2. The bigger part of the project is to implement a "bucket list" system into the nodes. This means, that each node has an array that can handle what the user inputs. I'm struggling with how the array buckets will work with my adding a node at the index. Also, if the right amount of elements have been removed, there must be some way to sort the elements to save space. Any advice would be appreciated.
    Here's the assignment description to be more specific:

    -You must provide a constructor that takes a single integer parameter that is the maximum bucket size
    -The underlying data strucure of your list should be a circular, douby linked reference based list.
    -Each node in your list should contain an array that is capable of containing 1 to max elements.

    Feel free to ask any questions for clarification!

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Need help adding "bucket list" implementation to my circular list!

    1. What can I do to improve/add to MyLinkedList.java?
    You could start by get it working properly.
    Try testing it with all the numbers from 1 to 13 and see what the test output shows.
    Also try removing an object rather than an index and see what happens.
    2. The bigger part of the project is to implement a "bucket list" system into the nodes.
    I suggest you look at the source code for java.util.ArrayList to see how to handle arrays with generics. It may also give you a few ideas on how to implement the iterator method.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured