6 Replies - 9171 Views - Last Post: 13 May 2009 - 09:13 AM Rate Topic: -----

#1 anya_ritika  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 16-March 08

linked list using class(edit my code)

Post icon  Posted 12 May 2009 - 05:19 AM

my code is supposed to take parameter from command line do these...
A uri ip : add URI and IP string pairs to the link list.
D uri : delete the URI and its associated IP from the link list.
U uri : given a URI find the IP address from the link list.
I ip : given an IP find the URI from the link list.
N : print out the number of items in the link list then
a line feed.

There may be several commands on the command line, for example-
- "A www.x.y 1.2.3.4 A www.a.b 7.8.9.10 N"
response "2".
- "A www.x.y 1.2.3.4 A www.a.b 7.8.9.10 U www.a.b"
response "7.8.9.10".
- "A www.x.y 1.2.3.4 A www.a.b 7.8.9.10 U www.a.B"
response "nil".
- "A www.x.y 1.2.3.4 A www.a.b 7.8.9.10 u www.a.B"
response "command error".

this is basic code below...but it doesnt do exectly what is require...please help///

#include <iostream>
#include <cstdlib>

using namespace std;
 
class linklist
{
	 private:
 
			 struct node
		 {
					char *ip;
					 char *uri;	  
  
			 
					 node *link;
		 }*p;
 
   public:
 
			 linklist();
		 void append( char *ip,char *uri );
				 void del( char *ip );
		 void display(char *ip,char *uri);
		 int count();
		 ~linklist();
};
 
linklist::linklist()
{
	 p=NULL;
}
 
void linklist::append(char *ip,char *uri)
{
	 node *q,*t;
 
   if( p == NULL )
   {
		p = new node;
	  p->ip = ip;
	  p->uri=uri;
	  p->link = NULL;
   }
   else
   {
		q = p;
	  while( q->link != NULL )
		   q = q->link;
 
	  t = new node;
	  t->ip = ip;
	  t->uri=uri;
	  t->link = NULL;
	  q->link = t;
   }
}
   
void linklist::del( char *ip )
{
	 node *q,*r;
   q = p;
   if( q->ip == ip )
   {
		p = q->link;
	  delete q;
	  return;
   }
 
   r = q;
   while( q!=NULL )
   {
		if( q->ip == ip )
	  {
		   r->link = q->link;
		 delete q;
		 return;
	  }
 
	  r = q;
	  q = q->link;
   }
   cout<<"\nElement "<<ip<<" not Found.";
}
 
void linklist::display(char *ip,char *uri)
{
	 node *q;
   cout<<endl;
 
   for( q = p; q != NULL; q = q->link )
		cout<<endl<<q->ip<<" "<<q->uri<<endl;
 
}
 
int linklist::count()
{
	 node *q;
   int c=0;
   for( q=p; q != NULL; q = q->link )
		c++;
 
   return c;
}
 
linklist::~linklist()
{
	 node *q;
   if( p == NULL )
		return;
 
   while( p != NULL )
   {
		q = p->link;
	  delete p;
	  p = q;
   }
}
 
int main(int argc,char * argv[])
{
	 
   for(int i=1; i<argc; i++) {
	char letter = argv[i][0]; 
			   linklist ll;
	   cout<<"No. of elements = "<<ll.count();
					ll.append(argv[i++],argv[i++]);   
		   
							
				 ll.display(argv[i++],argv[i++]);					   
   
  cout<<"\nNo. of elements = "<<ll.count();
 
   ll.del(argv[i++]);
   }

   system ("pause");
   return 0;
} 


i really need it...

Is This A Good Question/Topic? 0
  • +

Replies To: linked list using class(edit my code)

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: linked list using class(edit my code)

Posted 12 May 2009 - 05:44 AM

Please give us some more details of your problem.
( a ) Does your code compile?
( b ) Any errors or warnings?
( c ) Is the program producing any output?
( d ) How is the actual output different to what you want / expect? Give details.
Was This Post Helpful? 0
  • +
  • -

#3 anya_ritika  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 16-March 08

Re: linked list using class(edit my code)

Posted 13 May 2009 - 03:41 AM

i have made some changes to my code...here is the new version...
#include <iostream>
#include <cstdlib>

using namespace std;
 
class linklist
{
	 private:
 
			 struct node
		 {
					char *ip;
					 char *uri;	  
  
			 
			  //int data;
			node *link;
		 }*p;
 
   public:
 
			 linklist();
		 void append( char *ip,char *uri );
		 //void add_as_first( int num );
		 //void addafter( int c, int num );
		 void del( char *ip );
		 void display(char *ip,char *uri);
		 int count();
		 ~linklist();
};
 
linklist::linklist()
{
	 p=NULL;
}
 
void linklist::append(char *ip,char *uri)
{
	 node *q,*t;
 
   if( p == NULL )
   {
		p = new node;
	  p->ip = ip;
	  p->uri=uri;
	  p->link = NULL;
   }
   else
   {
		q = p;
	  while( q->link != NULL )
		   q = q->link;
 
	  t = new node;
	  t->ip = ip;
	  t->uri=uri;
	  t->link = NULL;
	  q->link = t;
   }
}
 
/*void linklist::add_as_first(int num)
{
	 node *q;
 
   q = new node;
   q->data = num;
   q->link = p;
   p = q;
}*/
 
/*void linklist::addafter( int c, int num)
{
	 node *q,*t;
   int i;
   for(i=0,q=p;i<c;i++)
   {
		q = q->link;
	  if( q == NULL )
	  {
		   cout<<"\nThere are less than "<<c<<" elements.";
		 return;
	  }
   }
 
   t = new node;
   t->data = num;
   t->link = q->link;
   q->link = t;
}*/
void linklist::del( char *ip )
{
	 node *q,*r;
   q = p;
   if( q->ip == ip )
   {
		p = q->link;
	  delete q;
	  return;
   }
 
   r = q;
   while( q!=NULL )
   {
		if( q->ip == ip )
	  {
		   r->link = q->link;
		 delete q;
		 return;
	  }
 
	  r = q;
	  q = q->link;
   }
   cout<<"\nElement "<<ip<<" not Found.";
}
 
void linklist::display(char *ip,char *uri)
{
	 node *q;
   cout<<endl;
 
   for( q = p; q != NULL; q = q->link )
		cout<<endl<<q->ip<<" "<<q->uri<<endl;
 
}
 
int linklist::count()
{
	 node *q;
   int c=0;
   for( q=p; q != NULL; q = q->link )
		c++;
 
   return c;
}
 
linklist::~linklist()
{
	 node *q;
   if( p == NULL )
		return;
 
   while( p != NULL )
   {
		q = p->link;
	  delete p;
	  p = q;
   }
}
 
int main(int argc,char * argv[])
{
	 
   for(int i=1; i<argc; i++) {
	char letter = argv[i][0]; 
			   linklist ll;
			   switch (letter){					   
			 case 'A': ll.append(argv[i++],argv[i++]); break;						
			 case 'D': ll.del(argv[i++]); break;						
			 //case 'U': optionU(list, argv[++i]); break;						
			 //case 'I': optionI(list, argv[++i]); break;						
			 case 'N': cout<<"\nNo. of elements = "<<ll.count(); break;						
			 case 'L':  ll.display(argv[i++],argv[i++]); break;						
			 default:	  cout<<"command error"<<endl;break;							  
			  //exit(1);								
			  //break;				
			  } 
   
}

   system ("pause");
   return 0;
} 



although the code compiles...however, it does not do what is required/..
when i run:
code.cpp A www.a.d.v 1.2.3.4
it gives me "no of elements=0..."
which is not correct...
Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: linked list using class(edit my code)

Posted 13 May 2009 - 04:10 AM

View Postanya_ritika, on 13 May, 2009 - 02:41 AM, said:

it gives me "no of elements=0..."
which is not correct...


And what would be "correct"?

Read item ( d ) of posting #2
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6079
  • View blog
  • Posts: 23,550
  • Joined: 23-August 08

Re: linked list using class(edit my code)

Posted 13 May 2009 - 04:52 AM

You're creating and deleting your linked list with every execution of the loop:
for(int i=1; i<argc; i++) 
{
    char letter = argv[i][0];
    linklist ll;
    switch (letter){                      
        case 'A': 
            ll.append(argv[i++],argv[i++]); 
            break;                        
        case 'D': 
            ll.del(argv[i++]); 
            break;                        
        //case 'U': optionU(list, argv[++i]); break;                        
        //case 'I': optionI(list, argv[++i]); break;                        
        case 'N': 
            cout<<"\nNo. of elements = "<<ll.count();    
            break;                        
        case 'L':  
            ll.display(argv[i++],argv[i++]); 
            break;                        
        default:      
            cout<<"command error <<endl;
            break;                              
            //exit(1);                                
            //break;                
    }
} 


Was This Post Helpful? 0
  • +
  • -

#6 amin_mubarak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 31-March 08

Re: linked list using class(edit my code)

Posted 13 May 2009 - 05:12 AM

hello
this code run correct
#include <iostream>
#include <cstdlib>
#include <string.h>
using namespace std;
 
class linklist
{
	 private:
 
			 struct node
		 {
					char ip[100];
					char uri[100];	  
					 node *link;
		 }*p;
 
   public:
			 linklist();
		 void append( char ip[],char uri[] );
				 void del( char ip[] );
		 void display();
		 int count();
		 ~linklist();
};
 
linklist::linklist()
{
	 p=NULL;
}
 
void linklist::append(char ip[],char uri[])
{
	 node *q,*t;
 
   if( p == NULL )
   {
		p = new node;
	  strcpy(p->ip,ip);
	  strcpy(p->uri,uri);
	  p->link = NULL;
   }
   else
   {
		q = p;
	  while( q->link != NULL )
		   q = q->link;
 
	  t = new node;
	  strcpy(t->ip,ip);
	  strcpy(t->uri,uri);
	  t->link = NULL;
	  q->link = t;
   }
}
   
void linklist::del( char ip[] )
{
	 node *q,*r;
   q = p;
   if(strcmp(q->ip,ip)==0)
   {
		p = q->link;
	  delete q;
	  return;
   }
 
   r = q;
   while( q!=NULL )
   {
		if(strcmp(q->ip,ip)==0)
	  {
		   r->link = q->link;
		 delete q;
		 return;
	  }
 
	  r = q;
	  q = q->link;
   }
   cout<<"\nElement "<<ip<<" not Found.";
}
 
void linklist::display()
{
	 node *q;
   cout<<endl;
 
   for( q = p; q != NULL; q = q->link )
		cout<<endl<<q->ip<<" "<<q->uri<<endl;
 
}
 
int linklist::count()
{
	 node *q;
   int c=0;
   for( q=p; q != NULL; q = q->link )
		c++;
 
   return c;
}
 
linklist::~linklist()
{
	 node *q;
   if( p == NULL )
		return;
 
   while( p != NULL )
   {
		q = p->link;
	  delete p;
	  p = q;
   }
}
 
int main(int argc,char * argv[])
{
	  linklist ll;
   for( i=1; i<argc; i++) {
	char letter = argv[i][0];
			   
			   switch (letter){					  
			 case 'A': ll.append(argv[++i],argv[++i]); break;						
			 case 'D': ll.del(argv[i+1]); break;						
			 //case 'U': optionU(list, argv[++i]); break;						
			 //case 'I': optionI(list, argv[++i]); break;						
			 case 'N': cout<<"\nNo. of elements = "<<ll.count(); break;						
			 case 'L':  ll.display(); break;						
			 default:	  cout<<"command error"<<endl;break;							  
			  //exit(1);								
			  //break;				
			  }
			  
   }
   system ("pause");
   return 0;
} 


i test it
bye

This post has been edited by amin_mubarak: 13 May 2009 - 05:13 AM

Was This Post Helpful? 0
  • +
  • -

#7 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 976
  • Joined: 09-May 09

Re: linked list using class(edit my code)

Posted 13 May 2009 - 09:13 AM

View Postamin_mubarak, on 13 May, 2009 - 04:12 AM, said:

hello
this code run correct
#include <iostream>
#include <cstdlib>
#include <string.h>
using namespace std;
 
class linklist
{
	 private:
 
			 struct node
		 {
					char ip[100];
					char uri[100];	  
					 node *link;
		 }*p;
 
   public:
			 linklist();
		 void append( char ip[],char uri[] );
				 void del( char ip[] );
		 void display();
		 int count();
		 ~linklist();
};
 
linklist::linklist()
{
	 p=NULL;
}
 
void linklist::append(char ip[],char uri[])
{
	 node *q,*t;
 
   if( p == NULL )
   {
		p = new node;
	  strcpy(p->ip,ip);
	  strcpy(p->uri,uri);
	  p->link = NULL;
   }
   else
   {
		q = p;
	  while( q->link != NULL )
		   q = q->link;
 
	  t = new node;
	  strcpy(t->ip,ip);
	  strcpy(t->uri,uri);
	  t->link = NULL;
	  q->link = t;
   }
}
   
void linklist::del( char ip[] )
{
	 node *q,*r;
   q = p;
   if(strcmp(q->ip,ip)==0)
   {
		p = q->link;
	  delete q;
	  return;
   }
 
   r = q;
   while( q!=NULL )
   {
		if(strcmp(q->ip,ip)==0)
	  {
		   r->link = q->link;
		 delete q;
		 return;
	  }
 
	  r = q;
	  q = q->link;
   }
   cout<<"\nElement "<<ip<<" not Found.";
}
 
void linklist::display()
{
	 node *q;
   cout<<endl;
 
   for( q = p; q != NULL; q = q->link )
		cout<<endl<<q->ip<<" "<<q->uri<<endl;
 
}
 
int linklist::count()
{
	 node *q;
   int c=0;
   for( q=p; q != NULL; q = q->link )
		c++;
 
   return c;
}
 
linklist::~linklist()
{
	 node *q;
   if( p == NULL )
		return;
 
   while( p != NULL )
   {
		q = p->link;
	  delete p;
	  p = q;
   }
}
 
int main(int argc,char * argv[])
{
	  linklist ll;
   for( i=1; i<argc; i++) {
	char letter = argv[i][0];
			   
			   switch (letter){					  
			 case 'A': ll.append(argv[++i],argv[++i]); break;						
			 case 'D': ll.del(argv[i+1]); break;						
			 //case 'U': optionU(list, argv[++i]); break;						
			 //case 'I': optionI(list, argv[++i]); break;						
			 case 'N': cout<<"\nNo. of elements = "<<ll.count(); break;						
			 case 'L':  ll.display(); break;						
			 default:	  cout<<"command error"<<endl;break;							  
			  //exit(1);								
			  //break;				
			  }
			  
   }
   system ("pause");
   return 0;
} 


i test it
bye


this is not your code ive seen this all over the internet!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1