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 2 of 2

Thread: About add and remove method of ListNode, i don't understand....

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default About add and remove method of ListNode, i don't understand....

    class ListNode {
    	private	Object data;
    	private	ListNode next;
    	ListNode(Object o) { data = o; next = null; }
    	ListNode(Object o, ListNode nextNode) 
    		{ data = o; next = nextNode; }
    	void setData(Object data){
    		this.data = data;
    	}
    	void setNext(ListNode next){
    		this.next = next;
    	}
    	Object getData() { return data; }
    	ListNode getNext() { return next; }
    } // class ListNode
     
    class EmptyListException extends RuntimeException {
    	public EmptyListException () { super ("List is empty"); }
    } // class EmptyListException
     
    class LinkedList {
    	private ListNode head;
    	private ListNode tail;
    	public LinkedList() { head = tail = null; }
     
    	public boolean isEmpty() { return head == null; }
     
    	public void addToHead(Object item) {
    		if (isEmpty())
    			head = tail = new ListNode(item);
    		else
    			head = new ListNode(item,head);
    	}
    	public void addToTail(Object item) {
    		if (isEmpty())
    			head = tail = new ListNode(item);
    		else{
    			tail.setNext(new ListNode(item));
    			tail = tail.getNext();
    		}
    	}
    	public Object removeFromHead() throws EmptyListException {
    		Object item = null;
    		if (isEmpty())
    			throw new EmptyListException();
    		item = head.getData();
    		if ( head == tail )
    			head = tail = null;
    		else
    			head = head.getNext();
    		return item;
    	}
    	public Object removeFromTail() throws EmptyListException {
    		Object item = null;
    		if (isEmpty())
    			throw new EmptyListException();
    		item = tail.getData();
    		if (head == tail)
    			head = tail = null;
    		else{
    			ListNode current = head;
    			while(current.getNext() != tail){
    				current = current.getNext();
    			}
    			tail = current;
    			current.setNext(null);
    		}
    		return item;
    	}
    	@Override
    	public String toString () {
    		String s = "[ ";
    		ListNode current = head;
    		while (current != null) {
    			s += current.getData() + " ";
    			current = current.getNext();
    		}
    		return s + "]";
    	}
     
    	public int count(){
    		if (isEmpty())
    			return 0;
    		int count = 0;
    		ListNode current = head;
    		while (current != null){
    			count++;
    			current = current.getNext();
    		}
    		return count;
    	}
     
    	public Object get(int n){
    		ListNode current = head;
    		if (n >= 1 && n <= count()){
    			for (int i = 1; i < n; i++){
    				current = current.getNext();
    			}
    			return current.getData();
    		}
    		else{
    			return null;
    		}
    	}
     
    	public Object remove(int n){
    		if (n < 1 || n > count())
    			return null;
    		ListNode current = head;
    		ListNode previous = head;
    		while (!current.getData().equals(get(n))){
    			current = current.getNext();
    		}
    		if ( n == 1){
    			removeFromHead();
    		}
    		else if (n == count()){
    			removeFromTail();
    		}
    		else{
    			while (!previous.getData().equals(get(n-1))){
    				previous = previous.getNext();
    			}
    			previous.setNext(current.getNext());
    		}
    		return current.getData();
    	}
     
    	public void add(int n, Object item){
    		ListNode current = head;
    		ListNode previous = head;
    		if (n <= 1){
    			addToHead(item);
    		}
    		else if (n > count()){
    			addToTail(item);
    		}
    		else{
    			while (!current.getData().equals(get(n))){
    				current = current.getNext();
    			}
    			while (!previous.getData().equals(get(n-1))){
    				previous = previous.getNext();
    			}
    			previous.setNext(new ListNode(item, current));
    		}
    	}
    } // class LinkedList

    Who can explain the logic of method?
    Thank you


  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: About add and remove method of ListNode, i don't understand....

    Have you tried playing computer with the code using a piece of paper to see what it does?

Similar Threads

  1. Help me to understand this
    By Madhushan in forum Java Theory & Questions
    Replies: 2
    Last Post: September 10th, 2011, 08:47 AM
  2. I can't understand the error.
    By Shivam24 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 21st, 2011, 01:56 PM
  3. How to remove argument from abstract method.
    By jainshasha in forum Object Oriented Programming
    Replies: 12
    Last Post: June 30th, 2011, 10:33 PM
  4. Can't understand why Interfaces are there
    By vortexnl in forum Object Oriented Programming
    Replies: 9
    Last Post: February 14th, 2011, 01:06 PM
  5. I need to fix the remove method on DoublyLinkedList.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 23rd, 2010, 09:20 PM

Tags for this Thread