Reputation: 105
Being a beginner for C++, I was able to get to the point of making a linkedlist using classes and not templates or vectors, and reached to a point where i can ad a node to the end, delete a node from the end (by value) and print the node. However, I now want to know how to be able to clear the complete set of nodes thereby clearing the complete linkedlist, without using any specialized functions or special headers.
I went through the following resources of reference but could not set or align my understanding with them as the following sources have done using different methods:-
Can't figure out how to clear a linked list in c++?
Linked List Delete Method (yes i tried getting ideas from java even though i dont know java but just tried too)
C programming Linked Lists delete node at position N
How would I clear a linked list? (came closest to make me understand it but some confusion)
Book: Deitel and Deitel (surprisingly near to my thought only that they have shown using templates or vectors)
Book: Primer (a little bit hard for me to understand)
Any help would be highly appreciated. Please try and forgive some of my bad habbits of programming like using namespace std and etc, etc, as my aim is to get the DeleteAll function working(clearing the complete list), look at the block of code which is commented totally in the last function of the class LinkedList. Then look at the Case 4 where i am trying to call it in the main function.this my first time with the concept of linked list, and yes it seems very intimidating.
#include <iostream>
#include <cstdlib>
#include "ctype.h" //for enabling the recognition of data types for different illegal user inputs for main program
using namespace std;
// Node class
class Node
{
int data;
Node* next;
public:
Node(){};
void setData(int aData)
{
data = aData;
}
void setNext(Node* aNext)
{
next = aNext;
}
int Data()
{
return data;
}
Node* Next()
{
return next;
}
};
// LinkedList Class
class LinkedList
{
private:
Node *head;
public:
List()
{
head = NULL;
}
// printing contents of list
void Print()
{
Node *tmp = head;
// but if no Nodes then following
if (tmp==NULL)
{
cout<<"\n \t\tcannot find any Nodes: List EMPTY\n";
return;
}
//if one node is found
else if (tmp->Next()==NULL)
{
cout<<tmp->Data();
cout<<"-->";
cout<<"NULL"<<endl;
}
//else more nodes then parse and print the list
else
{
do
{
cout<<tmp->Data();
cout<<"-->";
tmp = tmp->Next();
}
while(tmp!=NULL);
cout<<"";
cout<<"X"<<endl;
}
}
//Append or add a node to the linked list
void Append(int data)
{
//creates a new node;
Node *newNode = new Node();
newNode->setData(data);
newNode->setNext(NULL);
//create a temp pointer for further linking to be facilitated
Node *tmp = head;
//but before setting next link to point last node to new node
//we need to check if we are at the end of the list and if not then traverse to the end of node
if(tmp !=NULL)
{
while(tmp->Next() !=NULL)
{
tmp = tmp->Next();
}
tmp->setNext(newNode);
}
else
{
head = newNode;
}
}
//Delete a node from list BY VALUE
void Delete(int data)
{
//again create a temp pointer.
Node *tmp = head;
//again if no nodes
if (tmp==NULL)
{
cout<<"\n \t\tNo Nodes to delete\n";
return;
}
//Last node in the list which is the same as only one node left in list then following
else if(tmp->Next()==NULL)
{
delete tmp;
head = NULL;
}
//again parse through the nodes again to delete the data related node.
else
{
Node *prev;
do
{
if(tmp->Data()==data)
{
break;
}
else
{
prev = tmp;
tmp = tmp->Next();
}
}
while(tmp != NULL);
//once the data is found and located then readjust the linkage of previous with next and
//delete the current node
prev->setNext(tmp->Next());
delete tmp;
}
}
//
// int Deleteall()
// {
// again create a temp pointer.
// Node *tmp = head;
//
//
// again if no nodes
// if (tmp==NULL)
// {
// cout<<"\n \t\tNo Nodes to delete\n";
// return 0;
// }
//
// Last node in the list which is the same as only one node left in list then following
// else if(tmp->Next()==NULL)
// {
// delete tmp;
// head = NULL;
// }
//
// again parse through the nodes again to delete the data related node.
// else
// {
// Node *prev;
//
// while(tmp);
// {
//
// once the data is found and located then readjust the linkage of previous with next and
// delete the current node
// prev->setNext(tmp->Next());
// delete tmp;
// }
// } return 0;
// }
};
//Now call through the main function
int main()
{
//New List
LinkedList list1;
int usrdata, choice;
while(choice)
{
cout<<"\n\nPlease choose an action to be performed on the LinkedList\n\ninput 1 for adding/appending a node\ninput 2 for printing the list\ninput 3 for deleting the node by value\ninput 4 to exit\n\n";
cin>>choice;
switch(choice)
{
case 1 :
cout<<"\nEnter your desired data for adding/appending\n";
cin>>usrdata;
list1.Append(usrdata);
list1.Print();
continue;
case 2 :
cout<<"\nPrinting\n";
list1.Print();
break;
case 3 :
cout<<"\nEnter your desired data for removal/deleting\n";
cin>>usrdata;
list1.Delete(usrdata);
list1.Print();
break;
case 4 :
cout<<"\n deleting n exiting...\n";
// list1.Deleteall();
list1.Print();
goto end;
default :
cout<<"I think you should go home now you seem tired\n";
}
}
end:
cout<<"\n\nThis statement was reached because either you chose to exit or entered a wrong/illegal value or operation\n\n";
system("pause");
return 0;
}
How to get the last function (completely commented lines) DeleteAll working to clear the entire list without using any other specialized headers or special functions etc.
Upvotes: 0
Views: 155
Reputation: 2520
Delete all is actually a simple process.
Here is the algorithm:
current node is head
while the current node is not null
get the next node
delete the current node
current node is next node
set head to null
You don't have to worry about maintaining links as you are removing everything.
Upvotes: 1