Segmentation issue with Double linked list Split

Hello,

I am completely new at this, and need your help fixing my methods.

my mergeList is addeding extra numbers after the 88.

my SplitList is causing a seg fault.

my printRepeatedItems is also printing wrong

incorrect output as of now.



First Ordered List: -3  1  2  4  5  9  
Second Ordered List: 1  1  4  5  6  9  88  
Merged Ordered List: -3  1  1  1  2  4  4  5  5  6  9  9  88  9  6  5  4  1  1  
Are Items in the Merged List Distinct? N
Item 1 is repeated 1 times
Item 1 is repeated 1 times
Item 2 is repeated 1 times
Item 4 is repeated 1 times
Item 4 is repeated 1 times
Item 5 is repeated 1 times
Item 5 is repeated 1 times
Item 6 is repeated 1 times
Item 9 is repeated 1 times
Item 9 is repeated 1 times
Item 88 is repeated 1 times
Item 9 is repeated 1 times
Item 6 is repeated 1 times
Item 5 is repeated 1 times
Item 4 is repeated 1 times
Item 1 is repeated 1 times
Item 1 is repeated 1 times
Two lists formed by splitting merged list in the middle are: 
Segmentation fault



example input

9 1 -3 2 4 5
99999
6 5 9 88 1 4 1
99999


Last edited on
my code

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306

//deletes the given number from the list
void OrderedDoublyLinkedList::deleteNode(int n){
    nodeType *current;
       // if list in NULL 
    if (first == NULL )
        return;
 
    current = first;
    int i;
 
    /* traverse up to the node at position 'n' from
       the beginning */
    for (i = 1; current != NULL && i < n; i++)
        current = current->next;
 
    /* if 'n' is greater than the number of nodes
       in the doubly linked list */
    if (current == NULL)
        return;
 
    /* delete the node pointed to by 'current' */
    deleteNodeOperation(first, current);
}
//deletes the node sent over by deletenode above. 
void OrderedDoublyLinkedList::deleteNodeOperation(nodeType *head, nodeType *delet)
{
    /* base case */
    if (head == NULL || delet == NULL)
        return;
 
    /* If node to be deleted is head node */
    if (head == delet)
        head = delet->next;
 
    /* Change next only if node to be deleted is NOT 
       the last node */
    if (delet->next != NULL)
        delet->next->back = delet->back;
 
    /* Change prev only if node to be deleted is NOT 
       the first node */
    if (delet->back != NULL)
        delet->back->next = delet->next;
 
    /* Finally, free the memory occupied by del*/
    delete(delet);
}
//combines two list into one.
void OrderedDoublyLinkedList::mergeLists(OrderedDoublyLinkedList List1, OrderedDoublyLinkedList List2){
    nodeType *nxt_node, *pre_node, *pptr, *qptr;
    int dat;
 
    pptr = List1.first;
    qptr = List2.first;
    first = nxt_node = pre_node = NULL;
  
    while (pptr != NULL && qptr != NULL)
    {
        if (pptr->info < qptr->info)
        {
            dat = pptr->info;
            pptr = pptr->next;      
        }
        else
        {
            dat = qptr->info;
            qptr = qptr->next;
                
        }
        nxt_node = new nodeType;
        nxt_node->info = dat;
        nxt_node->next = NULL;
        
        if (first == NULL)
                first = nxt_node;
        else
            pre_node->next = nxt_node;

        pre_node = nxt_node;  
    }
        if (pptr == NULL)
        {
            while (qptr != NULL)
                {
                nxt_node = new nodeType;
                nxt_node->info = qptr->info;
                nxt_node->next = NULL;
                
                if (first == NULL)
                    first = nxt_node;
                   else
                    pre_node->next = nxt_node;
    
                pre_node = nxt_node;
                qptr = qptr->back;
                
            }
 
        }
  
 
        else if (qptr == NULL)
        {
            while (pptr != NULL)
                {
                nxt_node = new nodeType;
                nxt_node->info = pptr->info;
                nxt_node->next = NULL;
          
 
                if (first == NULL)
                    first = nxt_node;
                    else
                    pre_node->next = nxt_node;
                    
                pre_node = nxt_node;
                pptr = pptr->next;
               }
                
        }

}

// checks if all numbers in the list are unique/
bool OrderedDoublyLinkedList::distinctItems(){

        bool cancell = false;
    // if DLL is empty or if it contains only
    // a single node
    if (first == NULL || first->next == NULL)
        return false;
 
    nodeType *ptr1, *ptr2;
 
    // pick elements one by one
    for (ptr1 = first; ptr1 != NULL; ptr1 = ptr1->next)
    {
        
        if (cancell)
                break;
        else
        ptr2 = ptr1->next;
 
        // Compare the picked element with the
        // rest of the elements
        while (ptr2 != NULL)
         {
            if (ptr1->info == ptr2->info) 
                {
                        return true;
                        cancell = true;
                        break;
                }
            else
                ptr2 = ptr2->next;
        }
    }

        return false;

}


// outputs the duplicat number and the count of each/
void OrderedDoublyLinkedList::printRepeatedItems(){
        int repeat = 0;
        bool cancell = false;
    // if DLL is empty or if it contains only
    // a single node
    if (first == NULL || first->next == NULL)
        return;
 
    nodeType *ptr1, *ptr2;
 
    // pick elements one by one
    for (ptr1 = first; ptr1 != NULL; ptr1 = ptr1->next)
    {
            if (cancell)
             { 
                 cout<<"Item "<< ptr1->info <<" is repeated "<<repeat<<" times"<<endl;
                 repeat = 0;
              }
        else
                ptr2 = ptr1->next;
 
        // Compare the picked element with the
        // rest of the elements
        while (ptr2 != NULL && repeat <= count )
         {

            if (ptr1->info == ptr2->info) 
                {
                        ++repeat;
                        cancell = true;
                 }
            else 
                 {
                  ptr2 = ptr2->next;
                         
                 }

            }
              


    }



}

void OrderedDoublyLinkedList::splitList (OrderedDoublyLinkedList &List1, OrderedDoublyLinkedList &List2){

    int num = 0;
    nodeType  *iterator = first;
 
    while (iterator->next != last) {
        num++;
        iterator = iterator->next;
    }

   int listSize = num;
   int counter = 0;

if (listSize == 1)
        return;
 
    nodeType *tempNode = new nodeType;
 
    /**
     * A temporary node for swapping a node and its reflection node
     */
    nodeType *dummyNode = new nodeType;
 
    nodeType *headCursor = first;
    nodeType *tailCursor = last;
    for (int i = 0; i < listSize / 2; i++) {         
                headCursor = headCursor->next;
        tailCursor = tailCursor->back;
 
        nodeType *curNode = headCursor;
        nodeType *reflectionNode = tailCursor;
 
        if (listSize % 2 == 0 && listSize / 2 - 1 == i) {
            /**
             * insert a dummy node for reflection
             * for even sized lists at median position
             */
            curNode->next = dummyNode;
            dummyNode->back = curNode;
 
            reflectionNode->back = dummyNode;
            dummyNode->next = reflectionNode;
        }
        /**
         * swap the connections from previous and next nodes for current and reflection nodes
         */
        
        curNode->back->next = curNode->next->back = reflectionNode;
 
        reflectionNode->back->next = reflectionNode->next->back = curNode;
        
 
        /**
         * swapping of the nodes
         */
 
        tempNode->back = curNode->back;
        tempNode->next = curNode->next;
 
        curNode->next = reflectionNode->next;
        curNode->back = reflectionNode->back;
 
        reflectionNode->back = tempNode->back;
        reflectionNode->next = tempNode->next;
 
        if (listSize % 2 == 0 && listSize / 2 - 1 == i) {
            /**
             * remove the dummy node for reflection
             * for even sized lists from the median
             */
            reflectionNode->next = curNode;
            curNode->back = reflectionNode;
         }
 
        /**
         * Reassign the cursors to position over the recently swapped nodes
         */
        tailCursor = curNode;
        headCursor  = reflectionNode;
    }

    while (headCursor->next != last) {
        List1.insertNode(headCursor->info);
        headCursor = headCursor->next;
    }

    while (tailCursor ->next != last) {
        List2.insertNode(tailCursor->info);
        tailCursor = tailCursor->next;
    }
 
    delete tempNode, dummyNode;
}
You need to post all of the code, including main, so we can run it.
Topic archived. No new replies allowed.