For my next lab test I am required to write a DNS storage and retrival program using classes and a linked list structure. The program is required to take an ip and url pair from the command line and add them to the linked list. The Letter A denotes the ip and url pair which is to be added to the list eg:
A www.google.com 64.125.19.2
The program is also required to find specific ip's, defined by the letter I, and print out the corresponding url, find specific url's, defined by the letter U and print out the corresponding ip, print out the number of ip and uri pairs in the list, defined by the letter N and delete uri's and it's corresponding ip, defined by the letter D.
The issue I am currently having is that I am trying to use one function to search for an url and it's ip and search for an ip and it's url. For example, if I were to type in the following into the command line:
U www.google.com
the answer would be 64.125.19.2.
Likewise:
I 64.125.19.2
would give an answer www.google.com
However, if I were to type "I www.google.com" the answer I get is www.google.com, when I should be getting "nil" instead. The simplest solution would be to simply make two seperate functions but I would like to know if it's possible to do it with one function. My code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
#include <iostream>
#include <cstdlib>
using namespace std;
class ip_uri_store
{
public:
ip_uri_store(string add_uri, string add_ip);
ip_uri_store* find(string target);
string uri_found();
string ip_found();
void delete_item();
private:
string uri;
string ip;
ip_uri_store *next;
};
ip_uri_store *list_head = NULL;
int n=0;
ip_uri_store::ip_uri_store(string add_uri, string add_ip)
{
uri = add_uri;
ip = add_ip;
next = list_head;
list_head = this;
n=n+1;
}
void delete_item()
{
//To be my delete function. I've having trouble trying to figure this out.
//I was planning on writing the program by incorporating the data into a
//"struct" to make it easier to manipulate data for deleting items
//but the data needs to be protected, hence it's current format.
//If anyone has any suggestions I would appreciate it.
}
//the following is the url-ip search function I was talking about
ip_uri_store* ip_uri_store::find(string target)
{
if (uri == target || ip == target)
return(this);
if (next == NULL)
return(NULL);
return(next->find(target));
}
string ip_uri_store::uri_found()
{ return(uri) ;
}
string ip_uri_store::ip_found()
{ return(ip) ;
}
int main(int argc, char *argv[])
{
if (argc == 1)
{
cout << "No Parameters"<< endl ;
return(0) ;
}
ip_uri_store *found;
for(int i=1; i<argc; i++)
{
if(strlen(argv[i]) == 1)
{
char options;
options = *argv[i];
switch(options)
{
case 'A':
new ip_uri_store(argv[i+1], argv[i+2]);
break;
case 'D':
break;
case 'U':
found = list_head->find(argv[i+1]);
if ( found != NULL)
cout << found->ip_found() << endl ;
else cout << "nil" << endl;
break;
case 'I':
found = list_head->find(argv[i+1]);
if ( found != NULL)
cout << found->uri_found() << endl ;
else cout << "nil" << endl;
break;
case 'N':
cout << n <<endl; break;
case 'T': break;
case 't': break;
default: cout << "command error" << endl; return(0);
}
}
}
return(0);
}
|