I don't know how to get the program to display properly any tips? Also would anyone guide me on how the logic works for deallocating all this memory please.

#include <iostream>
#include <cstdlib>
#include <windows.h>

using namespace std;

struct Entry
{
    char name[23];
    int age;
    Entry *next;
};

// Prototype
Entry* buildList(int);
Entry* getNewEntry(int);
void displayList(Entry *);
void displayEntry(Entry *);

int main(int argc, char *argv[])
{   
    int numNodes;
    cout << "How many nodes? ";
    cin >> numNodes;
    Entry *myList = buildList(numNodes);
    displayList(myList);
    
    
    /*
    Entry *myEntry = getNewEntry();
    displayEntry(myEntry);*/
    
    Sleep(4000);
    return 0;
}

Entry* getNewEntry(int numNodes)
{
    Entry *newOne = new Entry;
    cout << "Name: ";
    cin >> newOne->name;
    cout << "Age: ";
    cin >> newOne->age;
    if(numNodes == 3)
    newOne->next = NULL;
    
    return newOne;
}

Entry* buildList(int numNodes)
{
// Start of List
    Entry *startList = NULL;
    for(int i = 0; i < numNodes; i++)
    {
        // Build Node
        Entry *newOne = getNewEntry(numNodes);
        
        if(!newOne)
        {
            cout << "No memory allocated." << endl;
            exit(0);
        }
        startList = newOne;
        newOne->next = startList;
    }
    
    return startList;
}

void displayList(Entry *dInfo)
{
     for(Entry *current = dInfo; current != NULL; current->next)
     displayEntry(current);
}

void displayEntry(Entry *dInfo)
{
     cout << dInfo->name << "    " << dInfo->age << endl;
}

There are a couple of errors in your code:

1. The for loop in displayList should set current = current->next
2. getNewEntry should always set newOne->next to NULL!
3. buildList should not link newOne to itself!!!

1 and 2 is not difficult to fix.
For nr 3 I would recommend to do something like this:

Entry *startList = NULL;
Entry *prev = NULL;
for (int i = 0; i < numNodes; i++) {
  Entry* newOne = getNewEntry();
  if (startList == NULL) {
    startList = newOne;
    prev = startList;
  }
  else {
    prev->next = newOne;
    prev = newOne;
  }
}
return startList;

If you want to check if (!newOne) you should do that right after that memory is allocated which is inside getNewEntry! Otherwise you will get a segmentation fault when you try to access its members passed to cin >> (if its NULL).

Oh I forgot your other question. Here are two ways you can free the memory of the list. The first one is a recursive function and the other one uses a loop.

void freeList(Entry* entry)
{
  if (entry != NULL) {
    if (entry->next != NULL)
      freeList(entry->next);
    delete entry;
  }
}

void freeList2(Entry* entry)
{
  while (entry) {
    Entry* tmp = entry;
    entry = entry->next;
    delete tmp;
  }
}

There is also an option to free the next pointer in Entry's destructor. However this will make it a little tricky to delete only one entry in a list. You would have to unlink that entry before deleting it..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.