简体   繁体   English

循环单链表

[英]Circular Single Linked List

I have been trying to make a Circular Linked List in Java. 我一直在尝试用Java创建循环链接列表。 I believe that I am inserting properly, but I cannot get my delete or display to work properly. 我相信自己已正确插入,但无法删除或显示其正常工作。 This is my code. 这是我的代码。

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);
  }
  }

I usually do some napking sketches before writing any code. 我通常在编写任何代码之前先做一些草稿草图。 They save me a lot of trouble. 他们为我省了很多麻烦。

Ok, but for your question: 好的,但是您的问题是:

Your insert is only inserting after the first element. 您的插入仅在第一个元素之后插入。 If your current points to the end, then when you insert a new element it should be linked to current, not to first. 如果当前指向末尾,则在插入新元素时应将其链接到当前,而不是第一个。 And current should always point to first (to make the list circular), even with just one element. 即使只有一个元素,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;
  }
}

Also, your display needs to display from first to current, but you shouldn't lost first and current. 另外,您的显示需要从第一显示到最新显示,但您不应失去第一和最新显示。

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);

   }   
}

As for the delete, I don't know if you want to delete the first or the current. 至于删除,我不知道您要删除第一个还是当前的。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM