Advertisement
cwchen

[C] List Traversal with an Iterator

Apr 18th, 2018
602
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.63 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement