Solved

SIMLPE linked list problem

Posted on 2000-04-22
7
227 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
ID: 2740637
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
ID: 2741047
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
ID: 2741122
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
Will my email signature work in Office 365?

You've built an email signature using raw HTML code in Office 365, but you can't review how it looks with Transport Rules. So you have to test it over and over again before it can be used. Isn't this a bit of a waste of your time? Wouldn't a WYSIWYG editor make it a lot easier?

 
LVL 1

Author Comment

by:pure032398
ID: 2741124
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
ID: 2741297
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
ID: 2741299
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
ID: 2741332
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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

867 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

21 Experts available now in Live!

Get 1:1 Help Now