linked list

i am getting an infinate loop for the while 67 and 71 lines i think it might be because the last node is no longer pointing to NULL. i have hand traced on paper and cant find anything wrong. someone please help.
<cheers>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
void MakeDistinctMirrorPairs(Node *& head)
{
   int dup = 0;
   if (head == 0 )   return;
   if (head == 0)
   {
      head = head->link;
      Node *nodePtr = new Node;
      nodePtr->data = head->data;
      head->link = nodePtr;
      nodePtr->link = 0;
      return;
   }
   Node *anc = head;
   Node *mirror = 0;
   {
      head = head->link;
      Node *nodePtr = new Node;
      nodePtr->data = head->data;
      head->link = nodePtr;
      nodePtr->link = 0;
      return;
   }
   Node *anc = head;
   Node *mirror = 0;
   while(anc != 0)
   {
      dup = 0;
      if(anc->link != 0)
      {  
         Node *pre = anc;
         Node *cur = anc->link;
         while(cur != 0)
         {
            if(cur->data == anc->data )
            {
               dup++;
               if(dup == 1)
               {
                  pre->link = cur->link;
                  cur->link = mirror;
                  mirror = cur;
                  cur = pre;
               }
               if(dup > 1)
               {
                  pre->link = cur->link;
                  delete cur;
                  cur = pre;
               }
            }
            pre = cur;
            cur = cur->link;
         }
      }
      if(dup == 0)
      {
         Node *newNode = new Node;
         newNode->data = anc->data;
         newNode->link = mirror;
         mirror = newNode;
      }
   anc = anc->link;
   }
   anc = head;

   while(anc->link != 0)
      anc = anc->link;
   anc->link = mirror;
   anc = head;
   while(anc != 0)
   {
      cout<<anc->data;
      anc = anc->link;
   }
   cout<<endl;
   return;
}

A linked-list processing function MakeDistinctMirrorPairs that is to process a linked list as follows.
Does nothing if given list is empty.
If given list is not empty, process it so that it comprises of 2 halves:
(1) The first half contains a sequence of distinct items (that the given list originally contains).
(2)The second half contains a sequence of distinct items that form a "mirror image" of the items in the first half.
The distinct data items are derived from the data items in the given list:
If given list (originally) has multiple nodes containing a certain distinct value, the second such node (i.e. the earliest duplicate) is to be used as the "mirror-image" node.
NOTE: Although the node to be used as the "mirror-image" node does not have to be the first duplicate, you are to make it so for the purpose of this exercise.
All remaining duplicates (if any, of a certain distinct value) are to be deleted from the list.
NOTE: No memory leak should be incurred, of course.
If given list (originally) has only a single node containing a certain distinct value, a new node for that value is to be created and used as the "mirror-image" node.
The sequence of distinct values in the first half must appear in the same order in which the first of the corresponding values originally (upon entering the function) appears in the given list.
List before List after
(empty) (empty)
5
5  5

5 2 4 5 4
5  2  4  4  2  5

1 2 3 3 1 3 2
1  2  3  3  2  1

1 3 1 1 0 4 3 2 0 3 2 2 2 2 3 4 1 1 2
1  3  0  4  2  2  4  0  3  1

3 0 2 2 2 0 1 5 1 0 0 4 4 1 0 5
3  0  2  1  5  4  4  5  1  2  0  3

5 5 5 5 2 3 1 3 3 2 3 0 2 4 1 4
5  2  3  1  0  4  4  0  1  3  2  5 

IMPORTANT requirements (you will earn little or no points if you don't do according to these):
Algorithm should:
NOT change the data of any existing nodes.
Destroy ONLY existing nodes that constitute second duplicate, third duplicate, etc. of a certain distinct value.
Recall that the first duplicate is not to be destroyed but instead used as a "mirror-image" node.
Create new nodes and copy data between nodes ONLY under the "given list (originally) has only a single node containing a certain distinct value" circumstances.
Creating pointer-to-node's (to provide temporary storage for node addresses) does not constitute creating new nodes or copying of items.
NOT make any temporary copies of any of the data items (using any kind of data structures including but not limited to arrays, linked lists and trees).
This means that the algorithm should largely involve the manipulation of pointers.
Function must be iterative (NOT recursive) - use only looping construct(s), without the function calling itself (directly or indirectly).
Function should NOT call any other functions to help in performing the task.
Topic archived. No new replies allowed.