• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

JAVA question

ludachaz

Limp Gawd
Joined
Feb 3, 2005
Messages
208
Hey guys,

I'm writing a program, it is a very basic text editor.
For the datastructure I am using a double linked list with three pointers, first, last and current. The current pointed has a method that makes the used be able to move it to either first, last, or up or down one position on the linked list.

=deleted original post, will post a more clear verssion=
 
I think you'll need to provide more information in order for us to help. However, my feeling is that setting the current pointer doesn't do much. Looking at what getPosition() checks would provide enlightenment on how to change the position of the pointer.

also, please use the [ code ] and [ /code ] tags to make you code more reasonable. (no spaces between the brackets).
 
We'll definitely need the structure of your linked list (class definition)
 
ok, so in the main class i have been running some tests and it seems that some links are lost during the insert function. So far i have only test FIRST and LAST. Thing is I have run a trace of those two on paper numerous times, and tried finding a problem and it all seems fine. Basically it works if you keep adding LASTs, but as soon as you add a FIRST the count (determined by getposition) restarts.


Code:
class dllist {

   public enum position {FIRST, PREVIOUS, FOLLOWING, LAST};

   public class node {
      String item;
      node prev;
      node next;
   };

   public node first = null;
   public node current = null;
   public node last = null;
   public int currentposition = 0;

   public void setposition (position pos) {
      throw new UnsupportedOperationException();
   };

   public boolean isempty () {
       if(first==null) return true;
       return false;
   };

   public String getitem () {
       return current.item;
   };

   public int getposition () {
       int counter=0;
       node itor = first;
       if(first!=last){
       while(itor.next!=null){
	   if(itor==current)break;
	   counter++;
	   itor=itor.next;
       }}
       return counter;
   };

   public void delete () {
      throw new UnsupportedOperationException();
   };

   public void insert (String item, position pos) {
       node temp = new node();
       temp.next=null;
       temp.prev=null;
       temp.item=item;
       if(first==null){
	   this.current=temp;
	   this.first=temp;
	   this.last=temp;
       }else{
	   switch(pos){
	   case FIRST:
	       temp.next=first;
	       first.prev=temp;
	       first=temp;
	       temp.prev=null;
	       current=temp;
	   case LAST:
	       temp.prev=last;
	       last.next=temp;
	       last=temp;
	       temp.next=null;
	       current=last;
	   case FOLLOWING:
	       if(current.next!=null){
		   current.next.prev=temp;
		   temp.next=current.next;
		   temp.prev=current;
		   current.next=temp;
		   current=temp;
	       }else{
		   current.next=temp;
		   temp.prev=current;
		   current=temp;
		   last=temp;
	       }
	   case PREVIOUS:
	       if(current.prev!=null){
		   temp.next=current;
		   temp.prev=current.prev;
		   current.prev.next=temp;
		   current.prev=temp;
		   temp=current;
	       }else{
		   current.prev=temp;
		   temp.next=current;
		   current=temp;
		   first=temp;
	       }
	   }
	   currentposition=getposition();
       }
   };

};
 
frickin HF database errors, i managed to triple post

at the end of each of your 'case' blocks, you need a break statement, otherwise it continues through into the code for the next case statement which you really don't want here.

switch(pos){
case FIRST:
//several lines of code here
break;
case LAST:
//mode code
break;
....continues...
}
 
i knew it was something stupid like that :-(

but i do have to say THANK YOU. cause i was about to rip my hair out.
 
Back
Top