Thread: Array of pointers to linked lists

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    11

    Array of pointers to linked lists

    Dear programmers,
    i have the following problem.
    The homework ask to create 3 linked lists and an array of pointers to them which is NULL at the beginning.
    I m stuck when it asks to enter values to the lists until the user enters a number >=1000.
    Also we have to make sure that every value in each list is distinct(no duplicate numbers are allowed).
    This is my code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <malloc.h>
    
    typedef struct node 
    {
    int temperature;
    struct node *next;
    }set;
    
     int main(void)
     {
    set *ap[]={NULL,NULL,NULL}; //array of pointers to linked lists
    set *p;
    int n;
    while(scanf("%d",&n)==1 && n<1000)
    
    {
    
        p=(set*)malloc(sizeof(set));
        p->temperature=n;
        p->next=ap[0];
        ap[0]=p;
    }
        
    
    for (p=ap[0];p;p=p->next)
    printf("%d ",p->temperature);
    return 0;
     }
    That code is without preventing duplicate values...
    i've made an function in order to search for duplicate values but i can't attach it to the code.. i call it like that exist(ap[0],n) but i think its wrong..

    Code:
    int exists(set *t , int value)
    {
        if(t==NULL)return 0;
        while (t!=NULL)
        {
            if(t->temperature==value)return 1;
            t=t->next;
        }
        return 0;
        }
    Please help me...
    Last edited by junglist; 05-06-2011 at 01:20 PM.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    First things you should fix:

    1) n is undeclared

    2) main() should be int main(void) and should return 0

    3) the return value of malloc should not be cast

    What do you mean "you think it's wrong"? It either is or isn't. What is your compiler telling you? What is happening when you run it? What test data are you feeding into it?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    11

    corrections

    made some changes.
    Thank you for the reply
    Quote Originally Posted by claudiu View Post
    First things you should fix:

    1) n is undeclared FIXED

    2) main() should be int main(void) and should return 0 FIXED


    3) the return value of malloc should not be cast what do you mean ?


    What do you mean "you think it's wrong"? It either is or isn't. What is your compiler telling you? What is happening when you run it? crashes
    What test data are you feeding into it? i want to pass the first element of the array of pointers(ap[0]) to the function exist

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    He means:
    Code:
    type *t = NULL;
    t = malloc( HOWMANY * sizeof *t );
    Notice how there is nothing between the word malloc and the = sign.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials: Array of pointers to linked lists?
    By eeengenious in forum C Programming
    Replies: 2
    Last Post: 04-19-2011, 01:55 AM
  2. Help with pointers and linked lists
    By Christine in forum C Programming
    Replies: 3
    Last Post: 02-13-2011, 11:21 AM
  3. Help with linked lists/ pointers
    By anything25 in forum C Programming
    Replies: 14
    Last Post: 06-26-2009, 02:41 PM
  4. Linked Lists & Pointers
    By fkheng in forum C Programming
    Replies: 4
    Last Post: 06-10-2003, 07:26 AM
  5. Problem with pointers and linked lists
    By bsimbeck in forum C Programming
    Replies: 2
    Last Post: 02-21-2003, 11:05 AM