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
//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;
}
}
elseif (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)
returnfalse;
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)
{
returntrue;
cancell = true;
break;
}
else
ptr2 = ptr2->next;
}
}
returnfalse;
}
// 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;
}