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

Link List Program I have a list of numbers that I need to know two things, what

ID: 3675589 • Letter: L

Question

Link List Program

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. Input file is LinkNbrs.dat.

Explanation / Answer

Comments attached

#include<stdio.h>
#include<stdlib.h>
#include <fstream>
#include <sstream>
//declaring numberNode struct
struct numberNode
{
int data;
int count;
struct numberNode* next;
};

//inserting new number function
void insert(struct numberNode** head_ref, int new_data)
{
struct numberNode* current = head_ref;
int totalcount = 0;
//checking for number already present or not
while (current != NULL)
{
if (current->data == search_for){
totalcount++;
int temp = current->count;
current->count = temp+1;
}
current = current->next;
}
  
//if number not present, create new one
if(totalcount == 0){
struct numberNode* new_node =
(struct numberNode*) malloc(sizeof(struct numberNode));

new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
}

//printing data
void printList(struct numberNode** head_ref){
struct numberNode* current = head_ref;
while (current != NULL)
{
cout<<"Number: "<<current->data<<" count: "<<current->count;
current = current->next;
}
}


/* Drier program to test count function*/
int main()
{
//head pointer declared
struct numberNode* head = NULL;
  
//reading file
std::ifstream readfile("LinkNbrs.dat");//reding numbers from text file
std::string eachline;
int count=0;
while (std::getline(readfile, eachline))
{
std::istringstream iss(eachline);
int n;   
while (iss >> n)//reading each value from file
{
//inserting inyo list
insert(&head, n);
}
}
//priting finally
printList(&head);
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote