Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old September 25th, 2007, 02:34 PM
darken4life darken4life is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 127 darken4life User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 h 41 m 12 sec
Reputation Power: 2
Linked List Help

Im suppose to implement these methods but Im stuck!


Code:
import java.util.*;

// doubly linked list, uses a cursor and no sentinels.

public class Bxxxx {
   private DNode cursor;
   private int size;

   public Bxxxx() {
     size = 0; 
     cursor = null;
   }

   public int size() {return size;}
   public boolean isEmpty() {return size == 0;}
   public void advance() {
	   cursor = cursor.getNext();
	   	if(cursor == null)
	   		throw new RuntimeException("Cannot Advance, Empty List");
   }
   public void goBack() {
	   cursor = cursor.getPrev();
	   	if(cursor == null)
	   		throw new RuntimeException("Cannot go back, Empty List");
   }
   public void addBefore(Object d) {
	   if(cursor == null){
		   DNode n = new DNode(d,null,cursor);
		   cursor = n;
	   }
	   else if(cursor != null){
		   DNode n = new DNode(d,null,cursor);
		   cursor.setPrev(n);
		   cursor = n;
	   }
	   else if(cursor.getPrev() != null){
		   DNode w = cursor.getPrev();
		   DNode n = new DNode(d,w,cursor);
		   cursor.setPrev(n);
		   w.setNext(n);
		   cursor = n;
	   }
	   size++;
   }
   public void addAfter(Object d) {
	   if(cursor == null){
		   DNode n = new DNode(d,cursor,null);
		   cursor = n;
	   }
	   else if(cursor != null){
		   DNode n = new DNode(d,cursor,null);
		   cursor.setNext(n);
		   cursor = n;
	   }
	   else if(cursor.getNext() != null){
		   DNode w = cursor.getNext();
		   DNode n = new DNode(d,cursor,w);
		   cursor.setNext(n);
		   w.setPrev(n);
		   cursor = n;
	   }
	   size++;
   }
   public Object remove() {
	   if(cursor == null)
		   throw new RuntimeException("Cannot remove, cursor is empty");
	   if(cursor.getPrev() == null)
		   throw new RuntimeException("Cannot remove, cursor prev empty");
	   if(cursor.getNext() == null)
		   throw new RuntimeException("Cannot remove, cursor next empty");
	   
	   DNode w = cursor;
	   DNode m = cursor.getPrev();
	   DNode n = cursor.getNext();
	   m.setNext(n);
	   n.setPrev(m);
	   size--;
	   
	   return w.getData();
   }
 
// LinkedList testing methods:
   public String toString() {
      String ans = "";
      DNode n = cursor;
      ans += "(^^)<-->";
      for (int i = 0; i < size; i++, n = n.getNext()) 
         ans += (n.getData() + "<-->");
      ans += "(^^)";
      return ans;
   }

   public static void main(String args[]) {
      Bxxxx l = new Bxxxx();
      DNode d = null;
      boolean done = false;
      while (!done) {
         try {
           Scanner s = new Scanner(System.in);
           System.out.println( l);
           System.out.println("\ncmds are B A R + - Q: >>");
           String cmd = s.next();
           String entry = null;
           char command = cmd.charAt(0);
           if (command == 'B' || command == 'A') entry = s.next();
           switch (cmd.charAt(0)) {
              case 'Q': done = true; break; 
              case 'R': l.remove(); break;
              case '+': l.advance(); break;
              case '-': l.goBack(); break;
              case 'B': l.addBefore(entry); break;
              case 'A': l.addAfter(entry); break;
           }
        } catch (Exception e) {
           System.out.println("Error " + e.toString());
        }
      }   
   }
}

class DNode {
   private Object data;
   private DNode prev, next;
   
   public DNode(Object d, DNode p, DNode n) {
     data = d; next = n; prev = p; }
   public Object getData() { return data; }
   public DNode getNext() { return next; }
   public DNode getPrev() { return prev; }
   public void setData(Object d) { data = d; }
   public void setNext(DNode n) { next = n; }
   public void setPrev(DNode p) { prev = p; }
}



Everything else is supplied by the professor so is just the
methods size, isEmpty, Advance, goBack, Addbefore, Addafter, and remove.

The problem is that addbefore works ok but when I advance the cursor, it will give me an exception because say I have 3 elements in the list and I advance the Tostring method will run 3 times because size is still 3 but since I advance theres only 2 elements to print.

2nd problem is Addafter method, adding works fine but the thing is since the cursor moves to the new one, the next node will always be null and the ToString method prints n.getNext() and it is null.

Thanks for reading.

Here is what my professor's instructions is
Code:
size()   to return the size of the list
isEmpty() to answer whether the list is empty
advance()   moves the cursor 1 node forward
goBack()    moves the cursor 1 node backward
addBefore   creates and inserts a new node before the cursor,
            the cursor is moved to the newly created node.
addAfter    creates and inserts a new node after the cursor,
            the cursor is moved to the newly created node.
remove      Removes the Node at the cursor,
            the cursor is moved to the next node.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Linked List Help


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway