Stephen Jacob
Stephen Jacob

Reputation: 901

Linked list using Class

I have a code, which seems to be working but I am failing to get the values stored in the linked list between the first and the last node, the pointers in between are skipped? And dereferencing these skipped pointer gives me a segfault, here is the code

#include<iostream>
#include <new>
using namespace std;

class list{ 
    int value;
    list* next;

public:    
    list(int a=0, list* b=0) {value=a;next=b;}    
    //~list() {delete next;}
    void newnode(int a, list* tmp) {
        tmp->next=new list;
        tmp=tmp->next;
        cout<<"Address of next: "<<tmp<<'\n';
        tmp->value=a;
    }

    void printlist (list* regist){
        list* tmp;
        tmp=regist; 
        cout<<tmp->value<<'\n';

        while(tmp->next != 0){
            tmp=tmp->next;
            cout<<tmp->value<<'\n';
            cout<<"Address of next: "<<tmp<<'\n';   
        }
    }
};

int main() {
    int first;    
    cout<<"Enter value for origin: \n";
    cin>>first; 
    list* root=new list(first);
    list* tpo=root;
    cout<<"How many numbers to add? \n";

    int choice;
    cin>>choice;

    int num;
    while(choice) {
        cout<<"Enter value: \n";
        cin>>num;    
        root->newnode(num, tpo);
        choice--;  
    }

    cout<<"Do you want me to show you these values, type 1 for yes and 0 for no: \n";
    cin>>choice;

    if(choice) {
        root->printlist(root);
    }
}
  1. On printing the values why does it skip these pointer(nodes) in between?
  2. Are the in between nodes being pointed to destroyed? If so, commenting the destructor, should do the trick, right?

What am I doing wrong?

Upvotes: 0

Views: 235

Answers (3)

Iosif Murariu
Iosif Murariu

Reputation: 2054

1) You always overwrite the 2nd element in your list when you call for more values. You need to change newnode()'s signature to newnode(int a, list*& tmp).

Later edit: another way would be to have the following signature list* newnode(int a, list* tmp) and at the end of the function you'd return tmp;. Then, in the main loop you'd have tpo = root->newnode(num, tpo);. This way tpo would always point to the next element.

2) Also, for freeing the memory list's destructor shouldn't do anything in particular. I'd say you make a static method in your class that deletes a list. Something like this:

public: static void deleteList(list*& root) { list* tmp = root; while (tmp) { tmp = root->next; delete root; root = NULL; root = tmp; } };

and call it like list::deleteList(root);

Upvotes: 2

maverickosama92
maverickosama92

Reputation: 2725

a comprehensive implementation of linked lists, check below link:

http://www.bitsbyta.com/2011/02/how-to-add-node-at-end-of-linked-list-c.html

Upvotes: 1

ogni42
ogni42

Reputation: 1276

you are always submitting root to newnode (as is was assigned to tpo) resulting in a list with two elements and an arbitrary number of leaked memory

Upvotes: 2

Related Questions