Thread: How to sort linked list alphabetically

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    45

    How to sort linked list alphabetically

    Hi I want to input names into a linked list and sort them as they go in. So if the name Google is already in the list, then Yahoo should go behind it. And if Firefox is being inputted into the list then it should be at the top of the list.
    I have char name[15] declared. However I'm not sure if we use strcmp to compare the names or something else.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How do you alphabetize anything? Look at the first one, see if what you have comes before, after, or is it. Then put it in the right place.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by chickenlittle View Post
    I have char name[15] declared. However I'm not sure if we use strcmp to compare the names or something else.
    Well, you are already starting out wrong. This is C++, not C, you should not be using char arrays for anything. Are you allowed to use the STL or is this a school project? If this is a personal project and you are allowed to use the STL take a look at my response in this thread. If you meant to post this in the C board, say so.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    45
    Nope this is a school project and I have to use char name[15]. It is c++ so this is the right place. Not very sure how to compare the names though.

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by chickenlittle View Post
    Nope this is a school project and I have to use char name[15]. It is c++ so this is the right place. Not very sure how to compare the names though.
    Let's be clear, using char arrays for strings is NOT C++, it is C with cout/cin. Post what you have so far and we will go from there even though step 1 should be to attend a school that knows what the heck they are doing.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Many schools start by teaching C-style C++ and move up. I don't really agree with that method, but it doesn't automatically make them bad. There are good schools that uses this approach.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Elysia View Post
    Many schools start by teaching C-style C++ and move up. I don't really agree with that method, but it doesn't automatically make them bad. There are good schools that uses this approach.
    Fair enough, it just drives me nuts. It makes problems like these even more burdensome, due to everything else you have to worry about with string processing, and detracts from the main problem at hand, e.g. how to add a linked list node in order.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I am inclined to agree.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    "Many schools start by teaching C-style C++ and" ... then send you on your merry way. - FTFY!

    Do be careful with statements such as "should not be using char arrays for anything" though. There's such a thing as going overboard.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by iMalc View Post
    "Many schools start by teaching C-style C++ and" ... then send you on your merry way. - FTFY!

    Do be careful with statements such as "should not be using char arrays for anything" though. There's such a thing as going overboard.
    Haha....fair enough. I was just trying to drive a point home.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-20-2010, 08:09 PM
  2. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  3. sort my linked list alphabetically
    By alfd6z in forum C Programming
    Replies: 2
    Last Post: 12-14-2002, 01:33 PM
  4. Inserting alphabetically into a Linked List
    By EDL in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2002, 06:51 AM
  5. sort a linked list ???
    By goran in forum C Programming
    Replies: 2
    Last Post: 11-13-2001, 08:18 AM