Linked List retriving problems

This is a discussion on Linked List retriving problems within the C++ Programming forums, part of the General Programming Boards category; I have these two functions that will add a link to a list and will also get a particular part ...

  1. #1
    c0w
    Guest

    Linked List retriving problems

    I have these two functions that will add a link to a list and will also get a particular part of the linked data. When the array is filled with the getuser function it fills it with junk data.
    Code:
    // Add user to the linked list
    void adduser(char *ipaddr, char username[], LINK first)
    {
    	LINK newE;
    	LINK tmp; 
    	LINK prev;
    	
    	if ( (newE = (LINK)malloc(sizeof(LIST))) == NULL)
    	{
    		cout << "Not enough memory";
    		exit(0);
    	}
    	
    	strcpy(newE->ip, ipaddr);
    	strcpy(newE->user, username);
    
    	if ( first == NULL )
    	{
    		first=newE;
    		newE->next=NULL;
    	}
    	else 
    	{
    		tmp = first->next;
    		prev=first;
    		while( tmp->next != NULL )
    		{
    			newE->next = tmp;
    			if (newE->next != prev->next)
    			{
       				cout << "ERROR";
        				exit(0);
    			}
    			prev->next = newE;
    			
    		}
    		if (tmp->next == NULL )
    		{
    			tmp->next = newE;
    			newE->next = NULL;
    		}
    	}
    }
    
    
    // Retrieve a user from the list
    void getuser(char name[], char *ipaddr, LINK first)
    {
    	LINK cur_ptr, next;
    	cur_ptr = first;
    
    	while ( cur_ptr != NULL )
    	{
    		next = cur_ptr->next;
    		if ( strcmp( ipaddr, cur_ptr->ip) == 0 )
                   strcpy( name, cur_ptr->user );
    		cur_ptr = next;
         }
    }
    ip is a char array of 20 and user is a char array of 30
    typedef struct data LIST;
    typedef LIST *LINK;
    and first is a value that is initialised in the main source to NULL.

  2. #2
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    169
    > void adduser(char *ipaddr, char username[], LINK first)

    The 'first' parameter needs to be the address of a pointer in order to update its pointer, otherwise only the local stack pointer will be updated, which will be lost on return.
    Code:
    main()
    {
         LINK first = NULL;
    
         adduser("1.1.1.1", "BATMAN", &first);
        ...  
    }
    
    void adduser(char *ipaddr, char username[], LINK *first)
    {
         if ( *first == NULL )
         {
              *first=newE;
              newE->next=NULL;
         }   
    }
    Last edited by Scarlet7; 05-23-2003 at 05:25 PM.

  3. #3
    c0w
    Guest
    I recieved errors of argument three in adduser cannot be of
    LIST**.

  4. #4
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    169
    This is compiles ok
    Code:
      
    #include <stdlib.h> 
    #include <malloc.h>
    #include <string.h>
    #include <iostream.h>
    
    struct data;
    typedef struct data LIST;
    typedef LIST *LINK;
    
    struct data
    {
         char ip[20];
         char user[30];
         LIST *next ;
    };
    
    
    // Add user to the linked list
    void adduser(char *ipaddr, char username[], LINK *first)
    {
         LINK newE;
         LINK tmp; 
         LINK prev;
         
         if ( (newE = (LINK)malloc(sizeof(LIST))) == NULL)
         {
              cout << "Not enough memory";
              exit(0);
         }
         
         strcpy(newE->ip, ipaddr);
         strcpy(newE->user, username);
    
         if ( *first == NULL )
         {
              *first=newE;
              newE->next=NULL;
         }
         else 
         {
              tmp = (*first)->next;
              prev=*first;
              while( tmp->next != NULL )
              {
                   newE->next = tmp;
                   if (newE->next != prev->next)
                   {
                           cout << "ERROR";
                            exit(0);
                   }
                   prev->next = newE;
                   
              }
              if (tmp->next == NULL )
              {
                   tmp->next = newE;
                   newE->next = NULL;
              }
         }
    }
    
    // Retrieve a user from the list
    void getuser(char name[], char *ipaddr, LINK first)
    {
         LINK cur_ptr, next;
         cur_ptr = first;
    
         while ( cur_ptr != NULL )
         {
              next = cur_ptr->next;
              if ( strcmp( ipaddr, cur_ptr->ip) == 0 )
                   strcpy( name, cur_ptr->user );
              cur_ptr = next;
         }
    }
    
    main()
    {
         LINK first = NULL;
         char name[30];
    
         adduser("1.1.1.1", "BATMAN", &first);
         getuser( name, "1.1.1.1", first);
    
         cout <<  name << endl;
    
         return 0;
    }
    Last edited by Scarlet7; 05-24-2003 at 05:38 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 09:04 PM
  2. Linked List Help!
    By mbk in forum C Programming
    Replies: 3
    Last Post: 01-31-2008, 03:54 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  4. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 02:29 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21