Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. How NOT to reverse a list [modified]

How NOT to reverse a list [modified]

Scheduled Pinned Locked Moved The Weird and The Wonderful
question
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CurtD
    wrote on last edited by
    #1

    Does anyone see anything stupid in this code? Here's a hint: it isn't the most efficient logic I've seen.

    void CrapClass::reverse_points()
    {
    CPoint *pt, *npt;
    CList <CPoint *, CPoint *> list;
    POSITION next, prev;

    // copy the list
    next = m_point_list.GetHeadPosition();
    while (next != NULL)
    {
    pt = m_point_list.GetNext(next);
    npt = new CPoint(*pt);
    list.AddTail(npt);
    }

    // delete the old list
    delete_points();

    // create the new list in reverse order
    prev = list.GetTailPosition();
    while (prev != NULL)
    {
    pt = list.GetPrev(prev);
    npt = new CPoint(*pt);
    m_point_list.AddTail(npt);
    }

    // delete the points from the temp list
    while (!list.IsEmpty())
    delete list.RemoveHead();
    }

    modified on Thursday, December 13, 2007 6:52:30 PM

    P H 2 Replies Last reply
    0
    • C CurtD

      Does anyone see anything stupid in this code? Here's a hint: it isn't the most efficient logic I've seen.

      void CrapClass::reverse_points()
      {
      CPoint *pt, *npt;
      CList <CPoint *, CPoint *> list;
      POSITION next, prev;

      // copy the list
      next = m_point_list.GetHeadPosition();
      while (next != NULL)
      {
      pt = m_point_list.GetNext(next);
      npt = new CPoint(*pt);
      list.AddTail(npt);
      }

      // delete the old list
      delete_points();

      // create the new list in reverse order
      prev = list.GetTailPosition();
      while (prev != NULL)
      {
      pt = list.GetPrev(prev);
      npt = new CPoint(*pt);
      m_point_list.AddTail(npt);
      }

      // delete the points from the temp list
      while (!list.IsEmpty())
      delete list.RemoveHead();
      }

      modified on Thursday, December 13, 2007 6:52:30 PM

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      That code is bad. I love the use of a completely pointless copy the first time round.

      Deja View - the feeling that you've seen this post before.

      My blog | My articles

      C 1 Reply Last reply
      0
      • C CurtD

        Does anyone see anything stupid in this code? Here's a hint: it isn't the most efficient logic I've seen.

        void CrapClass::reverse_points()
        {
        CPoint *pt, *npt;
        CList <CPoint *, CPoint *> list;
        POSITION next, prev;

        // copy the list
        next = m_point_list.GetHeadPosition();
        while (next != NULL)
        {
        pt = m_point_list.GetNext(next);
        npt = new CPoint(*pt);
        list.AddTail(npt);
        }

        // delete the old list
        delete_points();

        // create the new list in reverse order
        prev = list.GetTailPosition();
        while (prev != NULL)
        {
        pt = list.GetPrev(prev);
        npt = new CPoint(*pt);
        m_point_list.AddTail(npt);
        }

        // delete the points from the temp list
        while (!list.IsEmpty())
        delete list.RemoveHead();
        }

        modified on Thursday, December 13, 2007 6:52:30 PM

        H Offline
        H Offline
        Haroon Sarwar
        wrote on last edited by
        #3

        well the class has been named CrapClass...was the developer in question trying to write bad code?? but i guess u must have renamed it urself... :doh:

        modified on Friday, December 14, 2007 9:57:28 AM

        C 1 Reply Last reply
        0
        • H Haroon Sarwar

          well the class has been named CrapClass...was the developer in question trying to write bad code?? but i guess u must have renamed it urself... :doh:

          modified on Friday, December 14, 2007 9:57:28 AM

          C Offline
          C Offline
          CurtD
          wrote on last edited by
          #4

          Haroon Sarwar wrote:

          well the class has been named CrapClass...was the developer in question trying to write bad code?? but i guess u must have renamed it urself...

          Real names have been changed to protect the innocent (and the guilty).

          1 Reply Last reply
          0
          • P Pete OHanlon

            That code is bad. I love the use of a completely pointless copy the first time round.

            Deja View - the feeling that you've seen this post before.

            My blog | My articles

            C Offline
            C Offline
            CurtD
            wrote on last edited by
            #5

            Pete O'Hanlon wrote:

            Forum:Coding Horrors Subject:Re: How NOT to reverse a list Sender:Pete O'Hanlon Date:Friday, December 14, 2007 4:49:00 PM That code is bad. I love the use of a completely pointless copy the first time round.

            I'm thinking he wanted a really fresh copy -- nothing like data where the bits are still warm. Yummy.

            P 1 Reply Last reply
            0
            • C CurtD

              Pete O'Hanlon wrote:

              Forum:Coding Horrors Subject:Re: How NOT to reverse a list Sender:Pete O'Hanlon Date:Friday, December 14, 2007 4:49:00 PM That code is bad. I love the use of a completely pointless copy the first time round.

              I'm thinking he wanted a really fresh copy -- nothing like data where the bits are still warm. Yummy.

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              CurtD wrote:

              nothing like data where the bits are still warm

              Souldn't he have done it twice then - just in case the first wasn't heated through warmly enough.

              Deja View - the feeling that you've seen this post before.

              My blog | My articles

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups