smooth_atonal_sound
smooth_atonal_sound

Reputation: 69

C++ linear linked list information. I'm very lost as to what is wrong

EDIT: I figured it out, thanks for the help from those who answered.

Could anybody help me with a bit of my code? I'm new to C++, and I can't get my program to work. It just runs the switch statement without actually executing any of the functions I have written. I'm sure it's something fairly obvious, but I'd appreciate some help greatly.

My goal is to be able to make a linked list that I can add items to and display them, and ultimately move an item to the end of the list if the user wants to. I really appreciate your reading this. Here is my code:

class places  // class to hold all of the places to be inserted into the list
{

public:
    places();  // constructor
    void destroy(); // deconstructor
    void addToList(int placement); //adds items to list
    void displayAll(); // displays all items
    void displayFirst(); // displays first item and moves the first to the end       of the LLL

private:
    places * head;
    places * current;
    places * last;
    places * temp;

    char name[100];
    places * next;
}; 

places::places()
{
head = NULL;
last = NULL;
current = NULL;
temp = NULL;
int placement = 0;
}

void places::destroy()
{
delete[]head;
delete[]last;
delete[]current;
}

void places::addToList(int placement)
{


if (!head)
{
    current = new places;
    cout << "What is the name of the location you'd like to enter?" << endl;
    cin.get (name, 100, '\n');
    cin.ignore (200, '\n');
    current -> name;
    current -> next = NULL;
    last -> name; // attachs it to the last node
}
else
{
    head = new places;
    cout << "What is the name of the location you'd like to enter?" << endl;
    cin.get (name, 100, '\n');
    cin.ignore(200, '\n');
    head -> name;
    head -> next = NULL;
    last = head;
}
}
void places::displayAll()
{
if(!head)
{
    cout << "There are no items to display!" << endl;
    return;
}

temp = head;
cout << "Your favorite locations are: " << endl;

while(temp)
{
    cout << temp -> name << endl; // prints the current node
    //below sets the node to be printed to the next one
    if(temp -> next)
    {
        temp = temp -> next;
    }

    else
    {
        cout << "That's all there is!" << endl;
    }
}   
}

void places::displayFirst()
{
if(head)
{
    cout << head -> name << endl;
    while(temp!=NULL)
    {
        temp = temp -> next;
    }
    temp = head;
    delete[]head -> name;
}
 }

bool again()
{
char answer;

cout << "Do you want to enter another place? (Y/N)" << endl;
cin >> answer;

if(toupper(answer) == 'N')
{
    return false;
}

return true;
}
int main()
{
int i = 0;
places favPlaces;
int option;
do
{
    cout << "These are your options! PICK ONE!!!" << endl;
    cout << "\t1. Add to beginning of list\n";
        cout << "\t2. Add to end of list\n";
        cout << "\t3. Display the list\n";
        cout << "\t4. Display first item\n";
        cout << "\t5. DESTROY THE LIST!\n";


        cout << "Enter a choice : ";
        cin >> option;

    switch(option)
    {

        case 1:
            favPlaces.addToList();
            break;

        case 2: 
            favPlaces.addToList();
            break;

        case 3: 
            favPlaces.displayAll();
            break;

        case 4:
            favPlaces.displayFirst();
            break;

        case 5:
            favPlaces.destroy();
            break;

    }

i++;
}while(i < 4);


}   

Upvotes: 0

Views: 120

Answers (1)

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145457

The main thing I see with the code is failure to distinguish between information about the list (the places type) and information belonging to each node.

Here's a short tutorial on linked lists that I wrote in response to another such beginner's question.

I think that's the best help I can give here, that delving into the current code would not be fruitful. So, take a look at it. Perhaps try to work through the examples. :-)

Upvotes: 1

Related Questions