Linked list search

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 30
Reputation: mussa187 is an unknown quantity at this point 
Solved Threads: 0
mussa187 mussa187 is offline Offline
Light Poster

Linked list search

 
0
  #1
Apr 28th, 2008
What i am trying to to is the following

when user inputs ./test U www.google.com I <ip here>
same for ./test U <URL here> I <IP here>

using command line input argv and argc here is what i have done so far,
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <string>
  4.  
  5.  
  6. using namespace std;
  7.  
  8. struct str_pair
  9. {
  10. char ip [50];
  11. char uri [50];
  12. str_pair *next;
  13. int counter;
  14. };
  15. str_pair *str_ptr;// pointer to an order struct,
  16. str_pair *list_head;// points to nothing initially.
  17.  
  18. const int list_size=; [COLOR="Red"]i need to know the list size!![/COLOR]
  19. int main(int argc, char *argv[])
  20. {
  21. if (argc == 1)
  22. { cout << "my name"<<endl;
  23. return(0);
  24. }
  25. //--- initialize link list to empty.
  26. list_head = NULL;// can test for NULL to see if anything is there.
  27. //str_ptr = new str_pair; //--- Create data at run time on the heap.
  28. for (int i=0; i<list_size; i++)
  29. {
  30. str_ptr = list_head; // save old head of list.
  31. list_head = new str_pair; // create new item.
  32. list_head->next= str_ptr; // get next item to point to previous head.
  33. list_head->counter =i;
  34. }
  35. strcpy(
  36. str_ptr=list_head;
  37. while(str_ptr != NULL)
  38. {if(strcmp
  39. return(0);
  40. }
also a hint when where is showed add the ip addresses and uri.
Last edited by Ancient Dragon; Apr 28th, 2008 at 9:22 am. Reason: add code tags, removed url and ip address
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,485
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Linked list search

 
0
  #2
Apr 28th, 2008
Why don't you use c++ strings and vectors instead of re-inventing the wheel
  1. #include <string>
  2. #include <vector>
  3. using std::string;
  4. using std::vector;
  5. // other using statements here
  6.  
  7. class str_pair
  8. {
  9. public:
  10. str_pair() { counter = 0; }
  11.  
  12. str_pair(string ip, string uri)
  13. {
  14. this->uri = uri;
  15. this->ip = ip;
  16. counter = 0;
  17. }
  18. void setPair(string ip, string url)
  19. {
  20. this->uri = uri;
  21. this->ip = ip;
  22. counter = 0;
  23.  
  24. }
  25. public:
  26. string ip;
  27. string uri;
  28. int counter;
  29. };
  30.  
  31. vector<str_pair> list_head;
  32.  
  33. int main(int argc, char* argv[])
  34. {
  35. string_pair node;
  36. node.setPair(argv[1],argv[2]);
  37. // add string_pair to the vector
  38. list_head.push_back(node);
  39. }

what are those pieces of code after line 35?
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 30
Reputation: mussa187 is an unknown quantity at this point 
Solved Threads: 0
mussa187 mussa187 is offline Offline
Light Poster

Re: Linked list search

 
0
  #3
Apr 28th, 2008
Its just the format we are suppose to follow, and the codes after line 35 are gonna be used to match the ip and the uri when they are passed on the command line.

these is what supposed to happen running the progam from command line and passing a parameter eg

./program_name and then the command U(uri) and then an ip address(192....)

now for the most important question where can I add the ip and the uri?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,485
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Linked list search

 
0
  #4
Apr 28th, 2008
see the main() that I posted previously. If the format is ./test U <URL here> I <IP here> then url will be in argv[2] and ip will be in argv[4]. That assumes a space between U and the URL and another space between I and the IP address.
Last edited by Ancient Dragon; Apr 28th, 2008 at 9:43 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 30
Reputation: mussa187 is an unknown quantity at this point 
Solved Threads: 0
mussa187 mussa187 is offline Offline
Light Poster

Re: Linked list search

 
0
  #5
Apr 30th, 2008
This is all good but we have to use pointers in the form i have shown in my code!!!!
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,699
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 273
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Linked list search

 
0
  #6
Apr 30th, 2008
1) "where can I add the ip and the uri?"

I'm not sure exactly you mean by that question.

It is possible, but ungainly, to have the user enter (add) all the data needed as input for the program from the command line. It is more common for the program to get the majority of the input it needs from the keyboard, a file, or some source other than the command line.

On the other hand I would assign (add) the data to each new node before it is added to the list, so probably between lines 31 and 32.

2) Having each str_pair object have it's own counter seems counter intuitive. Are you sure that's what you want?

3) All variables declared with the const keyword must have their value entered by the programmer before compilation, meaning the value of the const variable can not be information provided to the program at run time.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 30
Reputation: mussa187 is an unknown quantity at this point 
Solved Threads: 0
mussa187 mussa187 is offline Offline
Light Poster

Re: Linked list search

 
0
  #7
Apr 30th, 2008
I want most of the data to be created at runtime that is why i am trying to use pointers. The purpose of the program is to find the corsponding uri for the given ip address!!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,485
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Linked list search

 
0
  #8
Apr 30th, 2008
first the program needs to generate a linked list of those structures. To do that you will have to prompt for the structure values then add the structure to the list. Put that into a loop until the user says he/she wants to stop. You could also expedite this by reading the data from a data file so that you don't have to type them every time you run the program.

After that is finished prompt for and get input for the IP address to search for. Then iterate through the linked list comparing the IP address of each node to the IP address you just entered.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC