Solved

Posted on 2000-04-22
226 Views
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;
};

student_list = NULL;

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

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-

0
Question by:pure032398

LVL 1

Expert Comment

first it should be something like that:

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

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

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

//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->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
0

LVL 1

Author Comment

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-
0

Expert Comment

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.

0

LVL 1

Author Comment

some code examples might be sweet also =)

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

-PK-
0

LVL 12

Accepted Solution

pjknibbs earned 50 total points

You're assigning a pointer to a pointer to something that's just a pointer, and I'm amazed your compiler didn't complain about it--try hiking the warning level some and let the compiler catch problems like this!

CREATE (spell_list, struct aff_spell_list, 1);
student_list->studentID = i;

I fail to see how the CREATE line assigns *anything* to student_list, so what you're almost certainly doing here is assigning a value to a structure at address 0 in memory. Under any memory-protected OS this would cause your program to crash in very short order.

To add an item to a linked list you need to use code like this:

// To add an item to the list, do this:
new_entry = (struct struct_student_list *)malloc(sizeof(struct struct_student_list));

// Fill some stuff into new_entry here.
new_entry->next = NULL;

while (walker)
walker = walker->next;

if (walker)
walker->next = new_entry;
else

I'll leave *removing* the item again as an exercise for the reader. <G>

As for not adding the same item twice, the only way to do it is to scan through the entire list and see if the entry is already there before adding it.
0

LVL 12

Expert Comment

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...
0

LVL 1

Author Comment

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) =)

-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;
}

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;
}
0

## Featured Post

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.