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

For this homework, you will write a program to create and manipulate a simple li

ID: 3689562 • Letter: F

Question

For this homework, you will write a program to create and manipulate a simple linked list. For each node in the linked list you will generate a random number, create a node that holds that number, and insert that node into its sorted position in the linked list. Once the nodes are inserted. you will traverse the list, printing the value held in each node. Then you will clean up the list (deleting all the nodes) and exit the program. You will also learn about a tool that you can use to help you check for memory errors in your code. 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) insertXodeSorted() - 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 o 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

Explanation / Answer

/*

* This progrm use for create link list, add node, print list and free list.

* In this program define main,insertNodeSorted,printList,deleteList.

*

*/

#include<stdio.h>

#include<stdlib.h>

//linked list node

struct node

{

// Any type of data store in this node

void *data;

struct node *next;

};

// this function use for add node start in link list

void insertNodeSorted(struct node** head_ref, void *new_data, size_t data_size)

{

// Allocate memory for node

struct node* new_node = (struct node*)malloc(sizeof(struct node));

new_node->data = malloc(data_size);

new_node->next = (*head_ref);

// move new_data to memory.

int i;

for (i=0; i<data_size; i++)

*(char *)(new_node->data + i) = *(char *)(new_data + i);

// add new node

(*head_ref) = new_node;

}

// This funcation use for print link list node value

void printList(struct node *node, void (*fptr)(void *))

{

while (node != NULL)

{

(*fptr)(node->data);

node = node->next;

}

}

// Function to print a integer value with pintList function

void printInt(void *n)

{

printf(" %d", *(int *)n);

}

// this function use for delete link list

void deleteList(struct node** head_ref)

{

struct node* current = *head_ref;

struct node* next;

// check every node null and not

while (current != NULL)

{

next = current->next;

// free current node here

free(current);

current = next;

}

/* deref head_ref to affect the real head back

in the caller. */

*head_ref = NULL;

}

// main function. in this funcation we call another function

int main()

{

// define some variable here

struct node *start = NULL;

int i, j, n;

time_t t;

int arr[5];

n = 5;

//call random number generator

srand((unsigned) time(&t));

// Create inager type link list here

unsigned int_size = sizeof(int);

//Print 5 random numbers between 0 to 4. if you want change link list lenght change value of n variable on top in the main function

// if you want change random number then chnage 4 with your requirement

for( j = 0 ; j < n ; j++ ){

// add random value in array

arr[j] = (rand() % (4));

}

// this loop use for add new node according to array

for (i=n; i>=0; i--){

// this function use for insert new node in link list

insertNodeSorted(&start, &arr[i], int_size);

}

printf("Create new linkedlist integer type:- ");

// this function use for print link list

printList(start, printInt);

// print start deleteing message here

printf(" Start deleting operation.....");

// this function use for deleting all node.

deleteList(&start);

// print end deleteing message here

printf(" End delete operation.");

return 0;

}

Output this this program:-

Create new linkedlist integer type:-

130310

Start deleting operation.....

End delete operation.

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