Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Feb 2004
    Location
    Malaysia
    Posts
    175
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy link list problem

    hi all,
    actually, i am not sure how or what to do in link list stuff. however, i tried inserting a record into a list which is empty. the question here is, do i need to create a node 1st before entering it into the list? or the other way round? here is my code.it's not finished but i am getting a dozen of errors which i don't know what it means. actually, at the inserting part, i hv totally no idea on what to do. can some one pls advice me or show me what to do?pls help.thank you all so much.

    Code:
                 #include <stdio.h>
    	#include <stdlib.h>
              
    	void printGradeList ();
    	void setPosition ();
    	void insertGradeRecord ();
    	void deleteGradeRecord ();
    	List *makeGradeNode (int count);    
    
    	struct GRADENODE 
    		{
    			char lastName[20];
    			float grade;
    			struct GRADENODE *nextPtr;
    		};
    			typedef struct GRADENODE gradeNode;
    
    	struct LinkedListRec
    		{
    			int count;
    			gradeNode *headPtr;
    		};
    			typedef struct LinkedListRec List;
    
    
    	void main()
    	{
    		int answer;
    
    		printf("-----------------------------------------------\n");
    		printf("|     Function         |     Key To Press     |\n");
    		printf("|----------------------|----------------------|\n");
    		printf("|                      |                      |\n");
    		printf("| 1) Insert Grade      |          1           |\n");
    		printf("|                      |                      |\n");
    		printf("| 2) Delete Grade      |          2           |\n");
    		printf("|                      |                      |\n");
    		printf("| 3) Search Grade      |          3           |\n");
    		printf("|                      |                      |\n");
    		printf("| 4) List Grade        |          4           |\n");
    		printf("|                      |                      |\n");
    		printf("| 5) Exit              |          5           |\n");
    		printf("|                      |                      |\n");
    		printf("|----------------------|----------------------|\n");
    		printf("   Enter Choice                   ");
    		scanf("%d",&answer);
    
    		switch (answer)
    			{
    				case 1 : insertGradeRecord ();
    						 break;
    				case 2 : deleteGradeRecord ();
    						 break;
    				case 3 : setPosition ();
    						 break;
    				case 4 : printGradeList ();
    						 break;
    				default : exit (1);
    			}			
    		
    	}
    
    	List *makeGradeNode (int count)
    	{
    		List *newPtr = List* malloc(sizeof(List));
    
    		if (newPtr == NULL)
    			{
    				printf("Out of memory");
    			}
    		else 
    			{
    				newPtr->count = count;
    				newPtr->nextPtr = NULL;
    			}
    
    		return newPtr;
    	}
    Warm Regards,
    Mivec

  • #2
    New Coder
    Join Date
    Feb 2005
    Location
    UK
    Posts
    90
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I'm not sure exactly what your code is supposed to do, but here is an example of a linked list:
    Code:
    struct element{
       //data
       element *next_element;
    };
    
    //then to create list:
    element my_elements;
    my_elements.next_element=NULL;  //important so you know it is the last element of the list
    
    //a function to add an element to list:
    
    element *add_element(element *current_element){
      for(int i=0; current_element!=NULL; i++){  //go through list until "NULL" is reached
          current_element=current_element->next_element;  //set the current element as the next in list
      }
      current_element=(element *)malloc(sizeof(element));   //then malloc a new element
      current_element->next_element=NULL;
      return current_element;
    }
    
    //you would call it like this:
    element *last_in_list=add_element(&my_elements);   //returns pointer to newly added element
    
    //use a similar "for" loop to access elements
    //also use one to free the list!
    I haven't tested it, but I hope it helps someone... (if not you!)
    Last edited by _Dan; 02-25-2005 at 09:20 PM.
    Daniel
    XZIST.org

  • #3
    Regular Coder
    Join Date
    Oct 2004
    Posts
    230
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Start at the first error you compiler gives you, as fixing one error will usually fix any number of errors after it.

    List *makeGradeNode (int count); <--- error on this line

    Why? because the compiler doesn't know what "List" is yet. Define your structs before you make a reference to them, and this error will go away.

    A linked list is usually a struct containing a *next pointer. Having a struct which contains a *pointer to another struct which contains a *next pointer is only going to confuse you at this point, so start off simple.

    Also, function main() should be of return type int, not void.
    int main()
    {
    return 0;
    }

  • #4
    Regular Coder
    Join Date
    Feb 2004
    Location
    Malaysia
    Posts
    175
    Thanks
    0
    Thanked 0 Times in 0 Posts
    A linked list is usually a struct containing a *next pointer. Having a struct which contains a *pointer to another struct which contains a *next pointer is only going to confuse you at this point, so start off simple.
    thanks guys but the problem here is that i MUST use these structures as stated below:

    Code:
    struct GRADENODE 
    		{
    			char lastName[20];
    			float grade;
    			struct GRADENODE *nextPtr;
    		};
    			typedef struct GRADENODE gradeNode;
    
    	struct LinkedListRec
    		{
    			int count;
    			gradeNode *headPtr;
    		};
    			typedef struct LinkedListRec List;
    and also these functions:

    Code:
    void printGradeList ();
    	void setPosition ();
    	void insertGradeRecord (List *listPtr, char lastName[], float grade, int position);
    	void deleteGradeRecord ();
    	List *makeGradeNode (int count);
    i know they are declared wrongly as i do not know what to pass down and what not to pass down and also how to declare them(i mean the parameters) and the function type....pls advice....thank you in advance.
    Warm Regards,
    Mivec

  • #5
    Regular Coder
    Join Date
    Oct 2004
    Posts
    230
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok then fix the compiler errors and warnings one at a time untill there are no more. Add blank function definitions to match all your prototypes. You can start filling in the parameters and the function bodies as you need them.

    You can ask specific questions here as you need, but remember noone is going to do your homework for you.

    You should work on one function at a time untill it works correctly, then move on to the next function. For example, your makeGradeNode() function should probably use a global or a static List object, and to make a new node you should be allocating memory for a List.headptr->nextPtr instead of making a new List object. You only need one List object, which has a pointer to the linked list of gradeNode objects.

    To get you started with the first node, you try this:
    Code:
        List myList;
        myList.headPtr = (gradeNode*)malloc(sizeof(gradeNode));
        if( myList.headPtr != NULL )
        {
            myList.count = 1;
            myList.headPtr->nextPtr = NULL;
        }
    And to access the nodes, you can try something like this:
    Code:
        gradeNode* node = myList.headPtr;
        if( node != NULL )
        {
            node->grade = some_grade;
            etc..
        }

  • #6
    Regular Coder
    Join Date
    Feb 2004
    Location
    Malaysia
    Posts
    175
    Thanks
    0
    Thanked 0 Times in 0 Posts
    of coz aman, i wasn't asking for anyone to do my homework. i was just asking what that is needed to be passed down and how to delare them? and how to use them from there? coz i am not so familiar with the pointers and structs. that was my objective of the question. so dun get me wrong here.
    well, i'll hv been trying but i do not know what is needed. like the makeGradeNode function. is it suppose to be done before inserting a new node?or the other way round? and if either way, what is need to be passed in the parameters? and what type is to be declared? thats the part i really dun knw.and thats all that i want to know. so...i hope am clarified here... .
    thanks in advance again
    Warm Regards,
    Mivec

  • #7
    Regular Coder
    Join Date
    Oct 2004
    Posts
    230
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well if I tell you what is needed to be passed down and how to delare them and how to use them from there.. I'd be doing your homework for you

    Try using makeGradeNode as part of inserting a new node, because you need to allocate memory for your new node before you can store data in it.

    insert_new_mode( last_name, grade )
    {
    makeGradeNode()

    node->grade= grade
    etc..
    }

    makeGradeNode()
    {
    malloc_memory_for_node
    }

    _Dan posted an example of adding a node, see if you can understand it.



    BTW I think that this is too advanced an assignment for not being familiar with pointers and structs, because this type of program depends heavily on both. You really need to understand those, along with basic tasks such as declaring parameters, before you will be able to easily accomplish this. That said, give it your best shot and check google for tons of examples on linked lists.
    Last edited by aman; 02-26-2005 at 09:37 AM.

  • #8
    Regular Coder
    Join Date
    Feb 2004
    Location
    Malaysia
    Posts
    175
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks, i think i hv an idea what to do dee.....but still somewhere in the blur. but anyway, i think i hv and idea what to do.thanks.....will post more if i hv anymore probs....thanks again...
    Warm Regards,
    Mivec


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •