I need help sorting by Last Name my Linked List

This is what I have for now.
I need to :

1.Modify function Find() to locate a person by last name in the linked list.

2.Function Sort() shall organize the linked list by last name.

3.Function Print() shall write to file each link of the linked list.

4.All CPerson attributes shall be printed to file as well as the address of the link
.


#include "stdafx.h"
#include "LinkedList.h"


//*******************************************************************
// Default Constructor
//*******************************************************************
CLinkedList::CLinkedList(void)
{
pLinkList = nullptr;
}

//*******************************************************************
// Destructor
//*******************************************************************
CLinkedList::~CLinkedList(void)
{
}

//*******************************************************************
// Function:Push()
//*******************************************************************
void CLinkedList::Push(CPerson* pP)
{
CPerson* pAdr = nullptr;

if (pLinkList == nullptr)
{
pLinkList = pP;
pP->pNext = nullptr;
}
else
{ // The linked list already exist, push the person object onto the list
pAdr = pLinkList;

// Go to the end of the List
while(pAdr->pNext != nullptr)
{
pAdr = pAdr->pNext;
}

// Push person object onto the List
pAdr->pNext = pP;
pP->pNext = nullptr;
}

return;
}

//*******************************************************************
// Function:Pop()
//*******************************************************************
void CLinkedList::Pop()
{
CPerson* pAdr;

if (pLinkList != nullptr)
{
pAdr = pLinkList;
if (pAdr->pNext == nullptr)
{ // Delete the beginning (Head) of the Linked List
delete pAdr;
pLinkList = nullptr;
return;
}

// Go to the next to last node in the list
while(pAdr->pNext->pNext != nullptr)
{
pAdr = pAdr->pNext;
}

// Pop person object of the List
delete pAdr->pNext;
pAdr->pNext = nullptr;
}

return;
}

//*******************************************************************
// Function:Find()
//*******************************************************************
CPerson* CLinkedList::Find(int Node)
{
CPerson* pAdr = pLinkList;

if (pAdr->NodeId == Node)
return pAdr;

while(pAdr->pNext != nullptr)
{
pAdr = pAdr->pNext;
if (pAdr->NodeId == Node)
return pAdr;
}

return 0x0000;
}

//*******************************************************************
// Function:FindLastName()
//*******************************************************************
CPerson* CLinkedList::FindLastName(string Name)
{
CPerson* pAdr = pLinkList;
if (!pAdr->LastName.compare(Name))
return pAdr;

while(pAdr->pNext != nullptr)
{
pAdr = pAdr->pNext;
if (pAdr->LastName.compare(Name))
return pAdr;
}

return 0x0000;
}

//*******************************************************************
// Function:Insert()
//*******************************************************************
void CLinkedList::Insert(CPerson* pFP, CPerson* pP)
{
// pP is inserted after pFP

pP->pNext = pFP->pNext;
pFP->pNext = pP;

return;
}

//*******************************************************************
// Function:Remove()
//*******************************************************************
void CLinkedList::Remove(CPerson* pFP)
{
CPerson* pAdr = pLinkList;

// Not dealing with the case where we are deleting the head of the list.

while(pAdr->pNext != pFP)
{
pAdr = pAdr->pNext;
}

CPerson* pTemp = pAdr->pNext->pNext;
delete pAdr->pNext;
pAdr->pNext = pTemp;

return;
}

//*******************************************************************
// Function:Sort()
//*******************************************************************
void CLinkedList::Sort(void)
{
// Create a pointeer where we last inserted an object into the Linked List
CPerson* pLastSort = pLinkList;

char Alpha = 'A';
while(Alpha != 'Z')
{
// FindLName is just like function FindLastName, except
// we need to compare the first character in the Last Name,
// not the entire string.
CPerson* pP = FindLName(pLastSort, Alpha);
if (pP == nullptr)
{
Alpha++;
}
else
{


// Keep a pointer to the previous link which is pPrevious
// Defined globally to the class
// pLastSort is the place where we want to insert the object pP.
Disconnect(pP, pPrevious);
Insert(pLastSort, pP);
}
}

return;
}

//*******************************************************************
// Function:Print()
//*******************************************************************
void CLinkedList::Print(void)
{
FILE* fptr = fopen("./LLtxt", "a");
if (fptr == nullptr)
{
// Couldnt open file.
return;
}

CPerson* pAdr = pLinkList;
while(true)
{
fprintf(fptr, "Node Address = 0x%x \n Node Number = %d \n pNext = 0x%x \n",
pAdr, pAdr->NodeId, pAdr->pNext);

if (pAdr->pNext == nullptr)
break;
else
{
pAdr = pAdr->pNext;
}

}
fclose(fptr);
return;
}


//*******************************************************************
// Function:Disconnect()
//*******************************************************************
void CLinkedList::Disconnect(CPerson* pP, CPerson* pPrevious)
{
// This step removed object pP from the Linked List
pPrevious->pNext = pP->pNext;
pP->pNext = nullptr;

return;
}

//*******************************************************************
// Function:FindLName()
//*******************************************************************
CPerson* CLinkedList::FindLName(CPerson* pLastSort, char Alpha)
{
// Do some work here.

return 0x0000;
}
Topic archived. No new replies allowed.