2

I was writing a small program using pointers, This is my code:

    #include<iostream>

using namespace std;

struct number
{
int data;
number *next;
} test;

number * last_node(number *head)
{
    number *temp = new number;
    temp = head;

    while (temp->next != nullptr)
    {
        temp = temp->next;
    }

    return temp;

}

void insert_at_end (number *head)
{
    number *temp = new number;
    number *last = new number;
last = last_node(*head);

    cout<<"Enter the data";
    cin>>temp->data;
    temp->next = nullptr;
    last->next = temp;
}

void view_all (number *head)
{
    number *temp = new number;
    temp = head;

    while (temp->next != nullptr)
    {
        cout<<temp->data;
        temp = temp->next;
    }
}

int main()
{
    number *head = &test;
    int choice;
    char ans = 'y';

    do
    {



    cout<<"Enter your choice";
    cout<<"\n 1. Insert ";
    cout<<"\n 2. View ";
    cout<<"\n 3. Exit ";

    cin>>choice;

    switch (choice)
    {
        case 1 : insert_at_end(head);
        break;

        case 2 : view_all(head);
        break;

        case 3 : goto label;

        default : cout<<"You have entered wrong choice";
    }

    cout<<"Do you want the menu again ? (y/n)" ;
    cin>>ans;

    }

    while (ans = 'y');

    label:

        return 0;
}

On compilation, an error occurs on saying it can't convert "number" to "number*" while calling the function 'last_node(*head)'.

Can you please help me understand this properly?

  • 2
    `operator*` is for dereferencing, thus if `head` is a `number*` then `*head` is a `number` – 463035818_is_not_an_ai Jul 21 '17 at 17:10
  • In `last = last_node(*head);` you are dereferencing `head`, and passing `number`, while `last_node` expects to receive `number*`. In addition, you leak memory in the following statements `number *temp = new number; temp = head;` (because you overwrite pointer to newly allocated `number`). Consider reading [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Algirdas Preidžius Jul 21 '17 at 17:12

1 Answers1

0

You've made a few mistakes but straight up front about that error, you just need to pass head pointer rather than *head which is dereferencing it. There are several memory leaks like inside last_node() function you've done -

number *temp = new number;
temp = head;

creating a new number type on heap and pointing that memory to temp pointer. Now on the very next line you change temp to point to head, thus making your new number lost. Also while iterating your linked list, you're missing the very last element so look at the modifications I've made. Another critical error is assignment operation instead of == here - while (ans = 'y');, be careful with these typos. And don't use goto where it doesn't fit.

#include <iostream>

using namespace std;

struct number {
    int data = 0;
    number *next = nullptr;
};

number *last_node(number *head)
{
    number *temp = head;
    while (temp->next != nullptr) {
        temp = temp->next;
    }
    return temp;
}

void insert_at_end(number *head)
{
    number *temp = new number();
    number *last = last_node(head);

    cout << "Enter the data";
    cin >> temp->data;
    temp->next = nullptr;
    last->next = temp;
}

void view_all(number *head)
{
    number *temp = head;
    while (temp != nullptr) {
        cout << temp->data;
        temp = temp->next;
    }
}

int main()
{
    number *head = new number();
    int choice;
    char ans = 'y';

    do {
        cout << "Enter your choice";
        cout << "\n 1. Insert ";
        cout << "\n 2. View ";
        cout << "\n 3. Exit ";

        cin >> choice;

        switch (choice) {
            case 1: insert_at_end(head); break;
            case 2: view_all(head); break;
            case 3: return 0;
            default: cout << "You have entered wrong choice";
        }

        cout << "Do you want the menu again ? (y/n)";
        cin >> ans;
    }
    while (ans == 'y');

    return 0;
}
Abhinav Gauniyal
  • 7,034
  • 7
  • 50
  • 93