Question

In: Computer Science

In C Language. build a singly linked list where each node stores a randomly generated value...

In C Language. build a singly linked list where each node stores a randomly generated value on [0,1]. Keep the list sorted. Generate some number of nodes at startup. Print out the list formatted as e.g. → 0.04,0.19,0.27,0.33,0.54,0.66,0.75,0.99

Solutions

Expert Solution

Hi. I have answered similar questions before. Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

#include<stdio.h>

#include <stdlib.h>

#include<time.h>

//structure representing a node

struct ListNode{

                double data;

                struct ListNode *next;

};

//method to create a node with given data and return it

struct ListNode* createNode(double data){

                //creating memory dynamically, for a ListNode

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

                //setting data

                node->data = data;

                //NULL value for next field

                node->next = NULL;

                return node;

}

//method to insert a data at proper position, so that the list is sorted

//returns the head node of the updated list

struct ListNode* insertSorted(struct ListNode* head, double data){

                //creating node

                struct ListNode* node=createNode(data);

                //if list is empty, adding as head node

                if(head==NULL){

                                head=node;

                                return head;

                }else{

                                //checking if item can be added before head

                                if(data<head->data){

                                               //adding before head

                                               node->next=head;

                                               head=node;

                                               return head;

                                }

                                //now we check each adjacent pair of nodes, to see if we can

                                //add new item at the middle

                                struct ListNode* temp=head;

                                struct ListNode* temp2=head->next;

                                //looping until end of list

                                while(temp2!=NULL){

                                               //checking if item can be added in between temp and temp2

                                               if(data>=temp->data && data<temp2->data){

                                                               //adding to the middle

                                                               node->next=temp2;

                                                               temp->next=node;

                                                               return head;

                                               }

                                               temp=temp->next;

                                               temp2=temp2->next;

                                }

                                //if program control reached this line, it means that the item is not

                                //added anywhere, so adding at the last

                                temp->next=node;

                                return head;

                }

}

//method to print a linked list comma separated.

void printList(struct ListNode* head){

                //taking a reference to head.

                struct ListNode* temp=head;

                //looping and printing data of each node, formatted with 2 digits precision

                while(temp!=NULL){

                                printf("%.2f",temp->data);

                                temp=temp->next;

                                //if this is not last node, printing a comma and space

                                if(temp!=NULL){

                                               printf(", ");

                                }

                }

                printf("\n");

}

//method to deallocate memory occuppied by nodes, to prevent memory leakage

void freeMemory(struct ListNode* head){

                struct ListNode* temp=head;

                while(temp!=NULL){

                                struct ListNode* temp2=temp;

                                temp=temp->next;

                                free(temp2);

                }

}

int main(){

                //seeding random number generator with time.

                srand(time(NULL));

                //creating a list, adding 20 random values

                struct ListNode* head=NULL;

                for(int i=0;i<20;i++){

                                //generating a value between 0 and 1

                                double randNum=(rand()/(double)RAND_MAX);

                                //adding to list, updating head.

                                head=insertSorted(head,randNum);

                }

                //printing list, it should be in sorted order.

                printList(head);

                //de allocating memory, and exiting

                freeMemory(head);

                return 0;

}

/*OUTPUT*/

0.09, 0.15, 0.17, 0.22, 0.29, 0.29, 0.34, 0.46, 0.48, 0.48, 0.49, 0.51, 0.53, 0.57, 0.59, 0.69, 0.74, 0.90, 0.94, 0.94


Related Solutions

I've provided a Node class that implements a node of a simple singly-linked list (with .value...
I've provided a Node class that implements a node of a simple singly-linked list (with .value and .next fields), and an empty LinkedList class. Your task is to implement LinkedList.sort(l), where given the node l as the head of a singly-linked list, LinkedList.sort(l) sorts the nodes in the list into ascending order according to the values in the .value field of each node. Your implementation should do an in-place update of the list. It is ok to use a simple...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class in C++ that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value x if...
Create a program that implements a singly linked list of Students. Each node must contain the...
Create a program that implements a singly linked list of Students. Each node must contain the following variables: Student_Name Student_ID In main(): Create the following list using addHead(). The list must be in the order shown below. Student_ID Student_Name 00235 Mohammad 00662 Ahmed 00999 Ali 00171 Fahad Print the complete list using toString() method. Create another list using AddTail(). The list must be in the order shown below. Student_ID Student_Name 00236 Salman 00663 Suliman 00998 Abdulrahman Print the complete list...
Create a program that implements a singly linked list of Students. Each node must contain the...
Create a program that implements a singly linked list of Students. Each node must contain the following variables: Student_Name Student_ID In main(): Create the following list using addHead(). The list must be in the order shown below. Student_ID Student_Name 00235 Mohammad 00662 Ahmed 00999 Ali 00171 Fahad Print the complete list using toString() method. Create another list using AddTail(). The list must be in the order shown below. Print the complete list using toString() method. Delete head note from both...
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT