Create a directory called lab10 and move into that directory with cd lab10 Compl
ID: 3815639 • Letter: C
Question
Create a directory called lab10 and move into that directory with cd lab10 Complete the lab10.c program shown below (you must write the two functions whose signatures are shown in red). The program reads a file of integers (the filename is specified on the command line) and builds a linked list of the unique integer values and a count of how many times that integer value has been seen. addOrdered - adds the new integer to the existing list in numerical order (smallest to largest). If this is the first time the number has been added to the list, create a new node and set the count for that node to one (1). If the number already exists in the list, then increment the count for that number (do not create a second node for that number). printList - prints the ordered list of numbers (and their counts) #include #include typedef struct node {int value; int count; struct node *next;} Node; Node *addOrdered(Node *, int); void printList(Node *); int main(int argc, char *argv[]) {Node *head = NULL; if (argc != 2) {printf("Usage: ./a.out datafile "); exit(1);} FILE *fp = fopen(argv[1], "r"); if (! fp) {printf("File %s does not exist ", argv[1]); exit(1);} int num; fscanf(fp, "%d", #); while (! feof(fp)) {head = addOrdered(head, num); fscanf(fp, "%d", #);} printList(head); return 0;} Do not modify the code shown above, simply complete the two functions needed for its execution A sample execution with the data set data is shown below First, on your local machine, compress your lab10 directory into a single (compressed) file. Second, once you have a compressed file that contains your lab10 program, submit that file to Blackboard.Explanation / Answer
Node *addOrdered(Node *head, int num)
{
Node *curr = head;
Node *prev;
if(head == null)
{
Node* temp = (Node*) malloc(sizeof( Node ));
temp->value = num;
temp->count = 1;
return temp;
}
while(curr!=null)
{
if(curr->value == num){
curr->count +=1;
break;
}
else if(curr->value < num){
prev = curr;
curr = curr->next;
}
else{
Node* temp = (Node*) malloc(sizeof( Node ));
prev->next = temp;
temp->next = curr;
break;
}
}
return head;
}
PrintList function
void printList(Node *head)
{
Node *curr = head;
while(curr!=n)
{
printf(" Number : %d Count : %d",curr->value,curr->count);
curr = curr->next;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.