while(temp != NULL)
                  {
                      if(temp->info.Name == cName)
                      {
                          temp->info.DiscountNumber = temp->info.DiscountNumber + 5;
                          flag = true;
                      }
                                 
                         if(temp ->info.DiscountNumber > 25)
                         {
                           temp->info.DiscountNumber = 25;
                          // prev->link = temp->link;
                          // delete temp;
                           //temp->link = first;
                          // first = temp;
                         }
                                        
                         //cout<<"Discount is :"
                         //<<temp->info.DiscountNumber
                         //<<endl;
                         prev = temp;
                         temp = temp->link;
                  }

I am writing a program that will read data on customers from a file and grab names from a second file and add a 5% increase on the customer if their name is in the 2nd file. The current problem I am having is that I am trying to take the customers who have a 25% discount and move them all to the front of the list,any help or suggestions would be greatly appreciated

Recommended Answers

All 4 Replies

Maybe the easiest way to do that is to make a completly new linked list. Transverse the original linked list and select the nodes that have 25% discount then add them to the new linked list. After that add all the other nodes.

Unfortunately its in the constraints that I may only use one linked list,I did the program before doing the way you said using 2 and achieved my desired results but then my professor said we may only use 1 linked list.

if you know how to insert a new node to your list and how to delete a node then you can do it.

declare a temporary pointer and initialize it using the node which has 25 discount . then delete the original node and insert the temporary node in it's appropriate place ,keep doing so until you have your list in the order you wish

Mostly Figured out my problem

for(int i = 0;i < 21; i++)
      {
        cout << setw(25) <<left<< (temp->info.Name);
        cout << setw(25) <<left<< (temp->info.DiscountNumber);
        cout <<setw(28)<<left << (temp->info.i);
        cout<<endl;
        if(temp->info.DiscountNumber == 25)
        {
          first = movetofront(first,temp->info.i); <<<------- This is the way i calledit
         // cout<<"THIS IS WHO WE ARE MOVING:"<<temp->info.i<<endl;
        }
        
        temp = temp->link;
      }
             



nodeType* movetofront(nodeType *first,int target)
{
      nodeType *current;
      current = first;
      nodeType *prev;
      int count  = 1;
      
      if(count == target)
      return current;
      
   //  cout <<"list contains\n\t";
     while (current != NULL)
      {
  
      count++;
      
      if(count == target)
      {
        prev = current;         //set up the pointers
        current = current ->link;//set up pointers
           
        prev->link = current->link;//point around the target node


        current ->link = first;  // point the link in the target
                          //node at the first node in the list
 
         first = current;
       
       }
      current = current->link;
      }
     return first;
   }

it works perfectly if i call the function like: movetofront(first,5) and put the positio i want it to move to the front but it seems to grab a random link in the list if I call it the way I did it works and takes all values = to 25 to the top of the list but like I said it grabs a random one as well and throws it up to the top

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.