Revesing the elements in a linklist.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Parul Bagadia
    New Member
    • Mar 2008
    • 188

    Revesing the elements in a linklist.

    I m getting stuck about back tracking the list;
    i can traverse the list in forward direction, and reach till the end of list; but how will i make it go back ; is the problem.
  • Encrypted
    New Member
    • Mar 2008
    • 8

    #2
    Originally posted by Parul Bagadia
    I m getting stuck about back tracking the list;
    i can traverse the list in forward direction, and reach till the end of list; but how will i make it go back ; is the problem.

    u may keep track of the previous address in the linked list..
    a way to do it is to use a doubly linked list... :)
    nevertheless i think u need to explain your problem in detail..i mean why would u like to back track the list... :|

    Comment

    • mac11
      Contributor
      • Apr 2007
      • 256

      #3
      Originally posted by Parul Bagadia
      I m getting stuck about back tracking the list;
      i can traverse the list in forward direction, and reach till the end of list; but how will i make it go back ; is the problem.
      How is the list implemented?

      If you're using the list from the stl you can use reverse iterators, but some lists are made using a single link between nodes and you can't go backwards through them.

      Comment

      • Parul Bagadia
        New Member
        • Mar 2008
        • 188

        #4
        Originally posted by mac11
        How is the list implemented?

        If you're using the list from the stl you can use reverse iterators, but some lists are made using a single link between nodes and you can't go backwards through them.
        Yaeh its a singly link list. And we are supposed to use the same thing.

        Comment

        • RedSon
          Recognized Expert Expert
          • Jan 2007
          • 4980

          #5
          Originally posted by Parul Bagadia
          Yaeh its a singly link list. And we are supposed to use the same thing.
          You need to have a temporary node pointer, pointing to the previous node. There is no other way. If you list is singly linked there is no way to traverse backwards.

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            Not necessarily. You could look at the first node, and make that the head of your new list.

            Now you look at the second node. Copy the data into a new, blank node. Set this new node's next pointer to the head node. Now make this node the head.

            Repeat as desired.

            EDIT: This is if you wish to obtain a list that is the reverse of the original list.

            If you want to traverse the list backwards while maintaining the original order in a singly-linked list, there is no way.

            Comment

            Working...