0

Javaで循環リンクリストを作成しようとしています。正しく挿入されていると思いますが、削除または表示が正しく機能しません。これは私のコードです。

public class Link
{
  public int data; 
  public Link next;

 public Link(int d)
  {
     data = d;  //store data
     next = null; //set next Link to newLink
  }
}

   public class IntListCircularCount
  {
  private Link first;//this always points to the first link.
  private Link current=null;
  private int count =0;

  public IntListCircularCount()
  {
     first = null;
  }

  public boolean isEmpty()
  {
     return (first==null);
  }

  public Link getFirst()
  {
     return first;
  }   

  public void insert(int n)
  {
     if(count == 0)
     {
        Link newLink = new Link(n);
        first=newLink;
        count++;
        current = first;       
     }
     else if(count>0)
     {
        Link newLink = new Link(n);
        first.next = newLink;
        newLink.next = first;
        count++;
        current = first.next;
     }
  }

  public void display(int width)
  {
     if(isEmpty())
        System.out.printf("%" + width + "s", "--");
     else if(count ==1)
        System.out.printf("%" + width + "d",first.data);
     else if(!isEmpty() && first.next !=first)
     {
        while (first !=current)
        {       
           System.out.printf("%" + width + "d", current.data);
           current = current.next;
        }
     }   
  }

  public void delete()
  {
     if(count==0)
     {
        first=null;
     }
     else if(count==1)
     {            
        first = first.next;
     }
     else if(count>1)
     {
        current.next=first.next;
        first = first.next;
        count--;
     }
    }
 }


  public class IntListUser
 {
  public static void main (String[] args)
  {
     final int n =5;//there will be n Links
     final int w=5; //field width for display
     IntListCircularCount list = new IntListCircularCount();
     for(int i=1; i<=n; i++)
     {
        list.display(w);
        list.insert(10*i);
     }
     list.display(w);


     System.out.println("  -------------end of inserting ----------------");
     list.delete();
     list.display(w);
         list.delete();
     list.display(w);
        list.delete();
     list.display(w);
        list.delete();
     list.display(w);
        list.delete();
     list.display(w);
  }
  }
4

1 に答える 1

0

私は通常、コードを書く前に仮眠スケッチをいくつか行います。彼らは私に多くの手間を省きます。

わかりましたが、あなたの質問のために:

挿入は、最初の要素の後にのみ挿入されます。現在が最後を指している場合、新しい要素を挿入するときに、最初ではなく現在にリンクする必要があります。また、要素が 1 つだけであっても、 current は常に first を指す必要があります (リストを循環させるため)。

public void insert(int n)
  {
  if(count == 0)
  {
     Link newLink = new Link(n);
     first=newLink;
     count++;
     current = first;
     current.next = first;     
  }
  else
  {
     Link newLink = new Link(n);
     current.next = newLink;
     newLink.next = first;
     count++;
     current = current.next;
  }
}

また、ディスプレイは最初から現在まで表示する必要がありますが、最初と現在を失うべきではありません。

public void display(int width)
{
   Link display_me = first;
   if(isEmpty())
      System.out.printf("%" + width + "s", "--");
   else 
   {
      Link display_me = first;
      do {       
         System.out.printf("%" + width + "d", current.data);
         display_me= display_me.next;
      } while (first != display_me);

   }   
}

削除に関しては、最初のものを削除するのか、現在のものを削除するのかわかりません。

お役に立てれば。

于 2011-05-27T15:55:23.860 に答える