I need to write a function splitMid that will split a linked list into two sublists of equal size. I have written most of the functions that I need to achieve this and some other things, but I am having trouble getting started on this function. Can anyone point me in the right direction please?

Here is what I have so far...

template<class Type>
bool linkedListType<Type>::isEmptyList()
{
	return(first == NULL);
}

template<class Type>
linkedListType<Type>::linkedListType() // default constructor
{
	first = NULL;
	last = NULL;
	count = 0;
}

template<class Type>
void linkedListType<Type>::destroyList()
{
	nodeType<Type> *temp;   //pointer to deallocate the memory 
							//occupied by the node
	while(first != NULL)    //while there are nodes in the list
	{
	   temp = first;        //set temp to the current node
	   first = first->link; //advance first to the next node
	   delete temp;         //deallocate memory occupied by temp
	}

	last = NULL;	//initialize last to NULL; first has already
                   //been set to NULL by the while loop
 	count = 0;
}

	
template<class Type>
void linkedListType<Type>::initializeList()
{
	destroyList(); //if the list has any nodes, delete them
}

template<class Type>
int linkedListType<Type>::length()
{
 	return count;
}  // end length

template<class Type>
Type linkedListType<Type>::front()
{   
    assert(first != NULL);
   	return first->info; //return the info of the first node	
}//end front


template<class Type>
Type linkedListType<Type>::back()
{   
	 assert(last != NULL);
   	 return last->info; //return the info of the first node	
}//end back

template<class Type>
bool linkedListType<Type>::search(const Type& searchItem)
{
    nodeType<Type> *current; //pointer to traverse the list
    bool found;

    current = first; //set current to point to the 
                     //first node in the list
    found = false;   //set found to false

    while(current != NULL && !found)		//search the list
        if(current->info == searchItem)     //item is found
           found = true;
        else
           current = current->link; //make current point 
                                    //to the next node
     
     return found;
}//end search

template<class Type>
void linkedListType<Type>::insertFirst(const Type& newItem)
{
	nodeType<Type> *newNode; //pointer to create the new node

	newNode = new nodeType<Type>; //create the new node

	assert(newNode != NULL);	//If unable to allocate memory, 
 								//terminate the program

	newNode->info = newItem; 	   //store the new item in the node
	newNode->link = first;        //insert newNode before first
	first = newNode;              //make first point to the 
                                 //actual first node
	count++; 			   //increment count

	if(last == NULL)   //if the list was empty, newNode is also 
                      //the last node in the list
		last = newNode;
}

template<class Type>
void linkedListType<Type>::insertLast(const Type& newItem)
{
	nodeType<Type> *newNode; //pointer to create the new node

	newNode = new nodeType<Type>; //create the new node

	assert(newNode != NULL);	//If unable to allocate memory, 
 							//terminate the program

	newNode->info = newItem;      //store the new item in the node
	newNode->link = NULL;         //set the link field of newNode
         						//to NULL

	if(first == NULL)	//if the list is empty, newNode is 
     					//both the first and last node
	{
		first = newNode;
		last = newNode;
		count++;		//increment count
	}
	else			//the list is not empty, insert newNode after last
	{
		last->link = newNode; //insert newNode after last
		last = newNode;   //make last point to the actual last node
		count++;		//increment count
	}
}//end insertLast

	//Overloading the stream insertion operator
template<class Type>
ostream& operator<<(ostream& osObject, const linkedListType<Type>& list)
{
	nodeType<Type> *current; //pointer to traverse the list

	current = list.first;   //set current so that it points to 
					   //the first node
	while(current != NULL) //while more data to print
	{
	   osObject<<current->info<<" ";
	   current = current->link;
	}

	return osObject;
}

template<class Type>
linkedListType<Type>::~linkedListType() // destructor
{
	destroyList(); 
}//end destructor

template<class Type>
void linkedListType<Type>::splitMid(linkedListType<Type> &sublist)
{
	
}

There's more than one way of splitting it up. Here are two of them:

1->2->3->4->5->6->7->8

becomes

1->2->3->4 and
5->6->7->8

or

1->3->5->7 and
2->4->6->8

The first way requires you to know how many elements you have in the first list before splitting the list. Go through half of them, have them be the first list, then start a new list with the second half.

The second way has you create two lists, then alternate as you go down the list, depositing every other element in list 1 and every other element in list 2.

So do you end up with three lists or two lists (i.e. do you keep the original list as is and create two new lists or do you just create one new list and remove half of the elements from the original and stick them in this second list?) If you keep the original list as is and create two new lists, you'll need to create new nodes as well as the new lists. If not, you can keep the same nodes and just rearrange the pointers to the next node.

  1. Decide exactly what you start with (in the above case, one list with 8 nodes).
  2. Decide exactly what you want to end up with (do you keep the original list of 8 nodes and how are the lists split?).
  3. From that, decide how to do it. Don't skip to step three without going through steps one and two.
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.