• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

need help with linked list

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I am having trouble understanding how d1.next = d2.next, d2.next = d1, and n.next = d2 work?

I don't understand what the variables on the left are referencing when they are moving to the next node.

If I have a linked list like n -> [1| ] -> [2| ] -> [3| ] -> [4| ] -> [5| ] -> [6|null ]

1) d1 points to [2| ]
2) d2 points to [3| ]
3) ??? d2.next points to [4| ], but d1.next points to [3| ], I don't know what d1.next = d2.next would be?


 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

There have been several questions about linked lists in the last week or so. Have a look at this thread, which has a diagram of a linked list in. It’s the thread where I thought it was a doubly‑linked list and it was only singly‑linked. See whether that diagram helps. Draw it on paper, with a large gap between nodes, then insert a node in the gap, with its arrows in a different colour. See whether that helps you understand the workings. You will also realise that things to do with linked lists are easier if you can divide them into smaller parts. Similary for diagrams. Start with a diagram for singly‑linked lists before you try anything for doubly‑linked lists.
reply
    Bookmark Topic Watch Topic
  • New Topic