Thread: Linked list problem

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    47

    Linked list problem

    Hi ,is there anybody,who can help me with this code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    typedef
    struct students  
    {
      int ID;
      char  *Name;
      float GPA;
      struct students *Next;
    }record;
     void insert(record *pr,int id,char *n,float sc);
     void printList(record *ptr_head);
    int main()
    {
         record *ptr_head=NULL;
        insert(&ptr_head,1111,"Jimi",80.6);
         printList(ptr_head);
         getch();
    	 return 0;
    }
       void insert(record *pr,int id,char *n,float sc)
        {
           record head;
           record tail;
           record curr;
           head=malloc(sizeof(record));
           if (head != NULL)
           {
             head->ID=id;
             head->Name=n;
             head->GPA=sc;
             head->Next=NULL;
             tail=NULL;
             curr=*pr;
          while (curr!=NULL && value >curr->ID)
           {
             tail=curr;
             curr=curr->Next;
           }
          if (tail==NULL)
            {
           head-> Next=*pr;
           *pr=head;
            } 
          else
            {
             tail->Next=head;
             head->Next=curr;
            }      
         }
          else
        {
         printf("%c not inserted.No memory avalible",value);
         }
          }
    
       void printList(record *ptr_head)
       {
        while(ptr_head){
           printf("%5d : %10s : %2.3f \n",ptr_head->ID,ptr_head->Name,ptr_head->GPA);
           ptr_head =ptr_head->Next;
                       }
       }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    It would help... rather a lot... if you could tell us what the problem is.

  3. #3
    Registered User hellork's Avatar
    Join Date
    Nov 2010
    Posts
    39
    Good effort.
    * malloc returns an address. You are trying to assign that to a variable of type record which is"typedef struct students" not a pointer.
    * malloc uses memory which must be free()'d to prevent a memory leak
    * many more variables-used-as-pointer errors.

    Read Cprogramming.com FAQ > A tutorial on pointers and try something simpler involving pointers before diving into linked lists.
    Last edited by hellork; 11-23-2010 at 10:25 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Linked list ()
    By caraie in forum C Programming
    Replies: 7
    Last Post: 08-30-2010, 01:56 AM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM