Why is my print function not working? Linked list

372 views Asked by At

I am doing a class assignment, where I have to create a linked list representing a big number, but I cant get the print function to work.

This is the code:

Header

class List
{
private:
    struct node
    {
        int data;
        node* next;
    };
    node* head;
    node* temp;
    node* curr;
public: 
    List();
    void addNode(std::string digits);
    void printList();
};

Constructor

List::List()
{
    head = NULL;
    curr = NULL;
    temp = NULL;
}

Function that creates the list

void List::addNode(string digits)
{
    int o = 1;
    int j = 0;
    int i = 0;
    node *n;
    node *head;
    node *temp;
    //=================================================================================//
    n = new node;
    if (digits[0]=='-')
    {
        n -> data = -1;
        head = n;
        temp = n;
    }
    else
    {
        n -> data = 1;
        head = n;
        temp = n;
    }
    //================================================================================//
    if ((head->data) == -1)
    {
        while (o < digits.length())
        {
            n = new node;
            n->data = stoi(digits.substr(o,1));
            temp -> next = n;
            temp = temp -> next;
            o++;
        }
    }
    else
    {
        while(i < digits.length())
        {
            n = new node;
            n->data = stoi(digits.substr(i,1));
            temp -> next = n;
            temp = temp -> next;
            i++;
        }
    }

The print function I've been trying to implement, it gives no output (blank):

void List::printList()
{
    node* curr = head;
    while(curr != NULL)
    {
        cout<<curr -> data<<" ";
        curr = curr -> next;
    }
}

The list prints fine when I use this code in the addNode function:

if ((head -> data) == -1)
{
    while(j < digits.length())
    {
        cout<<head -> data<<" ";
        head = head -> next;
        j++;
    }
}
else
{
    while(j<=digits.length())
    {
        cout<<head -> data<<" ";
        head = head -> next;
        j++;
    }
}
1

There are 1 answers

1
Vlad from Moscow On

For starters these data members

node* temp;
node* curr;

are redundant. Instead of them you can use similar local variables in member functions of the class if it is required.

The function addNode deals with the local variable head instead of the data member with the same name.

void List::addNode(string digits)
{
    int o = 1;
    int j = 0;
    int i = 0;
    node *n;
    node *head;
    //…

Also you forgot to set the data member next of the last node to nullptr.

If the member function will be called a second time then there will be memory leaks.

Calling the standard function std::stoi for one character

n->data = stoi(digits.substr(i,1));

is inefficient.

The class can look the following way as it is shown in the demonstrative program below. You will need to add other required member functions (as for example the copy constructor or destructor) yourself.

#include <iostream>
#include <string>

class List
{
private:
    struct node
    {
        int data;
        node *next;
    } *head = nullptr;
public: 
    List() = default;
    void addNode( const std::string &digits );
    std::ostream & printList( std::ostream &os = std::cout ) const;
};


void List::addNode( const std::string &digits )
{
    if ( !digits.empty() && 
         !( digits.size() == 1 && ( digits[0] == '-' || digits[0] == '+') ) )
    {

        node **current = &head;

        while ( *current )
        {
            node *tmp = *current;
            *current = ( *current )->next;
            delete tmp;
        }

        std::string::size_type i = 0;

        *current = new node { digits[i] == '-' ? -1 : 1, nullptr };

        if ( digits[i] == '-' || digits[i] == '+' ) ++i;

        for ( ; i < digits.size(); i++ )
        {
            current = &( *current )->next;

            *current = new node { digits[i] - '0', nullptr };
        }
    }
}

std::ostream & List::printList( std::ostream &os ) const
{
    if ( head != nullptr )
    {
        if ( head->data == -1 ) os << '-'; 

        for ( node *current = head->next; current != nullptr; current = current->next )
        {
            os << current->data;
        }
    }

    return os;
}

int main() 
{
    List lst;

    lst.addNode( "-123456789" );
    lst.printList() << '\n';

    lst.addNode( "987654321" );
    lst.printList() << '\n';

    return 0;
}

The program output is

-123456789
987654321