Hello. So I created this code for my last project for my CS class, yet I know there are problems with it but I need help analyzing this code. Would this work for dividing a linked list into two sublists at a given node? (By the way, I used a different already given function in this class to use as a sort of template for this function, so that's how I ordered my function.)
By the way, I think in this kind of linked list, the "first" pointer points to the last node, because it is a circular linked list and first->link would then point to the first node in the list. Or at least I think that is how it works.
void divideAt(circularLinkedList<Type> &secondList, const Type& item){
nodeType<Type> *current; //pointer to traverse the list
nodeType<Type> *trailCurrent;
bool found = false;
if (first == NULL) //Case 1; list is empty.
cout << "Can not split an empty list." << endl;
while (current != first && !found)
if (current->info == item)
found = true;
else
{
found = false;
trailCurrent = first;
current = first->link;
while (current != first && !found) {
if (current->info == item)
found = true;
else
{
trailCurrent = current;
current = current->link;
}
}
if (current == first)
{
if (first->info == item)
{
if (first == first->link)
cout << "Cannot split a list with only one node.";
else
{
trailCurrent->link = first->link;
(secondList.first)=current;
current->link = current;
}
}
else
cout << "The item to split at is not in the list." << endl;
}
elseif (current->info == item)
{
trailCurrent->link = first->link;
secondList.first = first;
secondList.first->link = current;
first = trailCurrent;
}
else
cout << "The item to split at is not in the list." << endl;
} //end else
}
Here is the function that creates a linked list when you are creating a linked list in main:
void insertNode(const Type& newitem)
{
nodeType<Type> *current; //pointer to traverse the list
nodeType<Type> *trailCurrent; //pointer just before current
nodeType<Type> *newNode; //pointer to create a node
bool found;
newNode = new nodeType<Type>; //create the node
newNode->info = newitem; //store newitem in the node
newNode->link = NULL; //set the link field of the node
//to NULL
if (first == NULL) //Case 1 e.g., 3
{
first = newNode;
first->link = newNode;
count++;
}
else
{
if (newitem >= first->info)//e.g., 25 > 3
{
newNode->link = first->link;
first->link = newNode;
first = newNode;
}
else
{
trailCurrent = first; //e.g., 1 < 3
current = first->link;
found = false;
while (current != first && !found)
if (current->info >= newitem)
found = true;
else
{
trailCurrent = current;
current = current->link;
}
trailCurrent->link = newNode;
newNode->link = current;
}
count++;
}//end else
}
Basically, I am just looking for feedback on the function I created (divideAt). Any feedback, both uplifting and constructive criticism would be helpful. Thank you!