Thread: I have a question about linked list

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    6

    I have a question about linked list

    Hello,
    I need to write a code in C that make linked list
    my professor gave us a three struct
    one of them is :
    Code:
    typedef struct classNode
    {
    struct classNode *next;
    struct genusNode *down;
    char name[NAME_LENGTH];
    double diversity;
    }classNode;
    I start to write the program and i dont know how to write a function that make the linked list of this struct
    1) i dont know what i need to send to the function
    2) the user need to enter the name and the diversity in the function and after that i dont know what to do with those Parameter (name and diversity) !!!

    if someone can help me how to write this function i thank him a lot

    P.S

    struct classNode *next pointer to the next struct classNode
    struct genusNode *down; pointer to struct genusNode

    bye,

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I understand the data structure as I have used a data structure with next and down pointers myself before. However I don't understand how you want to build this 'tree' (it's not really just a 'list'). Where does that data come from and how do you need to insert it?

    Where is the code you're written so far?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    6
    this is my code so far

    Code:
    // Function that make a linked list to the classNode genus !!!
    classNode *Insert_or_Update_Class(classNode *next,classNode *down,classNode *temp,classNode *head){
    	// Enter name of class and number diversity and check if its not wrong input!!!
    	do{
    		  printf ("Enter class name:\n");
    		  gets (name);
    		  printf ("Enter diversity:\n");
    		  scanf ("%lf",&diversity);
    		  gets (check);
    // here i send the input to a function that check if the input is good !!!
    		  input_check=check_input_class(diversity,name,check);
    	  }while (!input_check);
    	
    	// start to create or update the class !!!
    		temp=(classNode*)malloc(sizeof(classNode));
    		if (!temp)
    			return NULL;
    		temp->next=head;
    		head=temp;
    		strcpy(temp->name,name);
    		temp->diversity=diversity;
    		temp->down=NULL;
    		temp->next=NULL;
    		return head;
    	
    }//End create_or_update_class//

    i dont know if i send the right parameter to the functiovn

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Don't use gets - it's dangerous. Use fgets instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    6
    my problem is not the gets
    my problem is that i dont know if what i send to the function is right (is run good but when i want to create another struct the first is not exist so what i'm doing wrong ????)

    bye

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Tzahi View Post
    my problem is not the gets
    Typically, we teach and correct safe and portable code and gets isn't safe. So you should change your application to use fgets instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Just as an fyi, to change what a pointer points to, you can't pass in the pointer itself. You have to pass a pointer to the pointer. I haven't looked thoroughly through your code, but you are altering temp in your function. You also seem to move it around so head points to it as well, and a few more shell iteration. This won't change the value of what the pointers point to in the original calling function.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Tzahi View Post
    this is my code so far

    Code:
    // Function that make a linked list to the classNode genus !!!
    classNode *Insert_or_Update_Class(classNode *next,classNode *down,classNode *temp,classNode *head)
    i dont know if i send the right parameter to the function
    You're suspicions are correct. You're sending too many parameters to that function. You'll notice that you're not using next or down. I also noticed that you're using a global called 'name' that should probably be a local, as should temp I suspect. You know how to declare locals right? Just like a global, but inside the function.
    The only things left that we need to know are, how is this function called, and when do you want the node to be inserted in the down list instead of the next list?
    Last edited by iMalc; 12-30-2007 at 01:56 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Elysia View Post
    Typically, we teach and correct safe and portable code and gets isn't safe. So you should change your application to use fgets instead.
    It's not important for the OP to change to fgets at the moment, he or she can read up about it in their own time afterwards. You make it seem as though you wont help until the OP does exactly what you want with the code first. Typically we actually try and help the poster, though we may throw in a few safety or performance tips along the way.
    I'm sure you'd produce a similiar response if you posted some code only for someone to immediately post comments we know you weren't after. E.g. 'Foo' isn't a good name for a class.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Indeed. Well, if it sounds that I'm implying that I should demand it changes before helping further, then let me add that it is not so.
    The code is a mystery to me, though, so I'm not helping very much. I've been staying away from the subject since I don't have a good enough understanding to suggest corrections.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Yes, this is a tricky one to help with, I'll admit. I've realised that there are still many more unanswered questions. For example, do you want to insert at the front of the list, at the end, or in sorted order most often somewhere in the middle?
    Same question for the down list.
    Next, is it valid for head->down->next to be non-null? In other words is this really like a tree, or is it more like a list of lists.

    We really can't tell what you want to do. Perhaps the problems comes from you not knowing what you need to do either?! Lots more information required please!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #12
    Registered User
    Join Date
    Nov 2007
    Posts
    73
    first of all. which type of a linked list are you trying to write a code for???
    >singly linked
    >doubly linked
    ??
    if it is a singly linked... then
    where are insertions done and where are deletions>?
    is it a lifo or fifo???????
    if you are trying to do a lifo
    one pointer at the head may be sufficient...

    if u are doing a fifo you might need a front and rear pointer....

    can you explain a bit in detail....

    and why do you want a insert function pointer ..
    you can as well declare it as

    Code:
    void insert(void)
    {
    classnode *nu;
    //take inputs here
    nu->nxt=head;
    head=nu;
    }
    this would suffice....
    just declare head as global and initialize it to NULL
    Last edited by ElemenT.usha; 12-31-2007 at 03:19 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  2. linked list question
    By brb9412 in forum C Programming
    Replies: 16
    Last Post: 01-04-2009, 04:05 PM
  3. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  4. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM