fork(2) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Elem
  5. {
  6. int num;
  7. Elem* next;
  8. };
  9.  
  10. void printLinkedList(Elem* list) {
  11. while (list) {
  12. cout << list->num << ' ';
  13. list = list->next;
  14. }
  15. cout << endl;
  16. }
  17.  
  18. void deleteFromLinkedList(Elem* &list) {
  19.  
  20. if (!list)
  21. return;
  22.  
  23. Elem *curr = list, *next = list->next, *prev = NULL;
  24.  
  25. while (next)
  26. {
  27. if (curr->num < next->num) {
  28. if (prev)
  29. prev->next = next;
  30. else
  31. list = next;
  32. delete curr;
  33. }
  34. else {
  35. prev = curr;
  36. }
  37. curr = next;
  38. next = curr->next;
  39. }
  40. }
  41.  
  42. int main()
  43. {
  44. Elem* first = NULL, *last = NULL, *p;
  45. int i;
  46.  
  47. //cout << "Enter any number or 0 to finish: ";
  48. cin >> i;
  49.  
  50. while (i != 0)
  51. {
  52. p = new Elem;
  53. p->num = i;
  54. p->next = NULL;
  55. if (first == NULL)
  56. {
  57. first = last = p;
  58. }
  59. else
  60. {
  61. last->next = p;
  62. last = last->next;
  63. }
  64. //cout << "Enter any number or 0 to finish: ";
  65. cin >> i;
  66. }
  67.  
  68. printLinkedList(first);
  69. deleteFromLinkedList(first);
  70. printLinkedList(first);
  71. }
Success #stdin #stdout 0.01s 5360KB
stdin
15 12 11 7 9 5 2 3 0
stdout
15 12 11 7 9 5 2 3 
15 12 11 9 5 3