fork download
  1.  
  2. //(c)Terminator
  3. #include <iostream>
  4. using namespace std;
  5.  
  6.  
  7. struct node {
  8. int val;
  9. node* next;
  10. };
  11.  
  12. static void fill(node** lst, const int* fa, const int* la);
  13. static void clear(node* lst);
  14. static void remove_before(node** lst, bool (*pfn)(int));
  15.  
  16.  
  17. bool func_un(int val){ return (val > 0); }
  18.  
  19.  
  20.  
  21. int main(void){
  22. node* lst = NULL;
  23.  
  24. int a[] = { -10, -20, 30, 40 };
  25. fill(&lst, a, a + sizeof(a)/sizeof(a[0]));
  26. remove_before(&lst, func_un);
  27.  
  28. for(const node* i = lst; i != NULL; i = i->next)
  29. cout << i->val << endl;
  30.  
  31. clear(lst);
  32. return 0;
  33. }
  34.  
  35.  
  36.  
  37.  
  38. //заполнение списка массивом
  39. static void fill(node** lst, const int* fa, const int* la){
  40. node* h = NULL, *t = NULL;
  41.  
  42. while(fa != la){
  43. node* p = new node();
  44. p->next = NULL;
  45. p->val = *fa++;
  46.  
  47. if(h == NULL)
  48. h = t = p;
  49. else {
  50. t->next = p;
  51. t = p;
  52. }
  53. }
  54. *lst = h;
  55. }
  56.  
  57.  
  58.  
  59. //чистка всего списка
  60. static void clear(node* lst){
  61. node* tmp;
  62. while(lst != NULL){
  63. tmp = lst;
  64. lst = lst->next;
  65. delete tmp;
  66. }
  67. }
  68.  
  69.  
  70.  
  71. //удаление элемента по компаратору
  72. static void remove_before(node** lst, bool (*pfn)(int)){
  73. node* p = *lst;
  74.  
  75. if(p != NULL){
  76. while((p->next != NULL) && !(*pfn)(p->next->val)){
  77. lst = &p->next;
  78. p = p->next;
  79. }
  80.  
  81. if(p->next != NULL){
  82. *lst = p->next;
  83. delete p;
  84. }
  85. }
  86. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
-10
30
40