Solved

SIMLPE linked list problem

Posted on 2000-04-22
7
226 Views
Last Modified: 2010-04-02
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-
 
0
Comment
Question by:pure032398
7 Comments
 
LVL 1

Expert Comment

by:ntdragon
Comment Utility
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
0
 
LVL 1

Author Comment

by:pure032398
Comment Utility
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

by:coredumped071199
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 1

Author Comment

by:pure032398
Comment Utility
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

by:
pjknibbs earned 50 total points
Comment Utility
student_list_head = &student_list;

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:

struct struct_student_list *head, *walker, *new_entry;   // head is the head of the list.

head = NULL;

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

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

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

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

by:pjknibbs
Comment Utility
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

by:pure032398
Comment Utility
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;
      }
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
Video by: Grant
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.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now