Thread: Problem with Lists

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    60

    Problem with Lists

    I have been set a task to read an input using getchar() and then storing this in a list using calloc() to assign the memory space for it.

    The thing is each new character needs to be put at the head of the list. I have tried to create some code to do this but am encountering a problem, as I seem to be transferring a parameter between two functions that is a different data type.

    I am getting an "incompatible type for argument '1' of insertitem" error from the compiler which I presume points to the first item being different type to that in the function insertitem.

    The thing is that in my teaching class I have been taught to construct the functions for inserting into the list and adding new items to the head of a list like this. Which has confused me as they don't seem to work together, Have I been taught wrong?

    The relevant parts of code are highlighted.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct L{
            int x;
            struct L *next;
    }list;
    
    typedef struct {
            list *head;
            list *tail;
    } fulllist;
    
    list *insertlist(int hd, list *t1){
         list *t = calloc(1,sizeof(list));
         t->x = hd;
         t->next = t1;
         return t;
    }
    
    fulllist insertitem(fulllist a, int b){
             
             if (a.head == NULL){
                        a.head = a.tail = insertlist(b, NULL);
                        }
             else{
                        a.head = insertlist(b, a.head);
                        }
             return a;
    }
    
    list *mainlist(){
         list *h = NULL;
         int i = getchar();
         while (i != '.'){
               h = insertitem(h,i);
               }
         return h;
         }
    
    int main(){
        list *z = mainlist();
        return 0;
    }

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    heyy man, this code required too many changes. it looks like u haven't understood the proper usage of list (linked list) and stuff. I would suggest u have a look at some tutorials and get back to coding. Mean time did u look at the error messages given by the compiler.

    ssharish2005

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    60
    Yeh I did try with the error messages but it didn't really help.

    I thought I understood it but obviously not, i'll go back over the lecture notes I have got and through my textbook and give it another try.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  2. Lists: adding fields (not nodes) & more
    By Mariano L Gappa in forum C++ Programming
    Replies: 15
    Last Post: 11-09-2005, 07:26 PM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM