training

Code I wrote during training
git clone git://git.bitsmanent.org/training
Log | Files | Refs | README

searchList.c (2149B)


      1 /* Exercise 12.21 */
      2 
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <time.h>
      6 
      7 struct list_t {
      8    int data;
      9    struct list_t *next;
     10 };
     11 typedef struct list_t List;
     12 
     13 void insert(List **listp, int value);
     14 void printl(List *listp);
     15 List *searchList(List *listp, int value);
     16 
     17 int main(void)
     18 {
     19    int number;
     20    List *mylist = NULL;
     21    List *myvalue = NULL;
     22 
     23    srand( time(NULL) );
     24 
     25    /* populate the list */
     26    for(number = 0; number < 10; number++) {
     27       insert(&mylist, 1 + rand() % 100);
     28    }
     29 
     30    printf("What value you are looking for? : ");
     31    scanf("%d", &number);
     32 
     33    myvalue = searchList(mylist, number);  
     34 
     35    printl(mylist);
     36 
     37    putchar('\n');
     38 
     39    if( myvalue != NULL ) {
     40       printf("Found %d to the list. Printing the remaining values..\n", number);
     41       printl(myvalue);
     42    }
     43    else {
     44       printf("Can't find %d to the list\n", number);
     45    }
     46 
     47    return 0;
     48 } /* E0F main */
     49 
     50 /* 
     51  * Insert a value to the list
     52 */
     53 void insert(List **listp, int value)
     54 {
     55    List *newptr;
     56    List *currentPtr;
     57    List *previousPtr = NULL;
     58 
     59    if( (newptr = malloc( sizeof(List) )) == NULL ) {
     60       printf("Cannot allocate the memory: %d not inserted\n", value);
     61       return;
     62    }
     63    newptr->data = value;
     64    newptr->next = NULL;
     65 
     66    currentPtr = *listp;
     67 
     68    while( currentPtr != NULL && value > currentPtr->data ) {
     69        previousPtr = currentPtr;
     70        currentPtr = currentPtr->next;
     71    }
     72 
     73    if( previousPtr == NULL ) {
     74       newptr->next = *listp;
     75       *listp = newptr;
     76    }
     77    else {
     78       previousPtr->next = newptr;
     79       newptr->next = currentPtr;
     80    }
     81 
     82 } /* eof insert() */
     83 
     84 /*
     85  * Print a the list values using '-->' as separator
     86 */
     87 void printl(List *listp)
     88 {
     89    List *currentPtr = listp;
     90 
     91    while( currentPtr != NULL ) {
     92       printf("%d --> ", currentPtr->data);
     93       currentPtr = currentPtr->next;
     94    }
     95    puts("NULL");
     96 
     97 } /* eof printl() */
     98 
     99 /*
    100  * Found a value to the list and returns a pointer to its position
    101 */
    102 List *searchList(List *listp, int value)
    103 {
    104    List *currentPtr = listp;
    105 
    106    while( currentPtr != NULL && currentPtr->data != value )
    107       currentPtr = currentPtr->next;
    108 
    109    return currentPtr;
    110 
    111 } /* eof searchList() */
    112