Define a struct appropriate for holding a random integer. This struct will also
ID: 3689193 • Letter: D
Question
Define a struct appropriate for holding a random integer. This struct will also contain a "next" pointer to reference a separate instance of the struct. You may use the typedef keyword to give your struct a separate typename if you wish, but this is not a requirement. The program will read command line arguments. The first argument is the program name (as it will always be) followed by a random number seed, the number of random numbers to generate and ending with the Maximum Possible Value of the Random numbers generated (i.e. argc should be 4). Your program will use a "dummy node" for the linked list. In other words, the head of the list will be an actual node, but the data field in that node will not be used for sorting purposes. Your program must contain the following functions: main() (for obvious reasons) insertNodeSorted() - You may implement this either of two ways. One way is to use the head of the list as one parameter and the integer value as the second. The function will allocate memory for the node, initialize it and then insert it in the proper location in the list. The other way is to pass the head of the list as one parameter and a previously allocated and initialized node as the other. The existing node is inserted in the proper location in the list. printList() - Takes the head of the list as its only parameter, traverses the list, printing out the data in sorted order. deleteList() - Takes the head of the list as its only parameter, traverses the list, deleting all nodes. The basic algorithm of your program is Use a loop (based upon command line input values) that: Generates a random number Prints the number to stdout Creates a node that contains that random number Inserts the new node in sorted order into the existing list Print the sorted linked list Delete the linked list You will use a Makefile to compile your program.Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct node {
int data;
struct node *next;
};
typedef struct node* Number;
void insertNodeSorted(Number head, int data) {
Number num;
Number cur = head;
while(cur->next != NULL)
cur = cur->next;
num = (struct node* ) malloc( sizeof(struct node));
num->data = data;
num->next = NULL;
cur->next = num;
cur = cur->next;
}
void print(Number head) {
Number cur;
cur = head->next;
while(cur != NULL) {
printf("%d ",cur->data);
cur = cur->next;
}
}
int main( int argc, char *argv[]) {
Number head = NULL;
if(argc != 4) {
printf("Invalid arguments: Arguments must be 3 - seed, no Of random numbers, numbers maximum");
return 1;
}
head = (struct node* ) malloc( sizeof(struct node));
head->next = NULL;
// int j;
// for(j = 0; j < argc; j++)
// printf("%s", *(argv[j]));
int seed = atoi(argv[1]);//time(NULL);
int noOfints = atoi(argv[2]);
int max = atoi(argv[3])+1;//10;
printf("%d %d %d ",seed,noOfints,max);
int r;
srand(seed);
int i =0;
for(i = 0; i < noOfints; i++) {
r = rand();
printf("Number#%d generated: %d ",(i+1),(r % max));
insertNodeSorted(head, r % max);
}
print(head);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.