Link to home
Start Free TrialLog in
Avatar of pure032398
pure032398

asked on

SIMLPE linked list problem

Hello.

I have a linked list, but i cannot seem to keep track of the head of the linked list, so i can print out the contents of it...

This is what i have...

  struct struct_student_list {
    int studentID;
    int subjectID;
    struct student_list *next;
  };

  struct struct_student_list *student_list, *student_list_head;


student_list = NULL;
student_list_head = &student_list;

for (i = 0; i < 10; i++) {
    while (student_list != NULL) {
        student_list = student_list->next;
     }

     // Add the data.
     CREATE (spell_list, struct aff_spell_list, 1);   // a function given to us by teacher, but we do NOT have to use it at all. (says do not if u are ok with coding, which i;m not)
     student_list->studentID = i;
     student_list->subjectID = i*i;
     student_list->next = NULL;
}

// Now recurse through the Linked List,
which was just made, and display contents....


AND Question 2)
what if i'm trying to add a number into the linked list, but the number is already in tehre somwhere. EG. studentID 10 was added a minute ago, and now i'm trying to add it again, but i do not want of the same number in the list... HOW?

THANKS IN ADVANCE for any help  =)

-PK-
 
Avatar of ntdragon
ntdragon

first it should be something like that:



  struct struct_student_list {
    int studentID;
    int subjectID;
    struct student_list *next;
  };

  struct struct_student_list *student_list, *student_list_head;


student_list = NULL;
//it's a  pointer like student_list
//what do you mean by &student_list
//it have no menaing
student_list_head = NULL;

for (i = 0; i < 10; i++) {
    while (student_list != NULL) {
        student_list = student_list->next;
     }

     // Add the data.
//i won't use the func of your teacher
//     CREATE (spell_list, struct
//aff_spell_list, 1);   // a function
//given to us by teacher, but we do NOT
//have to use it at all. (says do not
//if u are ok with coding, which i;m
//not)
   student_list=(struct_student_list*)
   malloc(sizeof(struct_student_list );
   //or student_list =new  
   // struct_student_list;
   if (student_list_heah==NULL)
     student_list_head=student_list;
   student_list->studentID = i;
   student_list->subjectID = i*i;
   student_list->next = NULL;
}

about the second i didn't understood
it exectlly

if you have questoins ask i hope i didn't make any bugs at writing
Avatar of pure032398

ASKER

Yep. works except for the fact that student_list->next doesn't link to the new/next student_list...

(i also used CREATE, not malloc becuase your line above doesn't work).

-PK-
Regarding you second question...

The easiest way is to traverse the linked list recursively and check each time to see whether the value of the 'current' node is equal to the value you want to insert into the list. If it is, then kick out of the recursive function so that it doesn't continue and eventually insert the value anyway.

some code examples might be sweet also =)

eg. creating / inserting nodes into a new linked list, where nodes data has to be unique.

-PK-
ASKER CERTIFIED SOLUTION
Avatar of pjknibbs
pjknibbs

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry, had a brain fart in the above code: the while loop should be:

while (walker && walker->next)
    walker = walker->next;

Otherwise walker will *always* be NULL in the comparison on the next line...
yeah. i ended up doing it this way, but waited until i could see someone who did it.

this way, i don't really need a head, becuase i'm keeping track already.

but i'll give u the points, becuase you came the closest.

and i can do the remove also (did it in another function btw) =)

THANKS HEAPS FOR THE ANSWER.

-PK-


student_found = FALSE;
      for (student_new = student_list; student_new; student_new = student_new->next) {
        if (student_new->student_number == aff->type)
          student_found = TRUE;
      }

      /* Add the student, if we have not found it */
      if (student_found == FALSE) {
        CREATE (student_new, struct aff_student_list, 1);
        student_new->student_number = aff->type;
        student_new->duration = aff->duration;
        student_new->next = NULL;
        student_new->next = student_list;
        student_list = student_new;
      }