Guest User

Untitled

a guest
Oct 19th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. initial: 0 5 2 2 7 3 7 9 0 2
  2. ought2b: 0 0 2 2 2 2 0 0 2 2
  3. outcome: 0 0 2 2 2 2 0 0 2 2
  4.  
  5. initial: 0
  6. ought2b: 0 0
  7.  
  8. initial: 1
  9. ought2b: (empty)
  10.  
  11. void RemOddDupEven(Node*& headPtr){
  12. // Define two cursors to iterate through referenced list.
  13. Node *precursor = 0,
  14. *cursor = headPtr;
  15.  
  16. // Error Checking | Check for reference to empty list.
  17. if(headPtr == 0){
  18. cerr << "RemOddDupEven() attempted on empty list" << endl;
  19. return;
  20. }
  21.  
  22. while(cursor->link != 0){
  23. if(cursor->data%2 != 0){
  24. /// Odd node found in referenced list. Remove it.
  25. if(precursor == 0){
  26. // No previous. Removing first node.
  27. precursor = cursor;
  28. cursor = cursor->link;
  29. headPtr = cursor;
  30. delete precursor;
  31. precursor = 0;
  32. } else {
  33. Node* tempNodePtr = cursor;
  34. cursor = cursor->link;
  35. precursor->link = cursor;
  36. delete tempNodePtr;
  37. }
  38. } else {
  39. /// Even node found in referenced list. Duplicate it.
  40. if(precursor == 0){
  41. // No previous. Duplicate and insert before current node.
  42. Node* newNodePtr = new Node;
  43. newNodePtr->data = cursor->data;
  44. newNodePtr->link = cursor;
  45. headPtr = newNodePtr;
  46. precursor = cursor;
  47. cursor = cursor->link;
  48. } else {
  49. // There is a previous. Duplicate and insert before current.
  50. Node* newNodePtr = new Node;
  51. newNodePtr->data = cursor->data;
  52. precursor->link = newNodePtr;
  53. newNodePtr->link = cursor;
  54. precursor = cursor;
  55. cursor = cursor->link;
  56. }
  57. }
  58.  
  59. }
  60. /// We are at last item in the list.
  61. if(cursor->data%2 != 0){
  62. /// Odd node found at end of referenced list. Remove it.
  63. precursor->link = 0;
  64. delete cursor;
  65. cursor = 0;
  66. } else {
  67. /// Even node found at the end of referenced list. Duplicte it.
  68. Node* newNodePtr = new Node;
  69. newNodePtr->data = cursor->data;
  70. precursor->link = newNodePtr;
  71. newNodePtr->link = cursor;
  72. }
  73. }
Add Comment
Please, Sign In to add comment