Help With Sorting Char Arrays Using Pointers

This is a discussion on Help With Sorting Char Arrays Using Pointers within the C++ Programming forums, part of the General Programming Boards category; I have a program that I had to add three functions two. One was to insert a new node, one ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    2

    Help With Sorting Char Arrays Using Pointers

    I have a program that I had to add three functions two. One was to insert a new node, one was to delete a node, and the last needs to sort them alphabetically. I'm having all sorts of problems getting them to sort.

    I've done great in my programming classes until now. Pointers are really different, but I'm sure I will get them down.

    Code:
    struct Node;
    typedef Node *Node_ptr;
     
    struct Node
    {
                char word[MAX_WORD_LENGTH];
                Node_ptr ptr_to_next_node;
    };
    The code prompts the user to enter a word, which is stored in the char array. The user can keep entering words, which are stored in sequential nodes, until they enter the sentinel ".".

    Thanks for the help!
    Last edited by Charley; 04-13-2011 at 12:48 PM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Look, you're C++"ing", right?
    Then firstly get rid of that char array and replace it with a STL string.
    What you're trying to do is a linked list. Have they tought you about that?
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    2
    Quote Originally Posted by Sipher View Post
    Look, you're C++"ing", right?
    Then firstly get rid of that char array and replace it with a STL string.
    What you're trying to do is a linked list. Have they tought you about that?
    Yes, they have taught me linked lists. And I cannot replace the char array. That is required. I'm just having problems sorting the linked list into alphabetical order after the user has already entered all of his/her words.

  4. #4
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    458
    since a char is an ANSI code, you sort them like you sort numbers. So call up any sorting algorithm that you have for sorting numbers. Or be smarter and use the STL's list. It's a linked list, so it satisfies the requirements :-), then call sort from algorithm .
    Code:
    list<char> bleh; 
    // read into bleh; 
    sort(bleh.begin(),bleh.end());
    "All that we see or seem
    Is but a dream within a dream." - Poe

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char pointers and arrays
    By Scramble in forum C Programming
    Replies: 3
    Last Post: 06-30-2010, 05:17 AM
  2. Pointers & char arrays
    By lautarox in forum C Programming
    Replies: 11
    Last Post: 05-13-2009, 09:49 PM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  4. use of pointers with char arrays
    By txp200 in forum C Programming
    Replies: 6
    Last Post: 05-12-2004, 07:52 PM
  5. char arrays, pointers, etc, could use help badly
    By Nakeerb in forum C++ Programming
    Replies: 8
    Last Post: 10-11-2002, 11:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21