I am trying to take the two linkedlist that I have in my executable file and merge them into each other in alternating positions. Ex. ListOne 1,2,3 and ListTwo 4,5 the new ListOne should be 1,4,2,5,3.
LinkedList .h file:
class LinkedList
{
private:
struct ListNode
{
string firstName;
string lastName;
long int phoneNumber;
struct ListNode *next;
};
ListNode *head;
public:
LinkedList()
{
head = nullptr;
}
~LinkedList();
void appendNode(string f, string l, long int p);
void displayList();
};
LinkedList .cpp file:
LinkedList::~LinkedList()
{
cout << "LinkList destructor" << endl;
}
void LinkedList::appendNode(string f, string l, long int p)
{
ListNode *newNode;
ListNode *nodePtr;
newNode = new ListNode;
newNode -> firstName = f;
newNode -> lastName = l;
newNode -> phoneNumber = p;
newNode -> next = nullptr;
if (!head)
head = newNode;
else
{
nodePtr = head;
while (nodePtr -> next)
//while nodePtr is pointing to another node
nodePtr = nodePtr -> next;
//move to that node
nodePtr -> next = newNode;
//inset the newNode at the end of the linked list
}
}
void LinkedList::displayList()
{
ListNode *nodePtr;
nodePtr = head;
while(nodePtr)
//while nodePtr is true, meaning there is a node in the list
{
cout << nodePtr -> firstName << endl;
cout << nodePtr -> lastName << endl;
cout << nodePtr -> phoneNumber << endl;
nodePtr = nodePtr -> next;
}
}
Executable file:
LinkedList ListOne;
LinkedList ListTwo;
ListOne.appendNode("Cate", "Beckem", 7704563454);
ListOne.appendNode("Cabe","Tomas", 7703451523);
ListTwo.appendNode("Mary", "Smith", 4043456543);
ListTwo.appendNode("Mark", "Carter", 4045433454);
My programs runs perfectly including the displayList function. I am just very confused how to go about making a merge function.