Thread: linked list

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    18

    linked list

    what's wrong with this coding?

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    typedef struct node{
            int num;        
            struct node *link;
    }NODE;
    
    int main (void){
        
        NODE *ptr1, *ptr2, *ptr3, *ptr4, *ptr5, *pList,*pCurr;
        
        ptr1 = (NODE *)malloc(sizeof(NODE));
        ptr1->num = 89;
        ptr2 = (NODE *)malloc(sizeof(NODE));
        ptr2->num = 75;
        ptr3 = (NODE *)malloc(sizeof(NODE));
        ptr3->num = 34;
        ptr4 = (NODE *)malloc(sizeof(NODE));
        ptr4->num = 22;
        ptr5 = (NODE *)malloc(sizeof(NODE));
        ptr5->num = 21;
        
        pList = ptr1;  //a head pointer points to the first node
        ptr1->link = ptr2;
        ptr2->link = ptr3;
        ptr3->link = ptr4;
        ptr4->link = ptr5;
        ptr5->link = NULL;
        
    
        //printf("&#37;d\t%d\t%d\t%d\t%d", pList->data, pList->link->data, pList->link->data, pList->link->data, pList->link->data);
        //or
        printf("\n");
        for(pCurr = pList; pCurr != NULL; pCurr = pCurr->link)
               printf("%d\t", pCurr->num); 
    }
    i'm trying to make a program that contains a linked list of five integer numbers and display it in linked list.. it turns out my program have too many errors.. what's the header file for malloc?
    Last edited by princez90; 04-21-2008 at 06:50 PM.

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    18
    it's okay.. i knew it already.. the error is because i didn't use a header file for malloc.. it's #include <malloc.h>..

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    The proper header for malloc() is stdlib.h. I suppose if you have an ancient system you might need malloc.h, but try stdlib.h first. You also needn't cast the result of malloc() when you're using C.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    And don't use conio.h (In fact you're not even using it)... just including it (which you shouldn't be).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  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, 10:21 PM