I want someone to help me write a program to read the integer elements of two arrays A and B of different size from the user. And use link list to save this elements instead of arrays. And then do the following to the link lists given:

1. Sort link list A in ascending order using a special sorting function by passing a pointer to the function.
2. Sort link list B in descending order using a special sorting function by passing a pointer to the function.
3. Add an element with a value of 001 at the end of the first link list A
4. Add an element with a value of 002 at the beginning of second link
5. Join both link lists as A and B in one link list
6. add one element in the middle of the new link list with a value of 003
7. add all the elements in the link list
8. print the values of all elements and their sum on the screen

Show us what you've come up with on your own.

We help you, we don't code for you @_@

commented: @_@ +1
Member Avatar for jencas

Try to solve your problem with list and algorithms of the STL

#include <iostream.h>
#include<stdlib.h>

struct node
  { 
     int number;          // data
     node *nxt;// Pointer to next node
  };

node *start_ptr = NULL;
node *start_ptr2=NULL;
node *start_ptr3=NULL;
node *current;		 // Used to move along the list
node *current2;
int option = 0,i;

void add_node_at_end(int what,int val)
  {  node *temp, *temp2;   // Temporary pointers

     // Reserve space for new node and fill it with data
     temp = new node;
	 if(what!=3){ 
		
	 cout << "Please enter the value into list "<<what<<" :";
     cin >> temp->number;
	 }
	 else if(what==3){temp->number=val;}
     temp->nxt = NULL;

	 if(what==1){//list 1
     // Set up link to this node
     if (start_ptr == NULL)
       { start_ptr = temp;
	 current = start_ptr;
       }
     else
       { temp2 = start_ptr;
         // We know this is not NULL - list not empty!
         while (temp2->nxt != NULL)
           {  temp2 = temp2->nxt;
              // Move to next link in chain
           }
         temp2->nxt = temp;
       }
	 }
	 else if(what==2){//list 2
		 // Set up link to this node
     if (start_ptr2 == NULL)
       { start_ptr2 = temp;
	 current2 = start_ptr2;
       }
     else
       { temp2 = start_ptr2;
         // We know this is not NULL - list not empty!
         while (temp2->nxt != NULL)
           {  temp2 = temp2->nxt;
              // Move to next link in chain
           }
         temp2->nxt = temp;
       }
	 }

	 else if(what==3){//list 3
		 // Set up link to this node
     if (start_ptr3 == NULL)
       { start_ptr3 = temp;
	 current2 = start_ptr3;
       }
     else
       { temp2 = start_ptr3;
         // We know this is not NULL - list not empty!
         while (temp2->nxt != NULL)
           {  temp2 = temp2->nxt;
              // Move to next link in chain
           }
         temp2->nxt = temp;
       }
	 }
  }

void display_list(int what)
  {  node *temp;
system("cls");
if(what==1){  temp = start_ptr;}
else if(what ==2){temp=start_ptr2;}
else if(what==3){temp=start_ptr3;}
     cout << endl;
     if (temp == NULL)
       cout << "The list is empty!" << endl;
     else
       { while (temp != NULL)
	   {  // Display details for what temp points to
             
              cout << "Num : " << temp->number << " ";
	      
	      if (temp == current)
		
            
	      temp = temp->nxt;

	   }
	 cout << "End of list!" << endl;
       }
  }


void selection_sort()
{
 node *p, *q, *r, *s, *temp ;
p = r = start_ptr ;
 
while ( p -> nxt != NULL )
{
s = q = p -> nxt ;
while ( q != NULL )
{
if ( p -> number > q -> number )
{
if ( p -> nxt == q ) /* Adjacent Nodes */
{
if ( p == start_ptr )
{
p -> nxt = q -> nxt ;
q -> nxt = p ;
temp = p ;
p = q ;
q = temp ;
 
start_ptr = p ;
r = p ;
s = q ;
q = q -> nxt ;
}
else
{
p -> nxt = q -> nxt ;
q -> nxt = p ;
r -> nxt = q ;
temp = p ;
p = q ;
q = temp ;
s = q ;
q = q -> nxt ;
}
}
else
{
if ( p == start_ptr )
{
temp = q ->nxt ;
q -> nxt = p -> nxt ;
p -> nxt = temp ;
s ->nxt = p ;
temp = p ;
p = q ;
q = temp ;
s = q ;
q = q -> nxt ;
start_ptr = p ;
}
else
{
temp = q -> nxt ;
q -> nxt = p -> nxt ;
p -> nxt = temp ;
r -> nxt = q ;
s -> nxt = p ;
temp = p ;
p = q ;
q = temp ;
s = q ;
q = q -> nxt ;
}
}
}
else
{
s = q ;
q = q -> nxt ;
}

}
r = p ;
p = p -> nxt ;


}
 

}

void delete_start_node()
   { node *temp;
     temp = start_ptr;
     start_ptr = start_ptr->nxt;
     delete temp;
   }

void delete_end_node()
   { node *temp1, *temp2;
     if (start_ptr == NULL)
          cout << "The list is empty!" << endl;
     else
        { temp1 = start_ptr;
          if (temp1->nxt == NULL)
             { delete temp1;
               start_ptr = NULL;
             }
          else
             { while (temp1->nxt != NULL)
                { temp2 = temp1;
                  temp1 = temp1->nxt;
                }
               delete temp1;
               temp2->nxt = NULL;
             }
        }
   }

void move_current_on ()
   { if (current->nxt == NULL)
      cout << "You are at the end of the list." << endl;
     else
      current = current->nxt;
   }

void move_current_back (int what)
   { 
	if(what==1){
	if (current == start_ptr)
      cout << "You are at the start of the list" << endl;
     else
      { node *previous;     // Declare the pointer
        previous = start_ptr;

        while (previous->nxt != current)
          { previous = previous->nxt;
          }
        current = previous;
      }
	}
	else if(what ==2){
	
		if (current == start_ptr2)
      cout << "You are at the start of the list" << endl;
     else
      { node *previous;     // Declare the pointer
        previous = start_ptr2;

        while (previous->nxt != current)
          { previous = previous->nxt;
          }
        current = previous;
      }
	}
   }

void addDataToLists(){

	for(int i=0;i<=2;i++){
		add_node_at_end(1,0);
	}

	for( i=0;i<=4;i++){
		add_node_at_end(2,0);
	}

//	display_list(1);
	cout <<endl;
//	display_list(2);
	cout <<endl;

		

}


void addElementstoNewList(){

	node *temp;
	temp = start_ptr;


	while (temp != NULL)
	   {  // Display details for what temp points to
             
              
	      
	      if (temp == current)
		
            add_node_at_end(3,temp->number);
	        temp = temp->nxt;

	   }
	
	temp=start_ptr2;
	while (temp != NULL)
	   {  // Display details for what temp points to
             
              
	      
	      if (temp == current)
		
            add_node_at_end(3,temp->number);
	        temp = temp->nxt;

	   }

display_list(3);
	
}

void main()
  {  start_ptr = NULL;
	 start_ptr2 = NULL;
	 start_ptr3 = NULL;
     addDataToLists();
     do
	{
		
	  cout << endl;
	  cout << "Please select an option : " << endl;
	  cout <<"1.Sort link list A in ascending order using a special sorting function\n  by passing a pointer to the function.\n\n"
		   <<"2.Sort link list B in descending order using a special sorting function\n  by passing a pointer to the function.\n\n"
	       <<"3.Add an element with a value of 001 at the end of the first link list A.\n\n"
           <<"4.Add an element with a value of 002 at the beginning of second link.\n\n"
           <<"5.Join both link lists as A and B in one link list.\n\n"
           <<"6.Add one element in the middle of the new link list with a value of 003.\n\n"
           <<"7.Add all the elements in the link list.\n\n"
           <<"8.Print the values of all elements and their sum on the screen.\n\n"
		   <<"0.EXIT"<<endl<<endl;

          cout << endl << " >> ";
	  cin >> option;

	  switch (option)
	    {
	      case 1 ://selection_sort( );
				  //display_list(1);
			  break;
	      case 2 : //delete_start_node();
			  break;
	      case 3 :
				add_node_at_end(1,0);
			  	display_list(1);
				cout <<endl;
			  break;
	      case 4 :
			  for(i=0;i<5;i++){
				  move_current_back (2);}
					add_node_at_end(2,0);

			  break;
          case 5 :addElementstoNewList();
			  break;
	      case 6 : //add_node_at_end();
			  break;
	      case 7 :// delete_start_node(); 
			  break;
	      case 8 :// delete_end_node();
			  break;
		  case 0 : exit(0);
	    }
	}
     while (option != 0);
  }

This is what i have come up with .Pls help me with sorting part

>>1. Sort link list A in ascending order using a special sorting function by passing a pointer to the function.

Your selection_sort function does not mee that requirement. Is selection sort algorithm required? or can you use any sort algorithm you want? And where is the parameter ?

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.