4 Replies - 821 Views - Last Post: 28 February 2012 - 07:39 PM Rate Topic: -----

#1 thatguybob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 14-February 12

Double linked list?

Posted 28 February 2012 - 06:09 PM

I'm trying to create a double linked list, so i can go back and forth between nodes.

This is what i've got so far:

class LinearCollage
{
  private class Link
  {
    Picture data;
    Link next;
    Link prev;
    Link cursor;
  };
  private Link pFirst;
  private Link pLast;
  
  private int nPictures;
  private Picture clipboard;
  public LinearCollage()
  {
    pFirst = null;
    pLast = null;
    nPictures = 0;
    
  }
  public void InsertObjectEnd(Picture object)
  {
    Link temp = new Link();
    temp.data = object;
    temp.prev = null;
    temp.next = null; 
    temp.cursor = null;
    if( pLast == null )
    {
      pLast = temp;
      pFirst = temp;
    }
    else
    {
      pLast.next = temp;
      temp.prev = pLast;
      pLast = temp;
    }
    
    nPictures++;
  }


I've got no way of testing it currently, but this should work right?

Is This A Good Question/Topic? 0
  • +

Replies To: Double linked list?

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1009
  • View blog
  • Posts: 2,186
  • Joined: 05-May 05

Re: Double linked list?

Posted 28 February 2012 - 06:13 PM

Quote

I've got no way of testing it currently, but this should work right?


Yes. You should create a print function so that you can test it...

void print() {
    Link curr = pFirst;
    while (curr != null) {
        System.out.println(curr.data);
        curr = curr.next;
    }
}


Was This Post Helpful? 0
  • +
  • -

#3 thatguybob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 14-February 12

Re: Double linked list?

Posted 28 February 2012 - 06:59 PM

View Postblackcompe, on 28 February 2012 - 06:13 PM, said:

Quote

I've got no way of testing it currently, but this should work right?


Yes. You should create a print function so that you can test it...

void print() {
    Link curr = pFirst;
    while (curr != null) {
        System.out.println(curr.data);
        curr = curr.next;
    }
}


Ok so let's say I want to move through this list, only moving by one each time the move method is called, would I need a loop?

For example :

  public void f()
  {
    Link pointer = pFirst;
    pointer = pointer.next;
    System.out.println("This data is " + pointer.data + "jpg");
  }



But the problem with what i've got up there is that once it's run once, it won't move to the next picture, it stays at the first picture it moved to (if it started like 1 - 2 - 3, it stays at 2 regardless of how many times i run f()). Should I be using a loop for something like this?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8022
  • View blog
  • Posts: 31,133
  • Joined: 06-March 08

Re: Double linked list?

Posted 28 February 2012 - 07:10 PM

And no need to have a cursor in your Node object instance variables
You might need a cursor in some methods, depending the way you will organize your code, but it should be a local variable to the method that required it. If you have a LinkedList with 10,0000 Node no real resons to have 10,000 unused cursor Node.
Was This Post Helpful? 0
  • +
  • -

#5 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1009
  • View blog
  • Posts: 2,186
  • Joined: 05-May 05

Re: Double linked list?

Posted 28 February 2012 - 07:39 PM

Quote

But the problem with what i've got up there is that once it's run once, it won't move to the next picture, it stays at the first picture it moved to (if it started like 1 - 2 - 3, it stays at 2 regardless of how many times i run f()). Should I be using a loop for something like this?


Yes. I just showed you how to traverse a list. Did you try inserting a few nodes and running the function? Your f() function shows the same thing no matter how much you call it because you start the pointer at the head of the list each time:

 Link pointer = pFirst;


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1