#include <iostream>
usingnamespace std;
constshortunsignedint NUMVALS = 10;
struct ListNode {
int value;
ListNode* next;
};
ListNode* head = NULL;
bool insertNode(int num);
void displayList(void);
bool apendinglist (int num);
int main(void)
{
int dumVals[NUMVALS];
for (int i = 0; i<NUMVALS; i++)
{
cout<<"please enter your "<<i+1<<" number";
cin>>dumVals[i];
}
shortunsignedint i;
for(i=0; i<NUMVALS; i++) { // insert the array values into the linked list
if(insertNode(dumVals[i])) {
cout << "Error in function insertNode!\n";
cout << "Program will halt!\n";
exit(1);
}
}
displayList();
}
bool insertNode(int num)
// This function inserts a node into our linked list.
{
ListNode* newNode;
ListNode* nodePtr = head;
ListNode* prevNodePtr = NULL;
newNode = new ListNode;
if(newNode == NULL) {
cout << "Error allocating memory for new list member!\n";
return 1;
}
newNode->value = num;
newNode->next = NULL;
if(head==NULL) { // check if list is currently empty
cout << "List is currently empty - " << newNode->value;
cout << " is part of the list's first node!\n";
head = newNode;
}
else {
while(nodePtr != NULL && nodePtr->value<num) {
prevNodePtr = nodePtr; // advance prevNodePtr up to nodePtr
nodePtr = nodePtr->next; // advance nodePtr by 1 node
}
if(prevNodePtr == NULL) {// at beginning of list
newNode->next = head;
head = newNode;
}
else {
prevNodePtr->next = newNode;
newNode->next = nodePtr;
}
}
return 0;
}
void displayList(void)
// This function displays our linked list!
{
ListNode* nodePtr = head;
if(head == NULL) {
cout << "List is currently empty!\n";
}
else {
while (nodePtr != NULL) {
cout << nodePtr->value << endl;
nodePtr = nodePtr->next;
}
}
}
void deleteNode(int num)
// This function deletes a node, if it is in face there.
{
ListNode* nodePtr = head;
ListNode* prevNodePtr = NULL;
if(head==NULL) { // list is currently empty
cout << "The list is currently empty - ";
cout << "There are no nodes to delete!\n";
}
elseif(nodePtr->value == num) { // node is first one
head = nodePtr->next;
delete nodePtr;
}
else {
while(nodePtr != NULL && nodePtr->value != num) {
prevNodePtr = nodePtr;
nodePtr = nodePtr->next;
}
if(nodePtr == NULL) {
cout << "Your value (i.e., " << num << ")";
cout << " was not in the list!\n";
}
else {
prevNodePtr->next = nodePtr->next;
delete nodePtr;
}
}
return;
}
bool apendinglist(int num)
// This function inserts a node into our linked list.
{
ListNode* newNode;
ListNode* nodePtr = head;
ListNode* prevNodePtr = NULL;
newNode = new ListNode;
if(newNode == NULL) {
cout << "Error allocating memory for new list member!\n";
return 1;
}
newNode->value = num;
newNode->next = NULL;
if(head==NULL) { // check if list is currently empty
cout << "List is currently empty - " << newNode->value;
cout << " is part of the list's first node!\n";
head = newNode;
}
else
{
while(nodePtr->next != NULL)
nodePtr=nodePtr->next;
nodePtr->next = newNode;
}
return 0;
}
I am to write a destructor that destory the link list.
~Listnode to destory the list, but i do not have the knowledge to destory the list. how i do that? My approach is to write a for loop on deleting node but i think that's not hte correct approach anyone would like to help?
struct ListNode {
ListNode* next;
...
//this will delete the whole list by just deleting the head
// (if you don't want that, then you need to use a different approach like below)
~ListNode() {
if( next != NULL )
delete next;
}
//another method
staticvoid recursiveDelete(ListNode *node) {
if( node->next != NULL )
recursiveDelete(node->next);
delete node;
}
};
it's still not letting me do this next,
there are 2 problems in your method
1)line 18, is not valid "recursiveDelete identifier not found"
2)line 24~29 voidlinklist::listnode::recursivedelete(linklist *) overloaded member function not found in linklist::listnode (ignore my cap mistakes). I fix it by changing linklist to ListNode, but i can't fix problem one.