#include<iostream>

#include<fstream>

#include"d_nodel.h"

using namespace std;

void merge(dnode<double> *, dnode<double> *); //free function to merge the two lists


int main()

{

	dnode<double> *listA = new dnode<double>;

	dnode<double> *listB = new dnode<double>;
	
	double sz1, sz2;

	int val1; //first set of integers

	int val2; //second set of integers

	ifstream inFile; //declares the inFile
	
	ofstream outFile; //declares the outFile


	//Read in a file to build the two lists

	inFile.open("lnkList.in"); //Open the input file

	//statement for file not found
    if (!inFile){
		
		cout << "Unable to open file"; //output statement for file not found

		exit(1); //terminate with error
    }
	
	inFile >> val1 >> val2;//reads in the file with a pair of integers


//Pointers to build the orginal two lists
	
	dnode <double> *p = listA -> next; //points to the node following the header of listA

//Nested for loop to read in up to val1 # of doubles

	for (int i = 0; i < val1; i++)
	{
		inFile >> sz1;

		dnode<double> *p = listA->next;

		 while(p != listA && p ->nodeValue < sz1)//checking the value in the list
		 {
			 p = p ->next;//move the pointer forward.
		 }
		 {
			*insert(p, sz1);//else insert the new value now
		 }
	 }
	 dnode <double> *p2 = listB -> next; //points to the node following the header of listB

//Nested for loop to read in up to val2 # of doubles

	for (int i = 0; i < val2; i++)
	{
		inFile >> sz2;
		
		dnode<double> *p2 = listB->next;
		
		while(p2 != listB && p2 ->nodeValue < sz2)//checking the value in the list
		 {
			 p2 = p2 ->next;//move the pointer forward.
		}
		{
			*insert(p2, sz2);//else insert the new value now
		 }
	}

  outFile.open ("lnkList.out");  
{
	p = p->next;
	outFile << "List 1 before merge: \n";
	for(int i = 0; i < val1; i++) 
   {
      // output dnode value and move to the next dnode
      outFile << p->nodeValue << " ";
      p = p->next;
   }

	p2 = p2->next;
	outFile << endl << "List 2 before merge: \n";

   
	 for(int j = 0; j < val2; j++)
	   {
		outFile << p2->nodeValue << " ";
			p2 = p2->next;
	 }

//This is to merge A and B
merge(p, p2);

{
	p = p->next;

	outFile << endl << "List 1 after merge: \n";

	for(int i = 0; i < val1; i++) 
   {
      // output dnode value and move to the next dnode
      outFile << p->nodeValue << " ";

      p = p->next;
   }

	p2 = p2->next;
	outFile << endl << "List 2 after merge: \n";
   
	 for(int j = 0; j < val2; j++)
	   {
		outFile << p2->nodeValue << " ";
			p2 = p2->next;
	 }

	return 0;
}
}
}
void merge(dnode<double> *A, dnode<double> *B)//merges the two lists

{
	dnode <double> *p = A; //pointer that points to the beginning of A

	dnode <double> *p2 = B;//pointer that points to the beginning of B

	while(A != p || B != p2)

	A->next = A;

	B->next = B;
	{
		//if statement if B is less than A
		if (B < A)
		{
			B ->next = A; //links B to listA

			B ->prev = A ->prev;

			//This is to update the surrounding pointers
			A ->prev ->next = B;

			A ->prev = B;

			B -> next = B;
		}

		else //Else statement for opposite
		{
			B ->prev = A; //links B to list A

			B ->next = A ->next;

			//This is to update the surrounding pointers
			A->next ->prev = B; 

			A->next = B;

			B ->next = B;

		}
	}
}

I am trying to merge the two lists but here is the output I am getting in my outfile. So what is suppose to happen is the ones that are in order in the "before" merges is suppose to merge together for the "after" merge and also, the List 2 "after" merge is suppose to be left empty. In other words (AFTER MERGE) list one is suppose to have all integers and list two is suppose to be empty. But it keep displaying NODES!

Output:
List 1 before merge:
6.63 8.35 9.3 10.26 10.29 12.32 12.4 12.66 12.93 55.2 62.3 92.97
List 2 before merge:
2.14 4.24 8.31 10.26 10.29 12.14 12.14 12.9 106.7
List 1 after merge:
-6.27744e+066 -6.27744e+066 -6.27744e+066 -6.27744e+066 -6.27744e+066 -6.27744e+066 -6.27744e+066 -6.27744e+066 -6.27744e+066 -6.27744e+066 -6.27744e+066 -6.27744e+066
List 2 after merge:
-6.27744e+066 -6.27744e+066 -6.27744e+066 -6.27744e+066 -6.27744e+066 -6.27744e+066 -6.27744e+066 -6.27744e+066 -6.27744e+066

First of all, the while statement would never evaluate to true, therefore A->next = A; would never execute. The next statement B->next = B; , would remove all references to any subsequent nodes in the list. The statement if (A < B) is most likely not a good method to use. Simply because memory is allocated first, does not guarantee that the memory address will be lower than any memory allocated later.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.