Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. struct list_node
  6. {
  7. const char * name;
  8. list_node* next;
  9. };
  10.  
  11. void print( list_node* head )
  12. {
  13. while( head!= NULL ){
  14. printf( "%s ", head->name );
  15. head= head->next;
  16. }
  17. printf( "\n" );
  18. }
  19.  
  20. list_node* last( list_node * head )
  21. {
  22. while( head->next != NULL )
  23. head= head->next;
  24. return head;
  25. }
  26.  
  27. list_node * find_in_queue( list_node * head, const char* names[], int n )
  28. {
  29. list_node* result = NULL;
  30.  
  31. while( head != NULL ) {
  32. for( int i=0; i < n; ++i ) {
  33. const char* name = names[i];
  34. if( name == NULL )continue;
  35.  
  36. if( strcmp( head->name, name ) == 0 ) {
  37. if( result == NULL ) result = head;
  38.  
  39. names[i] = NULL;
  40. }
  41. }
  42. head= head->next;
  43. }
  44. return result;
  45. }
  46.  
  47. list_node* create_node( const char* name )
  48. {
  49. list_node* new_node = (list_node*)malloc( sizeof list_node );
  50. new_node->name = name;
  51. new_node->next = NULL;
  52. return new_node;
  53. }
  54.  
  55. list_node* add_after( list_node* after_this, const char* name )
  56. {
  57. list_node* new_node =create_node( name );
  58. new_node->next = after_this->next;
  59. after_this->next= new_node;
  60. return new_node;
  61. }
  62.  
  63. void add_guests_after( list_node* after_this, const char* names[], int n )
  64. {
  65. for( int i=0; i < n; ++i ) {
  66. const char* name = names[i];
  67. if( name == NULL )continue;
  68.  
  69. after_this= add_after( after_this, name );
  70. }
  71. }
  72.  
  73. void add_guests( list_node* queue_head, const char* names[], int n )
  74. {
  75. list_node* found = NULL;
  76.  
  77. found = find_in_queue( queue_head, names, n );
  78. if( found == NULL )
  79. found = last( queue_head );
  80.  
  81. add_guests_after( found, names, n );
  82. }
  83.  
  84. list_node* create_ABCD()
  85. {
  86. list_node* queue_head = create_node( "A" );
  87. list_node* iterator = queue_head;
  88.  
  89. iterator = add_after( iterator, "B" );
  90. iterator = add_after( iterator, "C" );
  91. iterator = add_after( iterator, "D" );
  92. return queue_head;
  93. }
  94.  
  95. void test_simple_create()
  96. {
  97. list_node* queue_head = create_ABCD();
  98.  
  99. print( queue_head );
  100. }
  101.  
  102. void test_add_end()
  103. {
  104. list_node* queue_head = create_ABCD();
  105.  
  106. const char* guests[3] ={ "X", "Y", "Z" };
  107. add_guests( queue_head, guests, 3 );
  108. print( queue_head );
  109. }
  110.  
  111. void test_add_one_middle()
  112. {
  113. list_node* queue_head = create_ABCD();
  114.  
  115. const char* guests[3] ={ "X", "C", "Z" };
  116. add_guests( queue_head, guests, 3 );
  117. print( queue_head );
  118. }
  119.  
  120. void test_add_repeated()
  121. {
  122. list_node* queue_head = create_ABCD();
  123.  
  124. const char* guests[3] ={ "D", "C", "A" };
  125. add_guests( queue_head, guests, 3 );
  126. print( queue_head );
  127. }
  128.  
  129. int main() {
  130. test_simple_create();
  131. puts( "-----" );
  132. test_add_end();
  133. puts( "-----" );
  134. test_add_one_middle();
  135. puts( "-----" );
  136. // broken
  137. test_add_repeated();
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement