Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Link List Program Input file is LinkNbrs.dat. Text File located: http://past

ID: 3675672 • Letter: C

Question

C++ Link List Program

Input file is LinkNbrs.dat. Text File located: http://pastebin.ca/3391307

I have a list of numbers that I need to know two things, what numbers I have and how many of each number do I have in the list. So you are to read in each number and add the number to an ordered link list. Now, if you are adding a number to the list and you find that number, then you will just count it. If you do not find the number in the link list, then you will add a new node to the link list with the new number and set the count to one.

The structure/class will have three fields, the number, a counter. The counter is used to keep count of how many of this number exist and of course a link field.

Print out the values and their counts say 5 per line.

   example     5 – 22   8 – 15   13 – 5 22 – 3 25 – 18   where the 5 is the number and 22 is its count (ie there were 22 number 5’s in the list). Label output please.

Print out the sum and average of the set of input values.

Print out the number of nodes in the link list.

Explanation / Answer

Answer:

#include<iostream>
#include<string>
#include<fstream>
using namespace std;
struct listNode
{
   int nodeData;
   int dataCount;
   listNode* nextListNode;
};
class orderedLinkedList
{
   listNode* headNode;
   int n;
public:
   orderedLinkedList()
   {
       headNode=NULL;
       n=0;
   }
  
   void insertIntoList(int dataIn)
   {
       listNode* mlist,mlist1;
       listNode* myNewNode=new listNode;
       myNewNode->nodeData=dataIn;
       myNewNode->dataCount=1;
       if(headNode==NULL)
       {          
           myNewNode->nextListNode=headNode;
           headNode=myNewNode;
           n++;
       }
       else if(listContains(dataIn))
       {
           mlist=headNode;
           while(mlist!=NULL)
           {
               if(mlist->nodeData==dataIn)
               {
                   mlist->dataCount=mlist->dataCount+1;
                   break;
               }  
               mlist=mlist->nextListNode;
           }          
       }
       else if(headNode->nodeData>=dataIn)
       {
           myNewNode->nextListNode=headNode;
           headNode=myNewNode;
           n++;
       }      
       else
       {
           mlist1=headNode;
           while(mlist1->nextListNode!=NULL&& mlist1->nodeData<dataIn)
           {
               mlist1=mlist1->nextListNode;
           }
           myNewNode->nextListNode=mlist1->nextListNode;
           mlist1->nextListNode=myNewNode;
           n++;
       }
   }
   void displayList()
   {
      
       listNode* mlist=headNode;
       cout<<"List:"<<endl;
       while(mlist!=NULL)
       {
           cout<<mlist->nodeData<<"-"<<mlist->dataCount<<endl;
           mlist=mlist->nextListNode;
       }
   }
   bool listContains(int dataIn)
   {
       listNode* mlist=headNode;
       while(mlist!=NULL)
       {
           if(mlist->nodeData==dataIn)
           {
               return true;
           }
           mlist=mlist->nextListNode;
       }
       return false;
   }
   int orderedLinkedListSize()
   {
       return n;
   }
   int findListSum()
   {
       int lSum=0;
       listNode* mlist=headNode;
       while(mlist!=NULL)
       {
           lSum+=mlist->nodeData;
           mlist=mlist->nextListNode;
       }
       return lSum;
   }
};
int main()
{
   orderedLinkedList newmyList;
   int t;
   ifstream linkFile("LinkNbrs.dat");
   if(linkFile.is_open())
   {
       while(linkFile>>t)
       {      
           newmyList.insertIntoList(t);
       }
   }
   linkFile.close();  
   newmyList.displayList();
   int myLstSize=newmyList.orderedLinkedListSize();
   cout<<" Size:"<<myLstSize;
   cout<<" Sum:"<<newmyList.orderedLinkedListSize();
   cout<<" Avg:"<<newmyList.orderedLinkedListSize()/myLstSize;
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote