Tóth Attila
Tóth Attila

Reputation: 149

Print Function Linked Lists C++

I'm currently learning Linked Lists in C++, and I can't write a print function which prints out the elements of the list; I mean I wrote the function but it doesn't work properly.

#include <iostream>

using namespace std;

struct node
{
    char name[20];
    node* next;
};

node* addNewPerson(node* head)
{
    node* person = new node;

    cout << "Name: ";
    cin >> person->name;

    person->next = NULL;

    if (head == NULL) //is empty
    {
        head = person;
    }

    else
    {
        person = person->next;
    }

    return head;
}

void printList(node* head)
{
    node* temp = head;

    cout << temp->name << endl;
}

int main()
{
    node* head = NULL;

    node* temp = head;

    unsigned short people = 0;

    cout << "How many people do you want to invite to the party?" << endl;
    cout << "Answer: ";
    cin >> people;

    cout << endl;

    for (unsigned short i = 1; i <= people; i++)
    {
        addNewPerson(head);

        cout << endl;
    }

    cout << "LIST: " << endl;
    cout << endl;

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

    cin.get();
}

I am wondering what I'm doing wrong, please help me!

Upvotes: 3

Views: 55240

Answers (6)

Vlad from Moscow
Vlad from Moscow

Reputation: 311146

It is obvious that function addNewPerson is wrong.

node* addNewPerson(node* head)
{
    node* person = new node;

    cout << "Name: ";
    cin >> person->name;

    person->next = NULL;

    if (head == NULL) //is empty
    {
        head = person;
    }

    else
    {
        person = person->next;
    }

    return head;
}

You allocated new node person.

    node* person = new node;

Set its field next to NULL

    person->next = NULL;

Then if head is not equal to NULL you set person to person->next

        person = person->next;

As person->next was set to NULL it means that now also person will be equal to NULL.

Moreover the function returns head that can be changed in the function. However you ignore returned value in main

addNewPerson(head);

At least there should be

head = addNewPerson(head);

The valid function addNewPerson could look the following way

node* addNewPerson(node* head)
{
    node* person = new node;

    cout << "Name: ";
    cin >> person->name;

    person->next = head;
    head = person;

    return head;
}

And in main you have to write

  for (unsigned short i = 1; i <= people; i++)
  {
    head = addNewPerson(head);

    cout << endl;
  }

Function printList does not output the whole list. It outputs only data member name of the first node that is of head.

void printList(node* head)
{
    node* temp = head;

    cout << temp->name << endl;
}

It should look the following way

void printList(node* head)
{
  for ( ; head; head = head->next )
  {
    cout << head->name << endl;
  }
}

And at last main should look as

int main()
{
    node* head = NULL;

    unsigned short people = 0;

    cout << "How many people do you want to invite to the party?" << endl;
    cout << "Answer: ";
    cin >> people;

    cout << endl;

    for ( unsigned short i = 0; i < people; i++ )
    {
        head = addNewPerson(head);

        cout << endl;
    }

    cout << "LIST: " << endl;
    cout << endl;

    printList( head );

    cin.get();
}

Upvotes: 5

S421
S421

Reputation: 21

struct node
{
    char name[20];
    node* next;
};

node* addNewPerson(node* head)
{
    node* person = new node;

    //cout << "Name: ";
    cin >> person->name;

    person->next = head;
    head = person;

    return head;
}

void PrintLL(node *head)
{
    while (head!=NULL){
        cout<< head->name <<endl;
        head=head->next;
    }

}

int main() {

    node* head = NULL;
    node* temp = head;
    int num_ppl;
    cout << "How many people do you want to invite to the party?" << endl;
    cout << "Answer: ";
    cin >> num_ppl;

    for(int arr_i = 0; arr_i < num_ppl; arr_i++){

      head = addNewPerson(head);

    }
    PrintLL(head);
    return 0;
}

Upvotes: 0

user3307862
user3307862

Reputation: 62

I think the problem is in how the link list is being added to in addNewPerson.

The input to addNewPerson is 'head' so each time, before adding a new node to the linked list, we need to traverse all the way down the linked list till the last node before appending 'person' to the linked list.

Upvotes: 0

Alex Chen
Alex Chen

Reputation: 41

When you are adding a new person, it is not actually put into the linked list, since you have never invoked something like head -> next = person.

The section in your addNewPerson routine should be something like

if (head == NULL) {
    head = person;
} else {
    node * end = head;
    while (end -> next != NULL) // find the end of the list to insert new person
        end = end -> next;
    end -> next = person;
}

And in the beginning you can't just pass head into addNewPerson, since it is a pointer with null value, the addNewPerson method accepts a pointer by value (value of the memory address it is pointing at) rather than a reference to head. So you need to use

node* addNewPerson(node* &head) { ... }

instead. Finally, when you declare temp, it first receives the value of head as well, so later access to temp would only give you NULL again. You need to change it to

node* &temp = head;

Upvotes: 0

Jonny Henly
Jonny Henly

Reputation: 4233

addNewPerson method

Your addNewPerson method never added a new person to the list it just set the head to the new person.

node* addNewPerson(node* head)
{
  node* person = new node;

  cout << "Name: ";
  cin >> person->name;

  person->next = NULL;

  if (head->next == NULL) //is empty
  {
    head->next = person;
  }
  else
  {
    person->next = head->next;
    head->next = person;
  }

  return head;
}

printList method

Your method printList should do what it says and print the list, instead of just printing one person at a time.

void printList(node* head)
{
  node* tmp = head;

  while(tmp->next != NULL) {
    tmp = tmp->next;
    cout >> tmp->name >> endl;
  }
}

main method

Upadted your main method with the new printList method.

int main()
{
  node* head = new node;

  unsigned short people = 0;

  cout << "How many people do you want to invite to the party?" << endl;
  cout << "Answer: ";
  cin >> people;

  cout << endl;

  for (unsigned short i = 1; i <= people; i++)
  {
    addNewPerson(head);

    cout << endl;
  }

  cout << "LIST: " << endl;
  cout << endl;

  cout << printList(head);

  cin.get();
}

Also why are you using unsigned short and char arrays? Do you have a limited amount of memory? Why not just use int and string?

Upvotes: 0

user1052842
user1052842

Reputation: 121

In your addNewPerson function person->next never gets set, because head is always NULL.

Upvotes: 0

Related Questions