simple linked list

This is a discussion on simple linked list within the C Programming forums, part of the General Programming Boards category; I'm using the following short code to help me understand linked lists (keep things simple at this stage!). The code ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    4

    simple linked list

    I'm using the following short code to help me understand linked lists (keep things simple at this stage!). The code compiles with the warning -
    ": warning C4133: "=" : incompatible types - from 'struct RECORD *' to 'struct RECORD *' .

    All I am checking with this code is that I can assign the address of the next location in the list to the current record (BTW I know that I have only one record in the list ).

    When I run the program it appears to work ok, but that warning is confusing me - can anyone help?

    Regards


    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    #define listSize 10
    
    
    
    /* global structure definitions */
    typedef struct 
    		{
    		   char                        name[10];
    		   struct RECORD	*nextPtr;
    			
    		} RECORD; /* end structure */
    
    
    int main(void)
    {
    /* declare local variables and initialise  */
        RECORD myRecord = {"abc", NULL}; 
        RECORD *listPtrStart = NULL, *listPtrCurrent = NULL;
    
    
    
        /* allocate sufficient memory to store the list */
        listPtrStart = (RECORD *) malloc(listSize*sizeof (RECORD));
    
        if(listPtrStart != NULL)
        {
                listPtrCurrent = listPtrStart;
    
                /* insert a copy of 'myRecord' into the list */
    	*listPtrCurrent = myRecord;
    
                /* move list pointer to next location and store its value */
    	 listPtrCurrent->nextPtr = listPtrCurrent + 1;
    		
          }
          else
          {
                     printf("Memory not allocated");
           }
    
    
    
          return 0; /* indicate successful termination */
    
    
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    typedef struct RECORD
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    1

    Post

    Hi there. I'm a bit new to the forums but I think the above poster meant that your structure should be declared:

    Code:
    typedef struct _RECORD
    {
    	char                        name[10];
    	struct _RECORD	*nextPtr;
    			
    } RECORD;
    Since you're using the struct you declared within it's own definition.

    You also don't need to have a macro for the list's size because linked lists by nature have no maximum size. So you're allocating way too much space with that malloc. I think it would be better to allocate each node (RECORD struct) as you go along and I wrote up an example of what I'm talking about:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    typedef struct _RECORD 
    {
      char                  *name;
      struct _RECORD	*nextPtr;
      
    } RECORD;
    
    
    int main(void)
    {
      /* Allocating a new linked list (LL) */
      RECORD *myRecord = (RECORD *) malloc( sizeof( RECORD ) ); 
      if (!myRecord) { printf("Memory not allocated\n");  return -1; }
    
      /* Couple of strings to add to LL's later */
      char *str1 = "abc";
      char *str2 = "def";
    
      /* Setting up the LL's data members */
      myRecord->name = str1;
      myRecord->nextPtr = NULL;
    
      /* Allocating another LL to add */
      RECORD *newRecord = (RECORD *) malloc( sizeof( RECORD ) );
      if (!newRecord) { printf("Memory not allocated\n");  return -1; }
      
      newRecord->name = str2;
      newRecord->nextPtr = NULL;
    
      /* Now we "link" the first node to the 2nd node */
      myRecord->nextPtr = newRecord;
    
      printf("First node's str: %s\n", myRecord->name);
      printf("Successor's str: %s\n", myRecord->nextPtr->name);
    
      free(myRecord);
      free(newRecord);
      return 0;
    }
    So the output of that code would be:
    abc
    def

    I hope this helps and if anyone sees anything that I did wrong please let me know

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list question
    By mikeman in forum C Programming
    Replies: 1
    Last Post: 11-30-2008, 01:56 PM
  2. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-28-2008, 12:24 AM
  3. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

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