0

I looked through other questions but none of them answered my question. I'm trying to set up a singly linked list that adds from the tail for a class assignment.

/** List node  **/
typedef struct node_type
{
   struct node_type *next;           /* Pointer to next node               */   
   void *objPtr;                     /* Pointer to associated object       */
} node_t;

/** List structure **/
typedef struct list_type
{
   node_t *head;                     /* Pointer to front of list           */
   node_t *tail;                     /* Pointer to end of list             */
} list_t;

list_t *newList() {
   list_t *list;
   list = (list_t *)malloc(sizeof(list_t));

   list -> head = NULL;
   list -> tail = NULL;
   return(list);
}

These were the structures given to me to use. My function for adding is like this:

/** l_add -- add an object to the linked list **/
void l_add(list_t *list, void *objPtr) {
   node_t *newNode;
   newNode = (node_t *)malloc(sizeof(node_t));

   newNode -> objPtr = objPtr;
   newNode -> next = NULL;

   if ((list -> head == NULL) && (list -> tail == NULL)) {
   list -> head = newNode;
   list -> tail = newNode;
   } else {
   list -> tail -> next = newNode;
   list -> tail = newNode;

}
}

and my iterator related functions are like this:

iterator_t *newIterator(list_t *list) {

   iterator_t *itr;
   itr = (iterator_t *)malloc(sizeof(iterator_t));

   itr -> list = list;                    
   itr -> position = list -> head;

   return(itr); 
}

void *l_next(iterator_t *iter) {
  void *object;

   if ((iter -> position) == NULL) {
      return NULL;
      exit(0);
   } else {
      object = iter -> position -> next;
      iter -> position = iter -> position -> next;
}
  return  object; 

}

void l_begin(iterator_t *iter) {
    iter -> position = iter -> list -> head;
}

It doesn't give me any errors when I compile it, I'm getting large values that I'm assuming are memory instead of the data I'm supposed to be recieving. The first value that I pass it is "5" and the number that's first in the list fluctuates but is in the millions. I'm still trying to get used to pointers, does anyone know why I'm adding the address and not the value?

Code where I use it:

   size = sizeof(data)/sizeof(int);
   for(index = 0; index < size; index++) {
      l_add(list1, &data[index]);
   }

   l_begin(iter1);
   ptr1 = l_next(iter1);
   fprintf(stdout, "First value=%d\n", *ptr1);

And list1 is:

int data[] = {5, 10, 15, 20, 25};
list_t *list1;
list1 = newList();

*Updated to include printing functions and my iterator. I didn't think the problem was there, but it might be. Sorry for such a long question, I tried to edit it so there's not so much code, so hopefully that all makes sense and I didn't mess up what I was trying to show.

All of you guys got it at about the same time, thanks for the help!

2

3 Answers 3

0

It would appear that you are not dereferencing objPtr. That would be why you are getting large values.

I assume l_next is supposed to return a node_type. In that case you would need:

fprintf(stdout, "First value=%d\n", *((int*)ptr1->objPtr));

To not change the fprintf change the object = in l_next to:

object = iter -> position -> next -> objPtr;

Also, you probably should know that l_next will not actually iterate. It doesn't change the value of position in iter.

2
  • When I change that, it says "error: request for member ‘objPtr’ in something not a structure or union." We're also not supposed to change the printing statements either so I think I have to change something in one of the functions I wrote, which would be the second and third big blocks. Thanks for the heads up about l_next though, fixed that. Sep 28, 2012 at 17:38
  • @Jaghancement I was assuming you were supposed to return a node_type pointer and change the print as necessary. If you're not supposed to change the prints then you need to return iter -> position -> next -> objData instead of iter -> position -> next. (Assuming you want to return the next value instead of the current value).
    – CrazyCasta
    Sep 28, 2012 at 17:45
0

Your l_next is broken; you're returning the next node instead of the object. It should be:

void *l_next(iterator_t *iter) {
  void *object;

   if ((iter -> position) == NULL) {
      return NULL;
      exit(0);
   } else {
      object = iter -> position -> objPtr;
      iter -> position = iter -> position -> next;
}
  return  object; 

}
0

You need to pass in a pointer to your 5 value. So instead of passing a "5" in allocate memory for an integer set that to 5 then pass the pointer for memory allocation in:

int * newObject = (int *)malloc(sizeof(int));

*newObject = 5;

l_add(list, newObject);

To retrieve it you will need to dereference the object as whatever type you instantiated the 5 value in.

Well, now that you posted the rest of your code it looks like you are grabbing the next node structure as your iterators returned object instead of the actual object here:

object = iter -> position -> next;

it should be like:

object = iter -> position -> objPtr;
1
  • Wouldn't that be the same as: void l_add(list_t *list, void *objPtr) { newNode = (node_t *)malloc(sizeof(node_t)); newNode -> objPtr = objPtr; newNode -> next = NULL; l_add(list1, &data[index]); } ? Sep 28, 2012 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.