#include #include #define TRUE 1 #define FALSE 0 #define COUNT 10 struct linkedStruct { int elem; struct linkedStruct* next; }; typedef struct linkedStruct linked; typedef linked* linkedPtr; /* written with a pass-by-value parameter */ /* calling code: */ /* linked* head = NULL; */ /* ... */ /* listLength (head); */ int listLength (linked* hd) { int length = 0; while (hd != NULL) { length++; hd = hd->next; } return length; } int getFirstValue (linked* hd) { if (hd != NULL) return hd->elem; else return -999; /* returns -999 if list is empty */ } int isEmpty (linked* hd) { if (hd == NULL) return TRUE; else return FALSE; } void show (linked* hd) { while (hd != NULL) { printf ("%d, ", hd->elem); hd = hd->next; } printf ("\n"); } /* code to dynamically allocate and initallize a struct of */ /* the type linked */ /* the address of the newly created node is returned */ linked* newLinkedNode (int value, linked* n) { linked* tmp = (linked *) malloc ( sizeof(linked) ); tmp->elem = value; tmp->next = n; return tmp; } /* code to insert values in increasing order */ /* smaller values placed closer to the front of the list */ /* larger values placed closer to the end of the list */ /* function is written recursively */ linked* insertInOrderR (linked* h, int value) { if (h == NULL || value < h->elem) h = newLinkedNode ( value, h ); else h->next = insertInOrderR(h->next, value); return h; } /* code to insert values in increasing order */ /* smaller values placed closer to the front of the list */ /* larger values placed closer to the end of the list */ void insertInOrder (linked** hd, int val) { linked* curr = *hd; linked* prev = NULL; /* set curr to refer to the node in the list that is >= val */ /* prev will be set to the node just before curr */ while ((curr != NULL) && (curr->elem < val)) { prev = curr; curr = curr->next; } /* create the node to add into the list */ linked* ptr = (linked*) malloc (sizeof(linked)); ptr->elem = val; ptr->next = curr; /* if prev is null, insert at the front of the list */ if (prev == NULL) { *hd = ptr; } else { /* otherwise insert right after prev */ prev->next = ptr; } } /* code to insert values in increasing order */ /* smaller values placed closer to the front of the list */ /* larger values placed closer to the end of the list */ linked* insertInOrder2 (linked* hd, int val) { linked* curr = hd; linked* prev = NULL; /* set curr to refer to the node in the list that is >= val */ /* prev will be set to the node just before curr */ while ((curr != NULL) && (curr->elem < val)) { prev = curr; curr = curr->next; } /* create the node to add into the list */ linked* ptr = (linked*) malloc (sizeof(linked)); ptr->elem = val; ptr->next = curr; /* if prev is null, insert at the front of the list */ if (prev == NULL) { hd = ptr; } else { /* otherwise insert right after prev */ prev->next = ptr; } /* return the newly updated head of the list */ return hd; } int main (int argc, char**argv) { linked* head = NULL; linked* head2 = NULL; int i; int temp; /* adding values to the front of the list */ for (i = 0 ; i < COUNT; ++i) { temp = rand() % 100; printf ("In main(): Inserting value: %6d\n", temp); /* head being sent as C pass-by-value */ head = insertInOrder2 (head, temp); /* head2 being sent as C pass-by-value */ head2 = insertInOrderR (head2, temp + 100); printf ("The list has %d nodes\n", listLength(head)); show (head); printf ("List 2 has %d nodes\n", listLength(head2)); show (head2); printf ("\n"); } }