Thread: How is this happening?(linked lists)

  1. #1
    Registered User
    Join Date
    Nov 2012
    Location
    Heraklion, Greece, Greece
    Posts
    26

    How is this happening?(linked lists)

    Hello,i am getting something strange and unfortunetely i dont understand it.

    Code:
    #include "LinkedList.h"
    
    void concatenate(LinkedList<string>& object1,LinkedList<string>& object2){
     if(object1.head==NULL){
        object1.head=object2.head;
     }
    
    }
    
    
    
    int main(){
        
        LinkedList<string> LinkedList1;
        LinkedList<string> LinkedList2;
        LinkedList2.AddNode("anna"); 
        LinkedList2.AddNode("montanna");
        LinkedList2.AddNode("annamontanna");
        
        concatenate(LinkedList1,LinkedList2);
        LinkedList1.DisplayList();
        
        keep_window_open();
        
    }
    Code:
    void DisplayList(){
          curr=head;
          while (curr!=NULL){
          cout<<curr->data;
          curr=curr->next;
          }
        }

    How come object1.head=object2.head; copy the the entire object2 list to object1 list by just copying the head of it?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Because it's a pointer. You should review how they work.
    Your concatenate function is wrong, though. You should insert the second list at the end of the first list (or vice versa).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Location
    Heraklion, Greece, Greece
    Posts
    26
    Quote Originally Posted by Elysia View Post
    Because it's a pointer. You should review how they work.
    Your concatenate function is wrong, though. You should insert the second list at the end of the first list (or vice versa).
    Thanks.This was just the case where the first list was empty,i am about to implement the second case now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 01-10-2013, 04:09 AM
  2. Double Linked Dynamic Lists Vs Unrolled Linked Lists
    By lantzvillian in forum C Programming
    Replies: 6
    Last Post: 02-14-2012, 01:07 PM
  3. Replies: 4
    Last Post: 05-01-2010, 10:19 PM
  4. Question about Linked lists of lists
    By hear_no_evil in forum C Programming
    Replies: 2
    Last Post: 11-08-2004, 02:49 AM
  5. question on linked lists(stack with linked lists)
    By dionys in forum C Programming
    Replies: 1
    Last Post: 06-02-2004, 11:08 AM