⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 intcircularlinkedlist.java

📁 IntCircularLinkedList code
💻 JAVA
字号:
/**
 * IntCircularLinkedList.java
 * @Hao Chen
 * @0236407
 */

/**
 *A circular linked list is like an ordinary linked list except that the last node,
 *instead of having a null link, has a link back to the first node on the list.
 *
 */

class IntCircularLinkedList {


	// This is the only allowed instance variable.
	protected IntNode head;
/**
  * This constructor takes as input an integer n and returns a circular linked list whose
  * nodes have values 1,2,...,n with head pointing to the Node with value 1. N is a positive integer.
  *
  * @param The input number which is the total values of runing times.
  *
  */
	  IntCircularLinkedList(int n){

          head = new IntNode(1,null);
          IntNode temp= head;
		  for(int i=2; i<=n; i++){
		    temp.link= new IntNode(i,null);
		    temp = temp.link;
	      }
	      temp.link = head;
	  }
/**
  *This constructor takes as input an array of integers and returns a circular linked list having
  *the values a[0], a[1], ..., a[m-1] (m = a.length) with head pointing to the node with value a[0].
  *
  * @param The input array.
  *
  */
      IntCircularLinkedList(int list[]){

		  head = new IntNode(list[0],null);
		  IntNode temp = head;
		  for(int i=2; i<=list.length;i++){
			  temp.link=new IntNode(list[i-1],null);
			  temp=temp.link;
	      }
	      temp.link=head;
	  }
	// Put your constructors here.
/**
  *This method returns a string containing the value of each node exactly once, starting at the head.
  *
  * @return the sum of all the output strings.
  *
  */
      public String toString(){
		  IntNode temp=head;
		  String sum=temp.val+" ";
         do{
			 temp= temp.link;
			 sum+=temp.val+" ";
		 }while(temp.link!=head);

       return sum;
	}
	// Put your toString method here.
/**
  *This method takes as input a non-negative int m and returns the integer value of the Node
  *obtained by following m links from the head (thus goAround(0) returns the value of the head node).
  *It will go around and around the list until it has followed the correct number of
  *links.
  *
  * @return the returns the value of the head node
  *
  */
        public int goAround(int k){
         IntNode temp=head;

         for(int i=0; i<k;i++){
			 temp=temp.link;
	     }
	     return temp.val;



    }
	// Put the goAround method here.

	// Do not modify this main.
/**
  * Main method give the main input.
  *
  * @param The input string stored as an array.
  */
	public static void main ( String[] notUsed ) {
		int[] b = {3,1,4,1,5,9,2,6,5,3};
		IntCircularLinkedList listA = new IntCircularLinkedList( 10 );
		IntCircularLinkedList listB = new IntCircularLinkedList( b );
		System.out.println( listA );
		System.out.println( listB );
		System.out.println( listA.goAround( 88 ) );
		System.out.println( listB.goAround( 22 ) );
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -