Solved

SIMLPE linked list problem

Posted on 2000-04-22
7
229 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
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

820 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