Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- initial: 0 5 2 2 7 3 7 9 0 2
- ought2b: 0 0 2 2 2 2 0 0 2 2
- outcome: 0 0 2 2 2 2 0 0 2 2
- initial: 0
- ought2b: 0 0
- initial: 1
- ought2b: (empty)
- void RemOddDupEven(Node*& headPtr){
- // Define two cursors to iterate through referenced list.
- Node *precursor = 0,
- *cursor = headPtr;
- // Error Checking | Check for reference to empty list.
- if(headPtr == 0){
- cerr << "RemOddDupEven() attempted on empty list" << endl;
- return;
- }
- while(cursor->link != 0){
- if(cursor->data%2 != 0){
- /// Odd node found in referenced list. Remove it.
- if(precursor == 0){
- // No previous. Removing first node.
- precursor = cursor;
- cursor = cursor->link;
- headPtr = cursor;
- delete precursor;
- precursor = 0;
- } else {
- Node* tempNodePtr = cursor;
- cursor = cursor->link;
- precursor->link = cursor;
- delete tempNodePtr;
- }
- } else {
- /// Even node found in referenced list. Duplicate it.
- if(precursor == 0){
- // No previous. Duplicate and insert before current node.
- Node* newNodePtr = new Node;
- newNodePtr->data = cursor->data;
- newNodePtr->link = cursor;
- headPtr = newNodePtr;
- precursor = cursor;
- cursor = cursor->link;
- } else {
- // There is a previous. Duplicate and insert before current.
- Node* newNodePtr = new Node;
- newNodePtr->data = cursor->data;
- precursor->link = newNodePtr;
- newNodePtr->link = cursor;
- precursor = cursor;
- cursor = cursor->link;
- }
- }
- }
- /// We are at last item in the list.
- if(cursor->data%2 != 0){
- /// Odd node found at end of referenced list. Remove it.
- precursor->link = 0;
- delete cursor;
- cursor = 0;
- } else {
- /// Even node found at the end of referenced list. Duplicte it.
- Node* newNodePtr = new Node;
- newNodePtr->data = cursor->data;
- precursor->link = newNodePtr;
- newNodePtr->link = cursor;
- }
- }
Add Comment
Please, Sign In to add comment