3

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!

1
  • 'but it doesn't work properly.' and 'I am wondering what I'm doing wrong' are to vague to diagnose your problems, sorry! And please edit your question, instead of trying to clarify with comments ... Commented Feb 19, 2014 at 22:16

6 Answers 6

5

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();
}
Sign up to request clarification or add additional context in comments.

2 Comments

What is temp in printlist?
@Damian It was a typo.:)
0

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

Comments

0

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?

Comments

0

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;

Comments

0

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.

Comments

0
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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.