bool DelOddCopEven(Node* headPtr) { if (headPtr == nullptr) { return false; }
bool DelOddCopEven(Node* headPtr) {
if (headPtr == nullptr) {
return false;
}
Node* prev = nullptr;
Node* current = headPtr;
while (current != nullptr) {
if (current->data % 2 != 0) {
// Odd-valued node, delete it
if (prev != nullptr) {
prev->link = current->link;
} else {
headPtr = current->link;
}
Node* temp = current;
current = current->link;
delete temp;
} else {
// Even-valued node, create a copy and insert it after the original node
Node* newNode = new Node;
newNode->data = current->data;
newNode->link = current->link;
current->link = newNode;
// Update the previous and current pointers
prev = newNode;
current = newNode->link;
}
}
return true;
}
int FindListLength(Node* headPtr)
{
int length = 0;
while (headPtr != 0)
{
++length;
headPtr = headPtr->link;
}
return length;
}
bool IsSortedUp(Node* headPtr)
{
if (headPtr == 0 || headPtr->link == 0) // empty or 1-node
return true;
while (headPtr->link != 0) // not at last node
{
if (headPtr->link->data < headPtr->data)
return false;
headPtr = headPtr->link;
}
return true;
}
void InsertAsHead(Node*& headPtr, int value)
{
Node *newNodePtr = new Node;
newNodePtr->data = value;
newNodePtr->link = headPtr;
headPtr = newNodePtr;
}
void InsertAsTail(Node*& headPtr, int value)
{
Node *newNodePtr = new Node;
newNodePtr->data = value;
newNodePtr->link = 0;
if (headPtr == 0)
headPtr = newNodePtr;
else
{
Node *cursor = headPtr;
while (cursor->link != 0) // not at last node
cursor = cursor->link;
cursor->link = newNodePtr;
}
}
void InsertSortedUp(Node*& headPtr, int value)
{
Node *precursor = 0,
*cursor = headPtr;
while (cursor != 0 && cursor->data < value)
{
precursor = cursor;
cursor = cursor->link;
}
Node *newNodePtr = new Node;
newNodePtr->data = value;
newNodePtr->link = cursor;
if (cursor == headPtr)
headPtr = newNodePtr;
else
precursor->link = newNodePtr;


Trending now
This is a popular solution!
Step by step
Solved in 2 steps









