Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. # include <stdio.h>
  2. # include <stdlib.h>
  3. # include <stdbool.h>
  4.  
  5. struct sll_node
  6. {
  7. int data ;
  8. struct sll_node * next ;
  9. };
  10.  
  11. bool insert_before ( struct sll_node ** node , int data )
  12. {
  13. struct sll_node * new_node = ( struct sll_node *)
  14. malloc ( sizeof ( struct sll_node ) ) ;
  15. if ( NULL != new_node )
  16. {
  17. new_node -> data = data ;
  18. new_node -> next = * node ;
  19. * node = new_node ;
  20. return true ;
  21. }
  22. return false ;
  23. }
  24.  
  25. bool insert_back ( struct sll_node ** node , int data )
  26. {
  27. * node = ( struct sll_node *) malloc ( sizeof ( struct sll_node ) ) ;
  28. if ( NULL != * node )
  29. {
  30. (* node ) ->data = data ;
  31. (* node ) ->next = NULL ;
  32. return true ;
  33. }
  34. return false ;
  35. }
  36. bool insert_node ( struct sll_node ** node , int data )
  37. {
  38. if ( NULL != * node )
  39. {
  40. if ((* node ) ->data <= data )
  41. return insert_before (node , data ) ;
  42. else
  43. return insert_node (&(* node ) ->next , data ) ;
  44. }
  45. else
  46. return insert_back (node , data ) ;
  47. }
  48.  
  49. void delete_node ( struct sll_node ** node , int data )
  50. {
  51. if ( NULL != * node )
  52. {
  53. if ((* node ) ->data == data )
  54. {
  55. struct sll_node * next = (* node ) ->next ;
  56. free (* node ) ;
  57. * node = next ;
  58. }
  59. else
  60. delete_node (&(* node ) ->next , data ) ;
  61. }
  62. }
  63.  
  64. void print_list ( struct sll_node * node )
  65. {
  66. if ( NULL != node )
  67. {
  68. printf ("%d ", node -> data ) ;
  69. print_list (node -> next ) ;
  70. }
  71.  
  72. else
  73. printf ("\n") ;
  74. }
  75.  
  76. void delete_last_number(struct sll_node **list_pointer,int number){
  77. if(*list_pointer){
  78. if(((*list_pointer)->data==number)&&(((*list_pointer)->next->data!=number)||((*list_pointer)->next==NULL))){
  79. struct sll_node *next=(*list_pointer)->next;
  80. free(*list_pointer);
  81. *list_pointer=next;
  82. }
  83. else{
  84. delete_last_number(&(*list_pointer)->next,number);
  85. }
  86. }
  87. }
  88.  
  89. void print_list_backwards (struct sll_node *node)
  90. {
  91. if ( NULL != node ) {
  92. print_list_backwards (node->next );
  93. printf ("%d ", node->data );
  94. }
  95. }
  96.  
  97. void remove_list ( struct sll_node ** node )
  98. {
  99. if ( NULL != *node ) {
  100. remove_list (&(*node)->next );
  101. free (*node );
  102. }
  103. }
  104.  
  105. void perform_operation ( struct sll_node *node ,void (* operation ) ( struct sll_node * node ) )
  106. {
  107. if (( NULL != node ) && ( NULL != operation ) ) {
  108. operation (node) ;
  109. perform_operation (node ->next , operation );
  110. }
  111. }
  112.  
  113. void print_node ( struct sll_node *node )
  114. {
  115. printf ("%d ", node -> data);
  116. }
  117.  
  118.  
  119. int evaluate_operation ( struct sll_node *node ,int (*operation ) (int result , struct sll_node *node ) )
  120. {
  121. if (( NULL != node ) && ( NULL != operation ) )
  122. return operation ( evaluate_operation (node ->next , operation ),node);
  123. return 0;
  124. }
  125.  
  126.  
  127. int main (void)
  128. {
  129. struct sll_node * front = NULL ;
  130. int i,suma=0/*,l_elementow=0,max=0,min=0*/;
  131. for (i=1; i <5; i++)
  132. if (! insert_node (& front , i) )
  133. fprintf (stderr , " Error while inserting %d!\n", i) ;
  134. for (i=6; i <10; i++)
  135. if (! insert_node (& front , i) )
  136. fprintf (stderr , " Error while inserting %d!\n", i) ;
  137. if (! insert_node (& front , 9) )
  138. fprintf (stderr , " Error while inserting %d!\n", 9) ;
  139. insert_node(&front, 9);
  140. insert_node(&front,10);
  141. insert_node(&front,10);
  142. insert_node(&front,11);
  143. insert_node(&front,11);
  144. insert_node(&front,11);
  145. puts("Poczatkowe elementy na liscie:");
  146. perform_operation (front , print_node ) ;
  147.  
  148.  
  149.  
  150. delete_last_number(&front,9);
  151. delete_last_number(&front,10);
  152. delete_last_number(&front,11);
  153. puts(" ");
  154.  
  155.  
  156.  
  157. puts("Elementy na liscie:");
  158. perform_operation (front , print_node );
  159.  
  160.  
  161.  
  162. remove_list (& front ) ;
  163. front = NULL ;
  164. return 0;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement