Thread: Linked List problem

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    67

    Linked List problem

    I know absolutely I am a noob in C++, and much worse thing happened: My professor can't finish our book in time, so he rushed the last chapters. For linked list, I can know the basics from the book like: add head, insert, search.
    This is the first problem for our projects, I know it is really easy but I can't get it:

    write a void function that takes a linked list of int and reserves the order of its nodes. The function will have one call-by-reference parameter that is a pointer to the head of the list.After the function is called, this pointer will point to the head of a linked list that has the same nodes as the original one, but in the reserve order they had in the original list. Note that your function will neither create or delete a node. Please place a test program for it.
    Like I said, I didn't get anything even after I thought about 2 hrs. So could anyone can show me a example or some specific code stuff? I think only the idea doesn't work for me, the best way is learn from examples!

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    If this is what you mean:
    write a void function that takes a linked list of int and reverses the order of its nodes. The function will have one call-by-reference parameter that is a pointer to the head of the list. After the function is called, this pointer will point to the head of a linked list that has the same nodes as the original one, but in the reverse order they had in the original list. Note that your function will neither create or delete a node. Please place a test program for it.
    All you need to do is think of a good algorithm. One thing that you could do is create a auxillary list. Take this new list and append the nodes of the old list, starting at the tail of the old list. When you're done, just reassign the parameter to the front of the new list. That should be sufficient.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    switch the head and tail pointers of each node.
    I will let you figure out the details yourself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM