Linked List not working again:

This is a discussion on Linked List not working again: within the C++ Programming forums, part of the General Programming Boards category; My else statement in the LinkedLists is crashing the program. I get EXC_ACCESS error on my compiler on head -> ...

  1. #1
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    466

    Linked List not working again:

    My else statement in the LinkedLists is crashing the program. I get EXC_ACCESS error on my compiler on head->num=temp->num;
    Code:
    void ListHandling::LinkedLists(NodePtr temp)
    {
        temp = NULL;
        
        if (head!=NULL) {
            current=head;
            
            while (current->next != NULL) {
                current=current->next;
            }
            current= new node;
            current->num= temp->num;
            current->next->next=nullptr;
        }
        else
        head = new node;
        head->num=temp->num;
        head->next=nullptr;
    
    }
    Code:
    void ListHandling::PrintLinkedLists()
    {
        current=head;
        if (head==nullptr) {
            std::cout<<"The list is empty"<<std::endl;
        }
        else
            while (current!=NULL) {
                std::cout<<current->num<<",";
                current=current->next;
            }
    
    }
    In main()
    Code:
    NodePtr temporary = new node;
        
        for (int i=1; i<20; ++i) {
            temporary->num = i;
            Mylists.LinkedLists(temporary);
        }
        
        Mylists.PrintLinkedLists();
    Code:
    typedef  struct node{
        int num;
        node* next;
    }*NodePtr;
    
    class ListHandling {
    private:
        std::vector<int> List3;
        
        struct sorting{
            bool operator () (int i, int j) {return (i<j);}
        } ComparedTo;
        
        NodePtr head;
        NodePtr current;
        
    public:
        ListHandling();
        ListHandling(std::vector<int>lst3)
        :List3(lst3)
        {
            head=NULL;
            current=NULL;
        
        }
        void MergeLists(std::vector<int>& ,std::vector<int>& );
        void SortLists();
        void PrintSortedList();
        void MergeArray(std::array<int, 5>&,std::array<int, 5>&);
        
        void LinkedLists(NodePtr);
        void PrintLinkedLists();
        
    };

  2. #2
    Registered User
    Join Date
    Jun 2013
    Posts
    56
    Code:
     NodePtr head;
     NodePtr current;
    


    Not pointers, these are variables. I believe you want pointers here.

  3. #3
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    466
    Code:
    typedef  struct node{
        int num;
        node* next;
    }*NodePtr;
    I did this for that

  4. #4
    Registered User
    Join Date
    Jun 2013
    Posts
    56
    From my understand all that does is mean you can use NodePtr or node to refer to that structure. It does not mean you no longer have to declare pointers to that structure as pointer. I may be mistaken, but given the error you are receiving I'm inclined to believe I am correct here.

  5. #5
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    466
    I have used this method a lot when making linked lists. I think my issue is how I am sending the function values in main with the loop. I am continuing to work it.

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You're dereferencing temp on that line, but temp has only been assigned NULL.
    Actually, passing temp in and immediately setting it to NULL is not very useful.
    Is "temp" the new node that you want to add or is it a node that you want to add a copy of?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User
    Join Date
    Jun 2013
    Posts
    56
    Wow, I missed this one the first read through.

    Code:
    temp = NULL;
    


    in your first code bracket line 3. Wouldn't that set the argument you are passing in to NULL?

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    If you want to add a new node with a given number:
    Code:
    void List::append(int num) {
        node *newnode = new node(num);
        if (head) {
            for (curr = head; curr->next; curr=curr->next)
                ;
            curr = newnode;
        }else
            head = newnode;
    }
    I've changed the names to protect the innocent.

    You could keep a pointer to the end of the list as well as the front to make this simpler/quicker.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  9. #9
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    466
    YOU WERE RIGHT. The NULL was getting to me... I fixed it now thanks.

  10. #10
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    466
    Code:
    for (int i=1; i<20; ++i){
            temporary->num=i;
            Mylists.LinkedLists(temporary);
        }
    Is this a wrong way to send a bunch of numbers to the function fast? Because it is only printing out the number 1;

  11. #11
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    There's a mistake in the code I posted (and yours it was based on). It should be:
    Code:
    void List::append(int num) {
        node *curr, *newnode = new node(num);  // node(num) sets num to num and next to nullptr
        if (head) {
            for (curr = head; curr->next; curr=curr->next)
                ;
            curr->next = newnode;           // NOTE: curr->next is set to new node
        }else
            head = newnode;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  12. #12
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    466
    Wow never seen it done like that... I love this website!!!!!!

    I am still trying to tackle
    Code:
     NodePtr temporary = new node;
        
        for (int i=1; i<20; ++i){
            temporary->num = i;
            Mylists.LinkedLists(temporary);
        }
    Code:
    std::cout<<"Printing linked list: \n";
        Mylists.PrintLinkedLists();
    


    It is printing out the number 1

  13. #13
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    What does your append (or ListHandling::LinkedLists) function look like now?
    Should you maybe just be passing in the integer (not a node pointer)?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  14. #14
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    466
    Here is what has me puzzled when I take the function outside the loop it equals 19, which makes sense. However when I leave it in the loop it returns 1. That is what is getting me. Example:
    Code:
    NodePtr temporary = new node;
        
        for (int i=1; i<20; ++i){
            temporary->num = i;
            Mylists.LinkedLists(temporary);
        }
        std::cout<<"Printing linked list: \n";
        Mylists.PrintLinkedLists();
    Code:
    NodePtr temporary = new node;
        
        for (int i=1; i<20; ++i){
            temporary->num = i;
          
        }
        Mylists.LinkedLists(temporary);
        
        std::cout<<"Printing linked list: \n";
        Mylists.PrintLinkedLists();

  15. #15
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    466
    No that did not work....

    Code:
    void ListHandling::LinkedLists(int x)
    {
        
        if (head!=NULL) {
            current=head;
            
            while (current->next != NULL) {
                current=current->next;
            }
            current= new node;
            current->num = x;
            current->next=nullptr;
        }
        else
        {
        head = new node;
        head->num = x;
        head->next=nullptr;
        }
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 04-13-2012, 08:46 AM
  2. Partially working function - linked list
    By patricio2626 in forum C++ Programming
    Replies: 3
    Last Post: 10-28-2006, 06:38 AM
  3. question about a working linked list
    By cold_dog in forum C++ Programming
    Replies: 23
    Last Post: 09-13-2006, 01:00 AM
  4. linked list not working :(
    By 8ball in forum C Programming
    Replies: 2
    Last Post: 05-12-2004, 08:45 AM
  5. Linked List Working Code
    By Linette in forum C++ Programming
    Replies: 9
    Last Post: 01-24-2002, 11:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21