the following code will print out all the names of patients I have in a linked list.

I have given the user an option to choose which patient they would like to move in the linked list.

i thought if I called S.insert(passed the new postition, and P (where P is the object of the patient class) it would work.

then all I had to do was remove the previous location of that position.

when I run the code it only removes the patient but dose not add it to the location I want.

is the code right by me below. I have just added snippets as the whole project would be too confusing.

thx

tim

for(int a=1;a<(S.size()+1);a++)
{
  cout<<a<<". "; S.display(a);
 }   
  cout<<"\nWhich Patient would you like to treat"<<endl;
  cin>>num;  
  cout<<"\nYou have chosen to treat:\n"<<endl;
  S.displayall(num);
  cout<<"\nWhat position would you like to place this patient in the Priority Q?"<<endl;
  cin>>position;
  S.insert(position,P);
  S.remove(num);
}
#include <iomanip>


using namespace std;   
typedef patient SeqItemType;  
 
class ListNode
    {
	    public:
	    SeqItemType item;
	   ListNode *next;

	   };



    class Sequence
	  { 
	
    	public:
   	    Sequence();                  


   	   bool isEmpty();
   	   int size();
   	   void insert(int index, SeqItemType newItem);
   	   void remove(int index);
   	   SeqItemType retrieve(int index);
   	   void display(int index);
   	   void displayall(int index);
      
  	  private:
	  ListNode *find(int index);
      int length;       	
      ListNode *head;
  
      };
	
    Sequence::Sequence()
   {
	length = 0;
	head = NULL;
   }

   bool Sequence::isEmpty() 
   {
	return length==0;	
   } 

   int Sequence::size()
   {
   	return length;
   } 


   void Sequence::display(int index)
   {
   ListNode *cur = find(index);
	  		
            
           cout<<"Name :"<< cur->item.get_Name() << endl;
           //cout<<"Date of Birth :"<<cur->item.get_dob() << endl;
           //cout<<"Illness :"<<cur->item.get_illness() << endl;
           //cout<<"Priority :"<<cur->item.get_priority() << endl;
           //cout<<"Ward :"<<cur->item.get_ward_no() << endl;
           //cout<<"\n\n";
           
		
   }



   SeqItemType Sequence::retrieve(int index)
  {
   	if ((index < 1) || (index > size())){
   		// can't delete something that isn't there
   	}
   	ListNode *cur = find(index);
   	return cur->item;	
  } 

   void Sequence::insert(int index, SeqItemType newItem)
  {
   	
	if ((index < 1) || (index > (size()+1))){
   		// if index is out of bounds don't do anything
   	}
   	else
   	{  
   		// otherwise create new node and place newItem in it
      	ListNode *newPtr = new ListNode();
      	
      	// increment the size attribute
      	length++;
        // store the data item in the new node
      	newPtr->item = newItem;

		//  if the index is 1, insert new node at beginning of list
        if (index == 1)
        { 
       		newPtr->next = head;
        	head = newPtr;
        }
        else
        {  
        	ListNode *prev = find(index-1);
        	newPtr->next = prev->next;
			prev->next = newPtr;
            
        }
	 } 
   }


void Sequence::remove(int index) 
{   
    
	ListNode *cur;

   	if ((index < 1) || (index > size())){
   	
   		}
   	else
   	{  
		// remove the first node in the list
		if (index == 1)
      	{ 
      		cur = head;
     		head = head->next;   	
     		
      	}
		else{
        	ListNode *prev = find(index-1);	
        		
			cur = prev->next;
			prev->next = cur->next;
		}	
		
		// clean things up!
		length--;
		cur->next = NULL;
		delete cur;
	}
  
}


ListNode *Sequence::find(int index){

	if ((index <1) || (index >size())){
		return 0;
	}
	else{
		ListNode *cur = head;
		for(int skip =1; skip<index; ++skip)
			cur=cur->next;
		return cur;
  	}
}


void Sequence::displayall(int index)
   {
   	ListNode *cur = find(index);		
            
           cout<<index<<". Name:          "<<cur->item.get_Name() << endl;
           cout<<"   Date of Birth: "<<cur->item.get_dob() << endl;
           cout<<"   Illness:       "<<cur->item.get_illness() << endl;
           cout<<"   Priority:      "<<cur->item.get_priority() << endl;
           cout<<"   Ward:          "<<cur->item.get_ward_no() << endl;
           cout<<"\n\n";
           
   }
   
   
   /*void Sequence::displayall()
   {
   	for (ListNode *cur = head; cur!= NULL; cur = cur->next)
	   {		
            
           cout<<"Name :"<< cur->item.get_Name() << endl;
           //cout<<"Date of Birth :"<<cur->item.get_dob() << endl;
           //cout<<"Illness :"<<cur->item.get_illness() << endl;
           //cout<<"Priority :"<<cur->item.get_priority() << endl;
           //cout<<"Ward :"<<cur->item.get_ward_no() << endl;
           //cout<<"\n\n";
           
		}
   }*/

solution thanks to perniciosus

void Sequence::move(int from, int to)
{
  SeqItemType item = find( from )->item ;
  remove(from) ;
  insert( to, item ) ;
}
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.