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

在编写任何代码之前,我通常会做一些午睡草图。他们为我省去了很多麻烦。

好的,但是对于您的问题:

您的插入仅在第一个元素之后插入。如果您的当前指向末尾,那么当您插入一个新元素时,它应该链接到当前,而不是第一个。并且 current 应该始终指向第一个(使列表循环),即使只有一个元素。

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 回答