The goal of this assignment is to complete my teachers linke
The goal of this assignment is to complete my teachers linked list he made in class. He commented out at the end what needs to be added. He also stated that you must leave the variables that are private still private and that you are expected to fix any errors he might have in his code.
Link to the code
http://pastebin.com/Dm5rWNyF
Solution
Error 1:
CInetNode () is misspelled, it is a constructor so it should be CIntNode ()
Error 2:
Line no 30 ; if (m_pListAnchor->m_nData == key) should be if (m_pListAnchor->m_nData == nKey)
Error 3:
Line no 38 ; if (m_pListAnchor->m_nData == key) should be if (m_pListAnchor->m_nData == nKey)
Error 4:
DeleteNode (key, pCurrent, pCurrent->m_pNext); should be DeleteNode (nKey, pCurrent, pCurrent->m_pNext);
Error 5:
DeleteNode (nKey, pCurrent, pCurrent->m_pNext); should be Private_DeleteNode (nKey, pCurrent, pCurrent->m_pNext); as calling a three parameter function.
Error 6:
Line no 68 calls rand() function which is in stdlib.h header file so we add #include <stdlib.h>
Error 7:
Line no 68 :Private_AddNode_Depth (nData, _pListAnchor, (rand () / (double) RAND_MAX) * m_nNodeCounter); should be Private_AddNode_Depth (nData, m_pListAnchor, (rand () / (double) RAND_MAX) * m_nNodeCounter);
Error 8:
Line no 77: if (pCurrent->m_pNext == NULL || nDepth = 0) { should be if (pCurrent->m_pNext == NULL || nDepth == 0) assignmnet operator
Error 9:
Line no 82: AddNode(nData, pCurrent->m_pNext, nDepth - 1); should be Private_AddNode_Depth (nData, pCurrent->m_pNext, nDepth - 1);
Error 10:
Line no 106:AddNode(nData, pCurrent->m_pNext); should be Private_AddNode(nData, pCurrent->m_pNext);
Error 11:
Line no 112:
int main (int argc, char* argv) should be int main (int argc, char** argv)
************************************************************************************************
FOLLOWING Corrected CODE :
Additionally Contains:
print function to print data
Median function to find median by using sort function.
Average function to compute average.
Sample Output is:
Data is:
6
7
1
9
10
Sorted array is:1 6 7 9 10
Median is 7
Average of List is: 6.000000
************************************************************************************************
#include <stdio.h>
#include <stdlib.h>
class CIntNode {
public:
int m_nData;
CIntNode* m_pNext;
CIntNode () {
m_nData = -1;
m_pNext = NULL;
}
};
class CLinkedList {
private: // leave them private
CIntNode* m_pListAnchor;
int m_nNodeCounter;
public:
CLinkedList () {
m_pListAnchor = NULL;
m_nNodeCounter = 0;
}
bool DeleteNode (int nKey) {
if (m_pListAnchor == NULL) {
return false;
} else if (m_pListAnchor != NULL && m_pListAnchor->m_pNext == NULL) {
if (m_pListAnchor->m_nData == nKey) {
delete m_pListAnchor;
m_pListAnchor = NULL;
return true;
} else {
return false;
}
} else {
if (m_pListAnchor->m_nData == nKey) {
CIntNode* pTmp = m_pListAnchor->m_pNext;
delete m_pListAnchor;
m_pListAnchor = pTmp;
return true;
} else {
Private_DeleteNode (nKey, m_pListAnchor, m_pListAnchor->m_pNext);
}
}
}
// delete one node with matching key value
bool Private_DeleteNode (int nKey, CIntNode* pPrevious, CIntNode* pCurrent) {
if (pCurrent == NULL) { // Case 1: m_pListAnchor = NULL
return false;
} else { // Case 2: m_pListAnchor != NULL
if (pCurrent->m_nData == nKey) {
pPrevious->m_pNext = pCurrent->m_pNext;
delete pCurrent;
return true;
} else {
Private_DeleteNode (nKey, pCurrent, pCurrent->m_pNext);
}
}
}
// add one node at the end of the link
void AddNodeatRandomLoc (int nData) {
m_nNodeCounter++;
Private_AddNode_Depth (nData, m_pListAnchor, (rand () / (double) RAND_MAX) * m_nNodeCounter);
}
void Private_AddNode_Depth (int nData, CIntNode* pCurrent, int nDepth) {
if (pCurrent == NULL) { // Case 1: m_pListAnchor = NULL
CIntNode* pNew = new CIntNode;
pNew->m_nData = nData; // same to (*pNew).m_nData = nData;
m_pListAnchor = pNew;
} else { // Case 2: m_pListAnchor != NULL
if (pCurrent->m_pNext == NULL || nDepth == 0) {
CIntNode* pNew = new CIntNode;
pNew->m_nData = nData; // same to (*pNew).m_nData = nData;
pCurrent->m_pNext = pNew;
} else {
Private_AddNode_Depth (nData, pCurrent->m_pNext, nDepth - 1);
}
}
}
// add one node at the end of the link
void AddNode (int nData) {
m_nNodeCounter++;
Private_AddNode (nData, m_pListAnchor);
}
void Private_AddNode (int nData, CIntNode* pCurrent) {
if (pCurrent == NULL) { // Case 1: m_pListAnchor = NULL
CIntNode* pNew = new CIntNode;
pNew->m_nData = nData; // same to (*pNew).m_nData = nData;
m_pListAnchor = pNew;
} else { // Case 2: m_pListAnchor != NULL
if (pCurrent->m_pNext == NULL) {
CIntNode* pNew = new CIntNode;
pNew->m_nData = nData; // same to (*pNew).m_nData = nData;
pCurrent->m_pNext = pNew;
} else {
Private_AddNode(nData, pCurrent->m_pNext);
}
}
}
void sort(int arr[],int n)
{ int a;
for (int i = 0; i < n; ++i)
{
for (int j = i + 1; j < n; ++j)
{
if (arr[i] > arr[j])
{
a = arr[i];
arr[i] = arr[j];
arr[j] = a;
}
}
}
}
void print()
{
CIntNode *tmp=m_pListAnchor;
printf(\"Data is:\ \");
while(tmp->m_pNext!=NULL)
{
printf(\"%d\ \", tmp->m_nData);
tmp=tmp->m_pNext;
}
printf(\"%d\ \", tmp->m_nData);
}
void median()
{
CIntNode *tmp=m_pListAnchor;
int arr[5],i=0;
while(tmp->m_pNext!=NULL)
{
arr[i]=tmp->m_nData;
i++;
tmp=tmp->m_pNext;
}
arr[i]=tmp->m_nData;
i++;
sort(arr,i);
printf(\"Sorted array is:\");
for(int j=0;j<i;j++)
{
printf(\"%d \",arr[j]);
}
printf(\"\ \ Median is %d \ \",arr[i/2]);
}
void average()
{
CIntNode *tmp=m_pListAnchor;
int count=0,sum=0;
float avg;
while(tmp->m_pNext!=NULL)
{
sum=sum+tmp->m_nData;
count++;
tmp=tmp->m_pNext;
}
sum=sum+tmp->m_nData;
count++;
avg=sum/count;
printf(\"\ Average of List is: %f \ \ \",avg);
}
};
int main (int argc, char** argv)
{
CLinkedList list;
list.AddNode (5);
list.AddNode (6);
list.AddNode (7);
list.DeleteNode (5);
list.AddNode (2);
list.AddNode (1);
list.AddNode (9);
list.AddNode (10);
list.DeleteNode (2);
list.print();
// printf(\"%d\ \", list.m_nNodeCounter);
// what is the median node value on the list (2 ponits)
list.median();
// what is the average node value on the list (2 points)
list.average();
}
****************************************







