Converting a Single Linked List to a Double Linked List

拜拜、爱过 提交于 2019-12-10 12:15:47

问题


I have here a single linked list for a program that makes a collage. This runs perfectly but I was wondering how to make it a double linked list. I really have no idea what a double linked is though or how to create one. Any help would be appreciated...

There are 3 classes.

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

回答1:


A doubly linked list is simply a linked list where every element has both next and prev mebers, pointing at the elements before and after it, not just the one after it in a single linked list.

so to convert your list to a doubly linked list, just change your node to be:

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

and when iterating the list, on each new node add a reference to the previous node.




回答2:


A doubly-linked list takes a singly-linked list one step further by also having a reference to the previous node, rather than just the next one.

I confess I am slightly confused by your code as it looks like you have a private class for Node and then another public class for it. To make this a doubly-linked list add another Node instance variable into your Node class which references the previous node, and then update this variable when you add in new nodes.




回答3:


You can convert Single linked list to Double linked list via a concept called XOR based linked list. The beauty of XOR truth table makes suitable for this use case.

Check this out : https://www.geeksforgeeks.org/xor-linked-list-a-memory-efficient-doubly-linked-list-set-1/



来源:https://stackoverflow.com/questions/9542154/converting-a-single-linked-list-to-a-double-linked-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!