I'm making a simple linked list class, but when I display the current values, it will display the values but then the compiler will abruptly break and the unhandled exception error produces..
#include <iostream>
usingnamespace std;
class NodeType
{
private:
int info; // data
NodeType * next;// pointer to next node in the list
friendclass list;
};
class list
{
private:
NodeType *head;
public:
list(); //default constructor
void add(int data);
void addFront(int data);
void display() const;
~list();
};
// default constructor
list::list()
{
head = NULL;
}
void list::add(int data)
{
NodeType*ptr = new NodeType;
ptr->info = data;
if (head == NULL) //if empty list
{
//need to add to the front
head = ptr;
ptr->next = NULL;
}
else
{
NodeType*current = head;
while (current->next != NULL) // as long as next node is not null
{
current = current->next; // keep traversing till you reach the end
}
current->next = ptr;
}
ptr = NULL;
}
void list::addFront(int data)
{
NodeType*ptr = new NodeType;
ptr->info = data;
ptr->next = NULL;
if (head == NULL) //list is empty
{
head = ptr;
}
else
{
head = ptr;
ptr->next = head->next;
}
}
void list::display() const
{
NodeType*current = head;
while (current)
{
cout << current->info << " ";
current = current->next;
}
}
list::~list()
{
NodeType * current = head;
while (current)
{
NodeType *temp = current;
current = current->next;
delete temp;
}
current = NULL;
head = 0;
}
int main()
{
//going to add 2 values
list one;
int addinfo;
cout << "Enter a value you would like to add: " << endl;
cin >> addinfo;
one.add(addinfo);
cout << "Enter another value you would like to add: " << endl;
cin >> addinfo;
one.add(addinfo);
//display the current info
one.display();
system("PAUSE");
return 0;
}
while (current->next != NULL) // as long as next node is not null
{
current = current->next; // keep traversing till you reach the end
}
current->next = ptr; //Hmm, what's the value of "next"?
Like cire said, when will this loop exit? "next" has to not satisfy the condition there. What is curent->next going to be after it exits?
int main()
{
//going to add 2 values
list one;
int addinfo;
char choice;
do
{
cout << "Welcome to the linked list tutorial!" << endl;
for (int i = 0; i < 40; i++)
{
cout << "*";
}
cout << endl;
cout << "A) To insert a value\nB) To print out the current values\nQ) Quit" << endl;
cin >> choice;
choice = toupper(choice);
if (choice == 'A')
{
cout << "Enter a value to add to the list: " << endl;
cin >> addinfo;
one.add(addinfo);
}
elseif (choice == 'B')
{
cout << "Printing current values..." << endl;
one.display();
}
else
cout << "Invalid!";
} while(choice != 'Q');
system("PAUSE");
return 0;
}
What do you mean? Isn't that what while(choice!='Q') does? isn't this condition saying, as long as the choice doesn't equal Q, redo the menu, if it does equal Q, exit the loop?
What happens if the user enters 'Q'? It will print "Invalid" and then exit the loop. It's doesn't impede functionality, it just prints "Invalid" in one case when it shouldn't.