Announcement

Collapse
No announcement yet.

C++ Gurus

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • C++ Gurus

    I took a few intensive courses on C++ and Data structures my freshmen year of college. 5 years later I'm a bit rusty, but have a few requests to critique my logic:

    Simple function to remove a node in a list of type list, I think I've got the right idea but I'm so damn rusty on pointers it's killing me:
    Code:
    //This isn't right, but just shows what I'm working with
        struct list() {
            list *next;
            list *prev;
            list *data;
    }
    
    //This is the function I wrote
        void remove(list node) {
    list *nPrev = node->prev;
    list *nNext = node->next;
    
    nPrev->next = nNext;
    nNext->prev = nPrev;
    
    delete node;
    
    return 1;
    }
    USAR

  • #2
    Re: C++ Gurus

    You probably want to do some kind of comparison with node and the current node you're looking at. If they are the same, delete it.

    This part looks funny...
    Code:
    nPrev->next = nNext;
    You probably want that reversed or something...

    Code:
    nNext = node->next;
    nPrev = node->prev;
    So you have your references to nodes on either side of the node you want to delete. Make sure they reference each other and deallocate the memory for the node you want to delete.

    - It's who you game with.

    Comment


    • #3
      Re: C++ Gurus

      Hm, I thought that was what I was doing? I thought I was taking the node, selecting its next pointer as a node, and then changing the "next" nodes previous to the original nodes previous. And doing the same with the nodes previous pointer.

      Or am I just using them wrong?
      USAR

      Comment


      • #4
        Re: C++ Gurus

        Ah, I see... I got all mixed up when I first looked at it.

        What happens when you run it?

        The only other thing that might cause a problem is the function signature. If the function's argument is pass by value, it might not delete when you want it to. You'll have to pass the node argument by reference since the parameter calls for the list struct and not a pointer to a list struct.

        Change the function's parameter to
        Code:
        void remove (list *node)
        or call the function with
        Code:
        remove(&node);

        - It's who you game with.

        Comment


        • #5
          Re: C++ Gurus

          Understood. Right now I haven't been able to run any code, my PC is currently dead.
          USAR

          Comment


          • #6
            Re: C++ Gurus

            Ok, what about this:

            Code:
            void remove() {
            	list *nNext = this->next;
            	list *nPrev = this->prev;
            
            	nNext ->prev = nPrev ;
            	nPrev ->next = nNext ;
            
            	delete this->data;
            	delete this;
            }
            USAR

            Comment


            • #7
              Re: C++ Gurus

              It looks ok. It also seems like you changed it so that remove() is an operation on a node object. Everything looks ok logically.

              - It's who you game with.

              Comment


              • #8
                Re: C++ Gurus

                Yeah that's what I did (seems more logical, plus I won't have to check if the node is of type list). Great, thanks for the guidance.
                USAR

                Comment


                • #9
                  Re: C++ Gurus

                  Perhaps off topic, but if you are going to do programming these days you might want to look at either Java or .NET. This way you can safely ignore the pesky *'s.

                  Pointer arithmetics will be a thing of your past.
                  --
                  VI VI VI - the number of the beast

                  Comment


                  • #10
                    Re: C++ Gurus

                    Originally posted by PanzerHans View Post
                    Perhaps off topic, but if you are going to do programming these days you might want to look at either Java or .NET. This way you can safely ignore the pesky *'s.

                    Pointer arithmetics will be a thing of your past.
                    This is a good point. Managed code is much easier to program but... it's managed code. You lose that control you hated so much, which means your program might not run as fast or use the smallest memory footprint.

                    Managed code would be good for this unless it's a lesson in using pointers. I think it's still good to understand what pointers are and how to use them. Java uses pointers but not in the way you'd think because you never have to think about memory references. This can lead to problems when you accidentally pass a reference to a method instead of a copy of the object or vice versa. You also still need to understand the concept of pointers for linked lists! :)

                    - It's who you game with.

                    Comment


                    • #11
                      Re: C++ Gurus

                      This was entirely for a refresher with pointers and linked lists. I've done this all before, but I just needed to wrap my head around it again.

                      Plus I enjoy the control I get with pointers :) Now if I just remembered how to write in C instead of C++ I'd be in line to create some quick peices of code.

                      Now Assembly language is where the real speed is at :icon11:, oh my little microcontrollers how I miss thee.
                      USAR

                      Comment


                      • #12
                        Re: C++ Gurus

                        Whenever I'm using linked lists, I use a 'list *pCurrent' pointer and use that as my main reference point. (the notion of a 'current' pointer gets used a lot in real-time code as well)

                        Comment

                        Connect

                        Collapse

                        TeamSpeak 3 Server

                        Collapse

                        Advertisement

                        Collapse

                        Twitter Feed

                        Collapse

                        Working...
                        X