BACKGROUND Your company is writing software for a new networking device and
you have the task of managing the DNS storage and retrieval.
DNS must translate between an Internet IP address such as
74.125.19.99 and the URI (in this case www.google.com).
The networking device is not available so the code has been
split into segments and each bit is being developed on a PC.
You have been allocated a code segment that must use structures
and pointers to form a link list record of these pairs of strings
( IP naddress and URI).
It must allow insertion, deletion, and querying.


SPECIFIC REQUIREMENTS

* The command line parameters will consist of a DNS command and
then a number of IP-URI string pairs.
The DNS command is always a single letter followed by a search string.
A string pairs is always IP address then URI which must be placed
in the DNS store.
The example below has the DNS command U, search string 74.1.2.3,
and two IP-URI pairs.
lab3_s1235567 U 74.1.2.3 74.125.19.1 www.google.com 74.1.2.3 www.abc.net

* The program must insert all string pairs into a data structure defined
below in the proforma. It must then executed the command and print
the result (with an endl) to standard out.
In the example above the command is asking to find the URI of the
IP address 74.1.2.3. The output should be www.abc.net.

* Commands include-
U ip_address : find the IP address ip_address in the link list and
print out the matching URI.
I uri_name : search the link list for the URI given and print out
the matching IP address.


this is my code I am lost!!!

/*===================== linked_list_search.cpp ====================================

  PURPOSE - Show how to search a list link created at run time.

    USAGE - Read code carefully to understand it,
            then compile as "g++ -g -o x linked_list_search.cpp", run as "./x"

     NOTE - You must first understand the programs
             struct_and_ptr.cpp and linked_list-struct.cpp
          - The code creates a link list with amount values from
            zero upward and the name string filled in with the
            hexadecimal value of the amount.
            This is then serached to find a particular target
            in the name string,  for example 0x100.
*/


#include <iostream>   // IO library code to include.
#include <string>     // string functions.
using namespace std ; // Use standard C++ names for functions.


//====== structure type declaration, sets the form of structure 
//       but doesn't create one in memory.
typedef char ip50 [50] ; // 49 characters available for text,  
typedef char uri50 [50] ; //   need last string position for a zero.
typedef char boolean ;
const char TRUE  = 1 ;
const char FALSE = 0 ;

struct str_pair // all data associated with an individual string pair is in here.
 { int     amount ;
   ip50   ip ;
   uri50  uri;
   str_pair*  next ;     // poin to next item,  or set to NULL if end item.
 } ;


str_pair *list_head ;  // start of linked list, set to 1st item.
str_pair *work_ptr ;   // work pointer to work along list.


 
const int LIST_SIZE = 4096;//the combined arrays!!
const  ip50 abc_ip =" 74.1.2.3";
const uri50 abc_uri="www.abc.net";
const ip50 google_ip=" 74.125.19.1";
const uri50 google_uri="www.google.com";
char target[100] ;

//====== Start program here =================================================

int main(int argc, char *argv[]) // argc = number strings on command line.
                                 // argv is meter.

{//--- initialize link list with a loop.
   

list_head = NULL ;
   int i ;
   char I, U, ch;
   for ( i=0 ; i < LIST_SIZE ; i++ )
    { work_ptr = list_head ;       // save old head item.
      list_head = new str_pair ;      // create new item.
      list_head->next = work_ptr ; // get next item to point to previous head.

      list_head->amount = i ;
	//cout<<"amouhnt"<<list_head->amount<<endl;  // fill in payload data, i=0 upward.
      //sprintf(list_head->ip, "0x%x", i) ; // print value of i as hexadecimal.
    }

 //--- Now do a search for an item, simple linear search from start to end.


   strncpy( target, abc_uri, sizeof(target)-1); // not strictly necessary
   strcat( target,abc_ip);
cout<<target<<endl;
   work_ptr = list_head ;
   while ( work_ptr != NULL) // while still pointing to a valid item.
    { if ( strcmp( target, argv[2]) == 0)  // get a match.
        { cout << endl << "   Found it : given target name " << target <<
                  ",  value of amount was " << work_ptr->amount << endl << endl ;
          return(0) ; // break = exit from loop, return(0) = quit program.
        }
      work_ptr = work_ptr->next ; // move onto next item in list.
    }

 //--- If got here then no match was found.
   cout << endl << "   No match found for target " << target << endl << endl ;
   
   return(0) ;
   
}

Begin by writing some list functions, and not inlining all the code in main.

Say these prototypes

str_pair *listInit ( void );
str_pair *listAppend( str_pair *list, str_pair item );

The first one initialises the list (easy).
The second one makes a copy of item (read up on 'new'), appends the duplicate to the list, and returns the updated list.

You can add void listPrint( str_pair *list ); as a useful debug tool. Call this every time you modify the list to make sure it's still OK. If this crashes or produces garbage all of a sudden, you need to stop and investigate.

Why not generalise the linked list and then create one with a

struct
{
int ip;
int uri;
};

or something like this?

Is this is supposed to be written in C?

thanks for your help and here is my solution :)

#include <iostream>   // IO library code to include.
#include <string>     // string functions.
using namespace std ; // Use standard C++ names for functions.


//====== structure type declaration, sets the form of structure 
//       but doesn't create one in memory.
typedef char ip50 [50] ; // 49 characters available for text,  
typedef char uri50 [50] ; //   need last string position for a zero.
typedef char boolean ;
const char TRUE  = 1 ;
const char FALSE = 0 ;

struct str_pair // all data associated with an individual string pair is in here.
 { int     amount ;
   ip50   ip ;
   uri50  uri;
   str_pair*  next ;     // poin to next item,  or set to NULL if end item.
 } ;


str_pair *list_head ;  // start of linked list, set to 1st item.
str_pair *work_ptr ;   // work pointer to work along list.


 
const int LIST_SIZE = 8112;//the combined arrays!!
const  ip50 abc_ip ="74.1.2.3";
const uri50 abc_uri="www.abc.net";
const ip50 google_ip="74.125.19.1";
const uri50 google_uri="www.google.com";
char target[50] ;

//====== Start program here =================================================

int main(int argc, char *argv[]) // argc = number strings on command line.
                                 // argv is meter.

{//--- initialize link list with a loop.
   

list_head = NULL ;
   int i ;
   for ( i=0 ; i < LIST_SIZE ; i++ )
    { work_ptr = list_head ;       // save old head item.
      list_head = new str_pair ;      // create new item.
      list_head->next = work_ptr ; // get next item to point to previous head.

      list_head->amount = i ;
	
    }

   work_ptr = list_head ;//point to the head of the list
char ch;
  // storing argv[1] in char ch
  for (int k = 1; k < 2; ++k) {
    for (int j = 0; argv[k][j] != '\0'; ++j) {
       ch = argv[k][j];

      
    }

  }

   if(ch=='U'){//checking for input Universal Resources Indecator
while ( work_ptr != NULL) // while still pointing to a valid item.
    { if ( strcmp( abc_uri, argv[2]) == 0)  // get a match.
        { 
	strncpy( target, abc_uri, sizeof(target)-1);
	strcat( target,abc_ip);
	cout << endl << target << endl ;
          return(0) ;
	}
work_ptr = work_ptr->next ; // move onto next item in list.
}
work_ptr = list_head ;
while ( work_ptr != NULL){	
 if( strcmp(google_uri, argv[2]) == 0)
	{ 
	strncpy( target, google_uri, sizeof(target)-1);
	strcat( target,google_ip);
	cout << endl << target << endl ;
          return(0) ;
	}
work_ptr = work_ptr->next ; // move onto next item in list.
}
    }

   if(ch=='I'){//checking for input Ip address
while ( work_ptr != NULL) // while still pointing to a valid item.
    { if ( strcmp( abc_ip, argv[2]) == 0)  // get a match.
        { 
	strncpy( target, abc_ip, sizeof(target)-1);
	strcat( target,abc_uri);
	cout << endl << target << endl ;
          return(0) ;
	}
work_ptr = work_ptr->next ; // move onto next item in list.
}
work_ptr = list_head ;
while ( work_ptr != NULL){	
 if( strcmp(google_ip, argv[2]) == 0)
	{ 
	strncpy( target, google_ip, sizeof(target)-1);
	strcat( target,google_uri);
	cout << endl << target << endl ;
          return(0) ;
	}
work_ptr = work_ptr->next ; // move onto next item in list.
}
    }

 //--- If got here then no match was found.
   cout << endl << "nil"<< endl ;
   
   return(0) ;
   
}
This article has been dead for over six months. Start a new discussion instead.