0

ここに、コラージュを作成するプログラムの単一のリンクリストがあります。これは完璧に動作しますが、二重リンクリストにする方法を考えていました。ダブルリンクとは何か、どうやって作成するのか、私にはよくわかりません。どんな助けもいただければ幸いです...

3つのクラスがあります。

class LinearCollage
{
  private Picture myArray[];
  private class Node
  {
    Picture data;
    Node pNext;
  };

  private Node pFirst;
  private Node pLast;

  private int nPictures;  
  private Picture clipboard;
  public LinearCollage()
  {
    pFirst = null;
    pLast = null;
    nPictures = 0;

  }
  public void addPictureAtEnd(Picture aPictureReference)
  {
    Node temp = new Node();
    temp.data = aPictureReference;
    temp.pNext = null;
    if( pLast == null )
    {
      pLast = temp;
      pFirst = temp;
    }
    else
    {
      pLast.pNext = temp;
      pLast = temp;
    }
    nPictures++;
  }
  public Picture makeCollage()
  {
    int collageHeight = 400;
    int collageWidth = 400;
    for( Node finger = pFirst; finger != null; finger = finger.pNext )
    {
      System.out.println("Process Picture " + finger.data);
    }
    Picture retval = new Picture(collageHeight,collageWidth);
    int i = 0;
    for( Node finger = pFirst; finger != null; finger = finger.pNext )
    {
      System.out.println("Process Picture " + finger.data);
      finger.data.compose(retval, 50*i, 50*i);
      i++;
    }
    return retval;
  }
  public void cutMiddle()
  {
    int cutIndex = nPictures-1;
    clipboard = myArray[cutIndex];
    for( int i = cutIndex; i < nPictures - 1; i++ )
    {
      myArray[i] = myArray[i + 1];
    }
    nPictures--;
  }
  public void cutEnd()
{
int cutIndex = nPictures;
clipboard = myArray[cutIndex];
for( int i = cutIndex; i<nPictures - 1; i++)
{
myArray[i] = myArray[i + 1];
}
nPictures--;
}
public void pasteEnd()
{
myArray[nPictures] = clipboard;
nPictures++;
}
  public boolean isFull()
  {
    return false;
  }
  public boolean isEmpty()
  {
    return nPictures == 0;
  }
}

import java.util.Scanner;
class LineCollageMaker
{
  public static void main(String a[])
  {
    LinearCollage myCollage;
    Scanner uiInput = new Scanner(System.in);


    myCollage = new LinearCollage();

    FileChooser.pickMediaPath();
    boolean inputting = true;
    while( inputting ) 
    {
      System.out.println("Another picture? Type Y if so.");
      String answer = uiInput.next();
      if(answer.equals("Y"))
      {
        Picture pin = new Picture(FileChooser.pickAFile());
        myCollage.addPictureAtEnd(pin);
      }
      else
      {
        inputting = false;
      }


    }
    Picture firstToShow = myCollage.makeCollage();
    firstToShow.show();
    //YOU Code the user inteface loop and dispatch to methods
    //of myCollage here..
    boolean done = false;
   while( !done )
    {
      System.out.println("MENU (CASE SENSITIVE!)");
      System.out.println("CM - cut middle and move it to the clipboard");
      System.out.println("PE - paste clipboard to end");
      System.out.println("CE - cut end and move it to clipboard");
      System.out.println("XX - stop running this program");
      String command = uiInput.next();
      if(command.equals("XX"))
        done = true;
      else if(command.equals("CM"))
      {
        if(myCollage.isEmpty())
        {
          System.out.println("Can't cut from an empty collage.");
        }
        else
        {
          myCollage.cutMiddle();
          myCollage.makeCollage().show();
        }
      }
      else if(command.equals("PE"))
      {
        if(myCollage.isFull())
        {
          System.out.println("Can't paste to an empty collage.");
        }
        else
        {
         myCollage.pasteEnd();
         myCollage.makeCollage().show();
        }
      }
        else if(command.equals("CE"))
      {
        if(myCollage.isEmpty())
        {
          System.out.println("Can't copy from an empty collage.");
        }
        else
        {
         myCollage.cutEnd();
         myCollage.makeCollage().show(); 
        }
      }
      else
        System.out.println("Unrecognized command. Try again.");
    }

  }
}



public class Node
{
  //le class variables
  private Picture myPic;
  private Node next;

  //le constructors  
  public Node(Picture heldPic)
  {
    myPic=heldPic;
    next=null;
  }

  public void setNext(Node nextOne)
  {
    this.next=nextOne;
  }
  public Node getNext()
  {
   return this.next; 
  }
  public Picture getPicture()
  {
   return this.myPic; 
  }


  //le methods
  public void drawFromMeOn(Picture bg)
  {
   Node current;
   int currentX=0, currentY=bg.getHeight()-1;

   current = this;
   while (current != null)
   {
    current.drawMeOn(bg,currentX, currentY);
    currentX = currentX + current.getPicture().getWidth();
    current=current.getNext();
   }
  }

 private void drawMeOn(Picture bg, int left, int bottom)
 {
  this.getPicture().blueScreen(bg, left, bottom-this.getPicture().getHeight());
 }
}
4

3 に答える 3

3

二重リンクリストは、すべての要素に次のメンバーと前のメンバーの両方があり、単一のリンクリスト内の後続の要素だけでなく、その前後の要素を指す単純なリンクリストです。

したがって、リストを二重リンクリストに変換するには、ノードを次のように変更します。

private class Node
  {
    Picture data;
    Node pNext;
    Node pPrev;
  };

リストを反復処理するときは、新しいノードごとに前のノードへの参照を追加します。

于 2012-03-03T00:21:12.920 に答える
1

二重リンクリストは、次のノードだけでなく、前のノードへの参照も持つことで、単一リンクリストをさらに一歩進めます。

Nodeのプライベートクラスがあり、次に別のパブリッククラスがあるように見えるので、コードに少し混乱していることを告白します。これを二重リンクリストにするには、前のノードを参照する別のNodeインスタンス変数をNodeクラスに追加し、新しいノードを追加するときにこの変数を更新します。

于 2012-03-03T00:20:44.853 に答える