daily pastebin goal
20%
SHARE
TWEET

[C] List Traversal with an Iterator

cwchen Apr 18th, 2018 168 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. bool test_list_iter()
  2. {
  3.     bool failed = false;
  4.    
  5.     // List: 4 -> 9 -> 5 -> NULL;
  6.     List *lt = list_init(3, 4, 9, 5);
  7.     if (lt == NULL) {
  8.         perror("Failed to allocate List lt");
  9.         return false;
  10.     }
  11.    
  12.     int arr[] = {4, 9, 5};
  13.     size_t i = 0;
  14.     // Iterate through lt.
  15.     for (Node *it = list_start(lt); !list_end(it); it = list_next(it)) {
  16.         if (node_value(it) != arr[i]) {
  17.             failed = true;
  18.             goto LIST_FREE;
  19.         }
  20.        
  21.         i++;
  22.     }
  23.  
  24. LIST_FREE:
  25.     list_free(lt);
  26.    
  27.     if (failed) {
  28.         return false;
  29.     }
  30.    
  31.     return true;
  32. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top