CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Join Date
    Jul 2003
    Posts
    108

    Smile LinkedList for order

    Hi,

    How sure can I be about the order of the elements in a LinkedList in a for each loop? Does it goes from last to first?

    Thanks

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: LinkedList for order

    For the quick answers about how a class works, write a small simple program, execute it and see.
    If you have problems or questions, post the code here with the results and your questions.
    Norm

  3. #3
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: LinkedList for order

    Quote Originally Posted by HackmanC View Post
    How sure can I be about the order of the elements in a LinkedList in a for each loop?
    Traversing a LinkedList in a particular direction will always give you the same order of elements unless you explicitly modify the order of elements.

    Does it goes from last to first?
    Yes, if you traverse it in that order... java.util.LinkedList is a doubly-linked list, so can be traversed in either direction. It is also a Queue and a Deque - see the API docs.

    It is not knowledge, but the act of learning, not possession, but the act of getting there which generates the greatest satisfaction...
    F. Gauss
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  4. #4
    Join Date
    Jul 2003
    Posts
    108

    Smile Re: LinkedList for order

    Hi,

    Thanks Norm and dlorde, I already have a program installed and working in testing, in a testing environment, but this question is very important to the correct behavior of the application.

    I guess dlorde already answer my question but just to be sure.

    Example:
    Pseudocode:

    Code:
    list.add("A");
    list.add("B");
    list.remove("A");
    list.add("C");
    
    for (Item item : list) {
      System.out.println(item);
    }
    In this case, its positive I will get the items in order? Its the for loop normal behavior to get the elements in order always? Why I ask this? Because I couldn't find some site where its explained clearly, even when for now it doesn't represent a problem.

    Thanks again,
    Good Luck
    HackmanC

  5. #5
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: LinkedList for order

    Quote Originally Posted by HackmanC View Post
    ... I couldn't find some site where its explained clearly, even when for now it doesn't represent a problem.
    Really? The API docs for List say "An ordered collection (also known as a sequence)". Wikipedia sums it up in the first line: "a linked list is... a data structure that consists of a sequence of nodes each of which contains a reference (i.e., a link) to the next node in the sequence".

    If you can find a simple way to traverse that out of sequence, please let us know.

    The purpose of computing is insight, not numbers...
    R. Hamming
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  6. #6
    Join Date
    Jul 2003
    Posts
    108

    Smile Re: LinkedList for order

    Hi,

    Quote Originally Posted by dlorde View Post
    Really? The API docs for List say "An ordered collection (also known as a sequence)". Wikipedia sums it up in the first line: "a linked list is... a data structure that consists of a sequence of nodes each of which contains a reference (i.e., a link) to the next node in the sequence".

    If you can find a simple way to traverse that out of sequence, please let us know.
    ...
    Well, thank you dlorde, I got confused because there are other implementations that doesn't guarantee the order of the elements. But that's clear for me now.

    Thanks again,

  7. #7
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: LinkedList for order

    Quote Originally Posted by HackmanC View Post
    Well, thank you dlorde, I got confused because there are other implementations that doesn't guarantee the order of the elements. But that's clear for me now.
    You know of implementations of Linked Lists that don't guarantee the order of the elements?

    Sounds odd - I'm curious to know how they'd work; please post the relevant links.

    Before software can be reusable it first has to be usable...
    R. Johnson
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  8. #8
    Join Date
    Jul 2003
    Posts
    108

    Smile Re: LinkedList for order

    Hi,

    I didn't said other implementations of Linked List, and even I didn't said Java. I got confused, I read somewhere that some Collections doesn't guarantee the order, that's why I ask, to be sure and someone with more knowledge told me the right direction, like you already did.

    Well, thanks again.
    Good Luck
    HackmanC

  9. #9
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: LinkedList for order

    Quote Originally Posted by HackmanC View Post
    I didn't said other implementations of Linked List, and even I didn't said Java.
    Oh, OK - sorry, I thought that's what we were talking about - all the thread posts were about LinkedList and this is a Java forum...

    I got confused, I read somewhere that some Collections doesn't guarantee the order.
    Collection classes like HashMap don't guarantee order, but they're not sequential, ordered collections. A LinkedList clearly is - ordering is implicit in the links between nodes, and the API docs show it is a Queue and can be used as a stack. The API docs should be the first point of reference when trying to establish how a class behaves - although sometimes it takes a while to pin it down

    Computers are to computing as instruments are to music. Software is the score whose interpretations amplifies our reach and lifts our spirits. Leonardo da Vinci called music the shaping of the invisible, and his phrase is even more apt as a description of software...
    A. Kay
    Last edited by dlorde; June 6th, 2011 at 06:15 AM.
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  10. #10
    Join Date
    Jul 2003
    Posts
    108

    Smile Re: LinkedList for order

    Hi,

    Quote Originally Posted by dlorde View Post
    Oh, OK - sorry, I thought that's what we were talking about - all the thread posts were about LinkedList and this is a Java forum... ...
    You see, that is where I got confused. You right, I read somewhere some collection in some language doesn't provides that, I searched the API and even when thats was implicit I couldn't interpret that.

    Quote Originally Posted by dlorde View Post
    ... The API docs should be the first point of reference when trying to establish how a class behaves - although sometimes it takes a while to pin it down ...
    Your right, I remember now, Collections iterator, it doesn't guarantee the order, unless an implementing class provides a guarantee. And Linked Lists, by nature, provides that.

    I really appreciate all your help, like you said, sometimes it takes a while to pin it down, I will be aware of that next time.

    Thanks,
    Last edited by HackmanC; June 6th, 2011 at 08:02 PM.

  11. #11
    Join Date
    May 2009
    Posts
    2,413

    Re: LinkedList for order

    Quote Originally Posted by Norm View Post
    For the quick answers about how a class works, write a small simple program, execute it and see.
    That's a bad idea.

    To learn about the fundamental properties of a class one should study the documentation.

  12. #12
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: LinkedList for order

    You only know something if you can write working code.
    Norm

  13. #13
    Join Date
    May 2009
    Posts
    2,413

    Re: LinkedList for order

    Quote Originally Posted by Norm View Post
    You only know something if you can write working code.
    This idea that you can test code into working is the root of much evil. Code should be designed to work, not tested until it seems to work.

    The OP is absolutely on the right track here not being satisfied with code that's mere working. I suggest you follow his example instead of ignorantly advocating a sloppy and dangerous practice.

  14. #14
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: LinkedList for order

    Not my idea at all.
    Read the API doc, get some ideas of what you need, write a simple test program to get the technique down, copy the working code/techniques into your program.
    Norm

  15. #15
    Join Date
    May 2009
    Posts
    2,413

    Re: LinkedList for order

    Quote Originally Posted by Norm View Post
    Read the API doc, get some ideas of what you need, write a simple test program to get the technique down, copy the working code/techniques into your program.
    That's better. Especially the part where you study the standard Java API to learn about the fundamental properties and limitations of a class.

    Never rely on guesswork and testing and what seems to work. Don't even rely on how a class is currently implemented.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured