#include<stdio.h> #include<stdlib.h> #include<math.h> typedef struct _node { int data; struct _node *next; }node; void insertNodeSorted(node **head, node *newNode); void printList(node *head); void deleteList(node **head); void insertNodeSorted(node **head, node *newNode) { if(*head == NULL) { *head = newNode; } else if( (*head)->data > newNode->data ) { newNode->next = *head; *head = newNode; } else { node *temp = *head; while( (temp->next != NULL) && (temp->next->data < newNode->data)) { temp = temp->next; } newNode -> next = temp -> next; temp -> next = newNode; } } void printList(node *head) { int i = 0; node *temp = head; while(temp != NULL) { printf("data in node %d is: %d \n", i, temp->data); temp = temp->next; i++; } } void deleteList(node **head) { node *temp = *head; node *delNode; while(temp!= NULL) { delNode = temp; temp = temp -> next; delNode -> next = NULL; free(delNode); } free(temp); } int main(int argc, char *argv[]) { int seed; int num_of_rand_nums; int max_value; int i; node *head = NULL; sscanf(argv[1],"%d",&seed); srandom(seed); sscanf(argv[2],"%d",&num_of_rand_nums); sscanf(argv[3],"%d",&max_value); for(i=0;i<num_of_rand_nums;i++) { node *newNode = malloc(sizeof(node)); if(newNode == NULL) { printf("newNode is NULL! \n"); exit(1); } newNode -> next = NULL; newNode -> data = random()%(max_value + 1); insertNodeSorted(&head,newNode); } printList(head); deleteList(&head); return 0; }