2013-09-24 69 views
-3

我遇到了方法問題。沒有輸出是正確的,我不知道我是否正確使用雙鏈表。有人可以糾正或向我解釋我做錯了什麼嗎?java中的雙重鏈接列表

public class SlideList 
{ 

    private SlideNode head; 
    private SlideNode tail; 
    private SlideNode cursor; 

    public SlideList() 
    { 
     head = null; 
     tail = null; 
     cursor = null; 
    } 
    public void editCurrentSlide(String text, int lineNum) throws IllegalArgumentException 
    { 
     if(cursor==null||lineNum>5||lineNum<0) 
      throw new IllegalArgumentException("Slide does not exist"); 
     cursor.getSlide().setData(text, lineNum); 
    } 
    public boolean jumpToPosition(int position) 
    { 
     if(position<0 || position>listLength()) 
      return false; 
     resetCursor(); 
     for(int x=1;x<=position;x++) 
     { 
      cursor = cursor.getNext(); 
     } 
     return true; 
    } 
    public void displayCurrentSlide() throws EmptyListException 
    { 
     if(cursor==null) 
      throw new EmptyListException("There is no list"); 
     displaySlide(currentSlide()); 
    } 
    public void displaySlides(int start, int end) throws IllegalArgumentException 
    { 
     int length = listLength(); 
     resetCursor(); 
     if(start<1) 
      start=1; 
     if(end>length) 
      end = length; 
     if(start>length||length==0) 
      throw new IllegalArgumentException("There is no list"); 
     resetCursor(); 

     for(int i = 1; i < start; i++)//we can choose where to start the list with i 
     { 
      System.out.println("Inside loop1"); 
      cursor=cursor.getNext(); 

     } 
     System.out.println("start: " + start + " end: " + end); 
     for(int x = start; x<=end; x++) 
     { 

      System.out.println("Inside loop2"); 
      displaySlide(x); 
      if(start<end) 
       cursor = cursor.getNext(); 

     } 
     //cursor = cursor.getPrev(); 


    } 
    public void displaySlide(int x) 
    { 
     System.out.print("******************************"); 
     System.out.print(" " + x + " "); 
     System.out.print("******************************\n\n"); 
     System.out.print(cursor.getSlide().toString()); 
     System.out.print("\n******************************"); 
     System.out.print(" " + x + " "); 
     System.out.print("******************************\n"); 
    } 
    public int currentSlide() 
    { 
     SlideNode temp = new SlideNode(); 
     temp = head; 
     int position=1; 
     while(temp!=cursor) 
     { 
      temp = temp.getNext(); 
      position++; 
     } 
     return position; 
    } 
    public int listLength() 
    { 
     System.out.println("In Length of List metho"); 
     SlideNode nodePtr = head; 
     int answer = 1; 
     while(nodePtr != tail) 
     { 
      System.out.println("In Length of List loop"); 
      answer++; 
      nodePtr = nodePtr.getNext(); 
     } 
     return answer; 
    } 
    public boolean removeCurrentSlide() 
    { 
     if(cursor!=null) 
     { 
      cursor = cursor.getPrev(); 
      cursor.getNext().getNext().setPrev(cursor); 
      cursor.setNext(cursor.getNext().getNext()); 
      cursor = cursor.getNext(); 
      return true; 
     } 
     return false; 
    } 
    public void addAfterCurrent(Slide newSlide) 
    { 
     SlideNode node = new SlideNode(newSlide); 
     if(cursor==null) 
     { 
      addToEnd(newSlide); 
     } 
     else 
     { 
      node.setNext(cursor.getNext()); 
      node.setPrev(cursor); 
      node.getNext().setPrev(node); 
      cursor.setNext(node); 

      cursor = cursor.getNext(); 

      if(cursor.getNext() == null) 
      { 
       tail = cursor; 
      } 
     } 
    } 
    public void addToEnd(Slide newSlide) 
    { 
     SlideNode node = new SlideNode(newSlide); 
     if(isEmpty()) 
     { 
      System.out.println("Created list"); 
      head = node; 
      tail = node; 
      cursor = node; 
     } 
     else 
     { 
      while(cursor!=null) 
      { 
       cursor = cursor.getNext(); 
      } 
      tail.setNext(node); 
      node.setPrev(tail); 
      node.setNext(head); 
      tail = node; 
      cursor = node; 

     } 
     System.out.println("Out of addToEnd"); 
    } 
    public boolean isEmpty() 
    { 
     return (cursor==null); 
    } 
    public boolean moveForward() 
    { 
     if(cursor!=tail) 
     { 
      cursor = cursor.getNext(); 

      return true; 
     } 
     else 
      return false;  
    } 
    public boolean moveBack() 
    { 
     if(cursor!=head) 
     { 
      cursor = cursor.getPrev(); 

      return true; 
     } 
     else 
      return false;  
    } 
    public void resetCursor() 
    { 
     cursor = head; 
    } 

    public static void main(String[] args) 
    { 
     BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); 
     try 
     { 


      SlideList slides = new SlideList(); 

      System.out.println("AddToEnd"); 

      Slide newSlide = new Slide(); 
      newSlide.setData("1", "2", "3", "4", "5"); 
      slides.addToEnd(newSlide); 
      slides.displayCurrentSlide(); 

      System.out.println("AddToEnd"); 

      Slide newSlide1 = new Slide(); 
      newSlide1.setData("2", "3", "3", "4", "5"); 
      slides.addToEnd(newSlide1); 
      slides.displayCurrentSlide(); 

      System.out.println("AddToEnd"); 

      Slide newSlide2 = new Slide(); 
      newSlide2.setData("3", "4", "5", "6", "7"); 
      slides.addToEnd(newSlide2); 
      slides.displayCurrentSlide(); 

      /*System.out.println("AddToEnd/ResetCursor"); 

      Slide newSlide3 = new Slide(); 
      newSlide3.setData("4", "5", "6", "7", "8"); 
      slides.resetCursor(); 
      slides.addToEnd(newSlide3); 
      slides.displayCurrentSlide(); 
      System.out.println("Length of List"); 
      System.out.println(slides.listLength()); 
      System.out.println("Displaying All Slides"); 
      slides.displaySlides(1, 10);*/ 

     } 
     /*catch (IOException e) 
     { 
      System.out.println(e.getMessage()); 
     } */ 
     catch (EmptyListException e) 
     { 
      System.out.println(e.getMessage()); 
     } 


    } 


} 
+1

請考慮寫一個[SSCCE](http://sscce.org/)。 –

+0

你的輸出有什麼問題?請解釋您獲得的結果以及您期望得到的結果。還要考慮馬克艾略特的建議,否則沒有人會打擾通讀所有的代碼。 – nedaRM

+0

SO不適合你的課程作業 –

回答

0

首先,大多數鏈表將有多個項目可能訪問它,所以光標不在列表中的固有部分,但「列表訪問」的。

第二,如果你做錯了事,你需要知道你打算做什麼。我們可能能夠找到答案(正如我上面所做的那樣);但是,沒有人知道它是否是針對您的特定問題的正確答案。描述問題(舉例),我們將盡力描述爲什麼它表現出色。