Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Issue with pointers in doubly linked lists.

Issue with pointers in doubly linked lists.

Scheduled Pinned Locked Moved Solved C++ Gurus
6 Posts 3 Posters 513 Views
  • 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.
  • Lonewolf46L Offline
    Lonewolf46L Offline
    Lonewolf46
    wrote on last edited by kshegunov
    #1

    Hello, I hope everyone's here is doing good. I'm not much of a forum guy but I have a question to ask and I can't seem to figure an answer on my own.

    The thing is, for certain assignment I need to use a doubly linked list, I have done it already in NetBeans 8.2 (C++) and works just fine but for some reason the assignation of value of the right pointer from the 2nd node until the end doesn't seem to work in QT. It compiles and runs but doesn't do the link from one node to another one at all.
    It links the first one with the second one and backwards, and it links the third one with the second one but not the second one with the third and so on.

    Here is a copy of the code that I doesn't seem to work on QT. (Yes, is a template)

    template<class T>
    class ListaAux;
    
    template<class T>
    class Node{
        private:
            Node<T> * LeftLink;
            Node<T> * RightLink;
            T info;
        public:
            Node();
            friend class ListaAux<T>;
    };
    
    template <class T>
    NodoDoble<T>::NodoDoble(){
        LigaDer = NULL;
        LigaIzq = NULL;
    }
    
    template <class T>
    class ListaAux{
        private:
            Node<T> * first;
            Node<T> * last;
        public:
            ListaAux();
            void Insert(T data);
    //More functions go here but aren't relevant for this matter
    
    template <class T>
    void ListaAux<T>::Insert(T data){
        if(first==NULL){
    //Insert first in case is the first element to ever come
            Node<T> * nuevo = new Node<T>();
            nuevo->info = data;
            first= nuevo;
            last= nuevo;
         }
         else{
    //Insert code if it is the second item to be inserted
            if(first==last){
                Node<T> * nuevo = new Node<T>();
                nuevo->info=dato;
                nuevo->LeftLink=first;
                first->RightLink=nuevo;
                last=nuevo;
             }
             else{
    //This inserts the new node at the very end of the list
    //Supposed to insert anything else that may come
                Node<T> * nuevo = new Node<T>();
                nuevo->info=data;
                nuevo->LeftLink=last;
                last->RightLink=nuevo;
                last=nuevo;
    //This three last lines don't seem to link...
    //...the past last node right link with the location of the new one.
             }
        }
    }
    

    Ignore any typos, or names that doesn't match, I could have missed something translating the names for more understandability but I assure you that my source code compiles and works completely fine except for the issue I mentioned.

    Maybe I am missing something in QT that is different with pointers, I don't know but please let me know. Thanks for reading. : )

    [Moved to C++ Gurus ~kshegunov]

    kshegunovK 1 Reply Last reply
    0
    • Lonewolf46L Lonewolf46

      Hello, I hope everyone's here is doing good. I'm not much of a forum guy but I have a question to ask and I can't seem to figure an answer on my own.

      The thing is, for certain assignment I need to use a doubly linked list, I have done it already in NetBeans 8.2 (C++) and works just fine but for some reason the assignation of value of the right pointer from the 2nd node until the end doesn't seem to work in QT. It compiles and runs but doesn't do the link from one node to another one at all.
      It links the first one with the second one and backwards, and it links the third one with the second one but not the second one with the third and so on.

      Here is a copy of the code that I doesn't seem to work on QT. (Yes, is a template)

      template<class T>
      class ListaAux;
      
      template<class T>
      class Node{
          private:
              Node<T> * LeftLink;
              Node<T> * RightLink;
              T info;
          public:
              Node();
              friend class ListaAux<T>;
      };
      
      template <class T>
      NodoDoble<T>::NodoDoble(){
          LigaDer = NULL;
          LigaIzq = NULL;
      }
      
      template <class T>
      class ListaAux{
          private:
              Node<T> * first;
              Node<T> * last;
          public:
              ListaAux();
              void Insert(T data);
      //More functions go here but aren't relevant for this matter
      
      template <class T>
      void ListaAux<T>::Insert(T data){
          if(first==NULL){
      //Insert first in case is the first element to ever come
              Node<T> * nuevo = new Node<T>();
              nuevo->info = data;
              first= nuevo;
              last= nuevo;
           }
           else{
      //Insert code if it is the second item to be inserted
              if(first==last){
                  Node<T> * nuevo = new Node<T>();
                  nuevo->info=dato;
                  nuevo->LeftLink=first;
                  first->RightLink=nuevo;
                  last=nuevo;
               }
               else{
      //This inserts the new node at the very end of the list
      //Supposed to insert anything else that may come
                  Node<T> * nuevo = new Node<T>();
                  nuevo->info=data;
                  nuevo->LeftLink=last;
                  last->RightLink=nuevo;
                  last=nuevo;
      //This three last lines don't seem to link...
      //...the past last node right link with the location of the new one.
               }
          }
      }
      

      Ignore any typos, or names that doesn't match, I could have missed something translating the names for more understandability but I assure you that my source code compiles and works completely fine except for the issue I mentioned.

      Maybe I am missing something in QT that is different with pointers, I don't know but please let me know. Thanks for reading. : )

      [Moved to C++ Gurus ~kshegunov]

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by kshegunov
      #2

      There's not a line of Qt specific code here. That's all plain C++.
      Have you traced it in the debugger?

      Beside the obvious simplification:

      template <class T>
      void ListaAux<T>::Insert(T data)
      {
           auto createNode = [this] (const T & data) -> Node<T> * {
                Node<T> * node = new Node<T>();
                if (last)
                    last->RightLink = node;
                node->LeftLink = last;
                node->info = data;
                return node;
           };
           
           last = createNode(data);
           if (!first)
               first = last;
      }
      

      I didn't spot anything obviously wrong, but then again I'm no debugger.

      Read and abide by the Qt Code of Conduct

      Lonewolf46L jeremy_kJ 2 Replies Last reply
      3
      • kshegunovK kshegunov

        There's not a line of Qt specific code here. That's all plain C++.
        Have you traced it in the debugger?

        Beside the obvious simplification:

        template <class T>
        void ListaAux<T>::Insert(T data)
        {
             auto createNode = [this] (const T & data) -> Node<T> * {
                  Node<T> * node = new Node<T>();
                  if (last)
                      last->RightLink = node;
                  node->LeftLink = last;
                  node->info = data;
                  return node;
             };
             
             last = createNode(data);
             if (!first)
                 first = last;
        }
        

        I didn't spot anything obviously wrong, but then again I'm no debugger.

        Lonewolf46L Offline
        Lonewolf46L Offline
        Lonewolf46
        wrote on last edited by
        #3

        @kshegunov Thanks for taking time to move my question and replying. I did try to use the debugger and is completely ignoring the line that sets the last node right link to aim at the new one. I tested it with different input data and apparently the data that I was initially using was causing issues for some reason. What kind of issues, I don't know.

        kshegunovK 1 Reply Last reply
        0
        • kshegunovK kshegunov

          There's not a line of Qt specific code here. That's all plain C++.
          Have you traced it in the debugger?

          Beside the obvious simplification:

          template <class T>
          void ListaAux<T>::Insert(T data)
          {
               auto createNode = [this] (const T & data) -> Node<T> * {
                    Node<T> * node = new Node<T>();
                    if (last)
                        last->RightLink = node;
                    node->LeftLink = last;
                    node->info = data;
                    return node;
               };
               
               last = createNode(data);
               if (!first)
                   first = last;
          }
          

          I didn't spot anything obviously wrong, but then again I'm no debugger.

          jeremy_kJ Offline
          jeremy_kJ Offline
          jeremy_k
          wrote on last edited by
          #4

          @kshegunov said in Issue with pointers in doubly linked lists.:

          There's not a line of Qt specific code here. That's all plain C++.
          Have you traced it in the debugger?

          Beside the obvious simplification:

          template <class T>
          void ListaAux<T>::Insert(T data)
          {
               auto createNode = [this] (const T & data) -> Node<T> * {
          

          Out of curiosity, what's the motivation for using a lambda to initialize the new node rather than running the code directly in Insert()?

          Given that the lambda is destroyed when the function returns, I wonder if any compiler will inline it and optimize out the capture.

          follow up: godbolt.org says yes, at least for some versions of gcc and clang.

          Asking a question about code? http://eel.is/iso-c++/testcase/

          1 Reply Last reply
          0
          • Lonewolf46L Lonewolf46

            @kshegunov Thanks for taking time to move my question and replying. I did try to use the debugger and is completely ignoring the line that sets the last node right link to aim at the new one. I tested it with different input data and apparently the data that I was initially using was causing issues for some reason. What kind of issues, I don't know.

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #5

            @Lonewolf46 said in Issue with pointers in doubly linked lists.:

            I tested it with different input data and apparently the data that I was initially using was causing issues for some reason. What kind of issues, I don't know.

            Did you forget to initialize first and last to nullptr?

            @jeremy_k said in Issue with pointers in doubly linked lists.:

            Out of curiosity, what's the motivation for using a lambda to initialize the new node rather than running the code directly in Insert()?

            No reason in particular. Started writing something, stopped, started again, so it just stuck trough.

            @jeremy_k said in Issue with pointers in doubly linked lists.:

            Given that the lambda is destroyed when the function returns, I wonder if any compiler will inline it and optimize out the capture.

            Yes, every decent optimizier is going to do it. (-O2 implied)

            Read and abide by the Qt Code of Conduct

            Lonewolf46L 1 Reply Last reply
            0
            • kshegunovK kshegunov

              @Lonewolf46 said in Issue with pointers in doubly linked lists.:

              I tested it with different input data and apparently the data that I was initially using was causing issues for some reason. What kind of issues, I don't know.

              Did you forget to initialize first and last to nullptr?

              @jeremy_k said in Issue with pointers in doubly linked lists.:

              Out of curiosity, what's the motivation for using a lambda to initialize the new node rather than running the code directly in Insert()?

              No reason in particular. Started writing something, stopped, started again, so it just stuck trough.

              @jeremy_k said in Issue with pointers in doubly linked lists.:

              Given that the lambda is destroyed when the function returns, I wonder if any compiler will inline it and optimize out the capture.

              Yes, every decent optimizier is going to do it. (-O2 implied)

              Lonewolf46L Offline
              Lonewolf46L Offline
              Lonewolf46
              wrote on last edited by
              #6

              @kshegunov said

              Did you forget to initialize first and last to nullptr?

              Negative, those where already perfectly initialized. All it took was to change the input data and it worked; which if you are wondering was a char but the method I was using to retrieve it from the data obtained was the reason for it to break. Really uncanny but I am happy that it works now and thankful to you for replying to begin with.

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved