6 Replies - 726 Views - Last Post: 09 March 2013 - 03:02 PM Rate Topic: -----

#1 RozenKristal  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 380
  • Joined: 29-September 12

Methods to create an empty doubly linked list.

Posted 08 March 2013 - 11:35 PM

So I will try to sum my problem up, if it is not clear, please tell me:

So I have an array of char contain a number. So let say I have a string that is 56789.
I change that string to array of char. I want to put it in a a doubly linked list, with head contain 5 and tail contain 9, everything in between.

Node class just has 3 fields which are char data, prev and next.

I am not sure how to put the value in the list using a for loop, since I know the size of the number. But now I think this way. First I would make a singly linked list, and setNext forward, then start again from tail to setPrev, then put the values in the list last.
Are there other ways to do this? Thank guys in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Methods to create an empty doubly linked list.

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Methods to create an empty doubly linked list.

Posted 09 March 2013 - 01:43 AM

Show the code for your doubly linked list.
Was This Post Helpful? 0
  • +
  • -

#3 RozenKristal  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 380
  • Joined: 29-September 12

Re: Methods to create an empty doubly linked list.

Posted 09 March 2013 - 02:37 PM

Oh Greg, I have not encounter any bugs or so, just simply asking on what other alternative exist besides what I said.
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Methods to create an empty doubly linked list.

Posted 09 March 2013 - 02:45 PM

I'm still confused by your thought of starting with a singly-linked list, that's why I asked to see your code.

No, you wouldn't start with one kind of a linked list and change it to another. And for a linked list, single- or double-, there can be methods that add a new node to the end, to the beginning, in sorted order, or inserted randomly somewhere between the head and the tail.

Seeing your list code and the methods you'd written to add nodes would have clarified your question about adding new nodes.
Was This Post Helpful? 1
  • +
  • -

#5 pbl  Icon User is offline

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

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Methods to create an empty doubly linked list.

Posted 09 March 2013 - 02:48 PM

If your lList has a method to append to it
it is quite easy to pass through the array appending a Node for each of the char
Was This Post Helpful? 1
  • +
  • -

#6 RozenKristal  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 380
  • Joined: 29-September 12

Re: Methods to create an empty doubly linked list.

Posted 09 March 2013 - 02:57 PM

View PostGregBrannon, on 09 March 2013 - 02:45 PM, said:

I'm still confused by your thought of starting with a singly-linked list, that's why I asked to see your code.

No, you wouldn't start with one kind of a linked list and change it to another. And for a linked list, single- or double-, there can be methods that add a new node to the end, to the beginning, in sorted order, or inserted randomly somewhere between the head and the tail.

Seeing your list code and the methods you'd written to add nodes would have clarified your question about adding new nodes.



My mind 's a mess recently so I kind of forget on how to insert node in a dlinked list. What I said above means insert node into the list and establish the connection between nodes with the next field first, then traverse the node again, and connect the nodes with the prev field. But scratch that, I thinking weird stuffs. When I get back on my comp, def upload the code.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Methods to create an empty doubly linked list.

Posted 09 March 2013 - 03:02 PM

Sure that the appendNode() method of a SingleList has the responsabilty to update the forward and backward pointers of the nodes involved in the operation mainly the firt/last node and the new inserted node.

You can implement 2 different versions (or even borth versions) of the insertNode() method. One that receives a Node as parameter and a one that receive the data as parameter. In that case the method creates the node and then call the previous one.

This post has been edited by pbl: 09 March 2013 - 04:30 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1