ordered linked list

This is a discussion on ordered linked list within the C Programming forums, part of the General Programming Boards category; [code]STU= (struct person_t ) malloc(sizeof(lperson_t))[code] that means that I should change this line to the malloc line right Code: assert(STU ...

  1. #31
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    [code]STU= (struct person_t ) malloc(sizeof(lperson_t))[code] that means that I should change this line to the malloc line right

    Code:
    assert(STU != NULL);

  2. #32
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Code:
      do
       {
          printf("\n\n"
                 "1. Enter new record\n"
                 "2. Display List\n"
                 "3. Quit\n\n");
          printf("Prompt: ");
          fflush(stdout);
          if ( scanf("%d", &Ans) == 1 ) /* get answer */
          {
            fgets(STU->FName,sizeof(STU->FName),stdin);
             if (STU == NULL)
             {
                perror("PERSON");
                exit(EXIT_FAILURE);
             }
    Is there anything here tha may have unallocated memory?

  3. #33
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    Code:
     fgets(STU->FName,sizeof(STU->FName),stdin);
    is says to get it but it dose not allicatit. so i should change it to
    Code:
     fgets(STU->FName),malloc(sizeof(STU->FName),stdin);
    so that it gets it and allicated it as well

  4. #34
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    What??????

    You seriously need to RTM on malloc().

  5. #35
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    Actually I'm starting to wonder if redmontab has ever studied a sorting algorithm before. Or if he's had any prior experience with C at all.

    http://en.wikipedia.org/wiki/Merge_sort

  6. #36
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    im very new at this. And it is a little hard to understand on my own. THe book that i have dose not help me out to much. Some times it makes it worse.

  7. #37
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    You want to malloc an entire structure, so something like

    Code:
    malloc(sizeof(PERSON));
    to allocate a block the size of one structure.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  8. #38
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    so I wnat the fgets to read the who ling. THe way I have it [code] fgets(STU->FName,sizeof(STU->FName),stdin);[code] I;m only reading the first name and not my whol structure. SO i need to change it to

    Code:
     fgets(PERSON),malloc (sizeof(Person));
    this is because i thave my Structure person_T defined to call as PERSON. Is this correct. Soory if im not geeting this. I appreciate all of you help and patience with me

  9. #39
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Also don't forget that malloc() can FAIL. So check the returned pointer BEFORE you use it!

    Also look at calloc() if you want to allocate a block of 'n' structures. (And unlike malloc it will initialize the memory to zeroes too).

  10. #40
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    ::sick of this::
    Since you don't want to _LEARN_. . .
    Code:
    STU = malloc(sizeof *STU);
    if (STU == NULL){
        printf("Out of memeory");
        exit(0);
    }
    Insert this before your first fgets() as mentioned above.

  11. #41
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    know i get the error 87 H:\lab4a.cpp invalid conversion from `void*' to `PERSON*'
    Code:
     STU = malloc(sizeof *STU);

  12. #42
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    It's actually :

    Code:
    STU = malloc(sizeof(PERSON));  /* or whatever you called your structure */
    if (STU == NULL){
        printf("Out of memeory");
        exit(0);
    }
    Also, to get rid of that error, you want STU to be a void*. Then, when you use it, you can cast it to a (PERSON*) if you wish.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  13. #43
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by redmondtab
    know i get the error 87 H:\lab4a.cpp invalid conversion from `void*' to `PERSON*'
    Code:
     STU = malloc(sizeof *STU);
    Stop compiling as C++.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #44
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    how do i do that. Im using the dev-c ++ complier

  15. #45
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    Im getting this error can someone help me please
    117 H:\lab4a.cpp invalid conversion from `void*' to `PERSON*' the orange line is where im getting the error
    Code:
    /****************************************************************
    FILE NAME: lab4.cpp
    DESCRIPTION: This program will ask the user if the want to read a file or input 
                 data.  Check the file to see if there is data if that is the first
                 request. IF not data go back to the options.  Have user add data. 
                 Send that data to an ordered linked list then save the data to a 
                 file.  Also have the options to show data from file or quit.  .
    DATE: October 22, 2006
    DESIGNER: Tabatha Mitchell
    ASSIGNMENT: Lab4
    FUNCTIONS: PERSON* InsertNode (PERSON *Top, PERSON *Node )
               amend_file
               show_file
    ********************************************************************/
    #include <assert.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <malloc.h>
    
    
    #define Nlen    26
    
    typedef struct person_t { //structure that will sort that data for PERSONS
       char FName[Nlen];
       char LName[Nlen];
       char Sex;
       char Grade;
       struct person_t *Next;
      } PERSON;
    
    struct person_t  *Top = NULL;    //used for the InsertNode set to empty       
    struct person_t  *Node = NULL;   //used for the InsertNode set to empty
    
    
    /****************************************************************
    FUNCTION: PERSON* InsertNode
    DESCRIPTION: put the sturctured list into the ordered linked list
    INPUT:
       Parameters:  The parameters as going to be the last name in the structured 
                    list it is going to take the last name and sort it into an 
                    ordered linked list   
       Keyboard:  none 
        File: none
    OUTPUT:
       Return Value: last name in an ordered linked list 
       Parameters: last name ordered linked list 
       Display: none 
       File: none 
    CALLS TO: none 
    ***************************************************************/
    
    PERSON* InsertNode (PERSON *Top, PERSON *Node )
    {
    PERSON *Here, /* Node being compared to new node */  
    *Prev; /* Node in front of Here node */
    
        if (Top == NULL)/* List is empty - Place new node at top of list */
          Top = Node;
             else { /* Top != NULL *//* Start at Top of list */
              Here = Top;
              
        strcmp (Here->LName , Node->LName);             
         Node->Next = Top;
         Top = Node;
        
            //  else {/* Search for place to insert new node */
              //  Prev = Top;
              //  Here = Prev->Next;
     
       while ( (Here != NULL) &&(Here->LName < Node->LName) ) {
           Prev = Here;
           Here = Here->Next;
            }/* Insert into list at found spot */
            
    Node->Next = Prev->Next;
    Prev->Next = Node;
    } /* end of Top != NULL */
    return Top;
    }
    /*************************************************************
    end of  Function to Ordered linked list
    ***************************************************************/
    
    void amend_file( char file[],PERSON *STU );//used to define amend_file function
    void show_file( char file[],PERSON *STU ); //used to define  show_file function 
    
    
    int main (void)
    {
      PERSON *STU;
       int Ans;
       char file[] = "data.txt";
        int Records [100];
         FILE *fin = fopen(file, "a+");
         struct person_t  *Top = NULL;    
    	struct person_t  *Node = NULL;
    	
        
        
      do
       {
          printf("\n\n"
                 "1. Enter new record\n"
                 "2. Display List\n"
                 "3. Quit\n\n");
          printf("Prompt: ");
          fflush(stdout);
         
         
         
        
           
             
          if ( scanf("%d", &Ans) == 1 ) /* get answer */
         {
            STU = malloc (sizeof (person_t));   //allocate  memory space for the STU
            if (STU == NULL){  //if not enough space tell us out of memory 
           printf("Out of memory");
            exit(0);
                }
            fgets(STU->FName,sizeof(STU->FName),stdin); 
             if (STU == NULL)
             {
                perror("PERSON");
                exit(EXIT_FAILURE);
             }
             switch(Ans)
             {
                case 1:
                   amend_file(file, STU);  //calls the amend file function 
                  break;
               
                case 2:
                   show_file(file, STU);  //calls the show file function
                   break;
               
                case 3: default:
                   Ans = 3;
                   break;
             }
          } /* /get answer */
       }
        
        
         while ( Ans != 3 );
        
          fflush(stdout);
          free(STU);
          getchar ();
       return EXIT_SUCCESS;
    }
    
    /****************************************************************
    FUNCTION:  amend_file
    DESCRIPTION: this will get the input for the use, send it to the linked list 
                 and then save it to the file. 
    INPUT:
       Parameters: Person structure. and  Person Insertnode
                  
       Keyboard: all of the following will be saved in the structure PERSON
                 LName= last name
                 FName = first name
                 Sex=  what gender the person is
                 Grade = The persons grade
        File: data.txt  this is where the data is saved. 
    OUTPUT:
       Return Value:  Send The PERSONS STU to the InsertNode put data into list the 
                    save to file
       Parameters: InsertNode, LName, FName, Sex, Grade 
       Display: none
       File: data.txt. the PERSONS STU in ordered link list format Lname, FName,
              Sex, Grade 
    CALLS TO:  List of all user-generated functions called
    ***************************************************************/
    void amend_file( char file[], PERSON *STU )
    {
         int i;
       assert(STU != NULL);
        STU = (struct person_t *) malloc(sizeof(person_t));
       FILE *fin = fopen(file, "a+");
       if ( fin == NULL )
       {
          perror(file);
          exit(EXIT_FAILURE);
       }
       printf("Enter new record: ");
       printf ("\nEnter student's First name ( up to %d letters):", Nlen);
       printf ("\nEnter student's  Last name  ( up to %d letters):", Nlen);
       printf ("\nPlease enter Sex");
       printf ("\nPlease enter Grade\n");
       scanf("%s %s %c %c", STU->FName, STU->LName, &STU->Sex,
          &STU->Grade);
          
        InsertNode (Top, Node);
        fprintf(fin, "%s %s %c %c\n", STU->LName, STU->FName, STU->Sex, STU->Grade);
               fflush(fin);
                fclose(fin); 
          }
    
    
    /****************************************************************
    FUNCTION:  show_file
    DESCRIPTION: Reads the data from the file and displays data after sorting if 
                needed.
    INPUT:
       Parameters: Person structure. and  Person Insertnode these are read from the 
                  file and then  put into the list then displayed. 
       Keyboard: 
       File:  data.txt  this is where the data is saved. 
    OUTPUT:
       Return Value:  Send The PERSONS STU to the InsertNode put data into list the
                     save to file
       Parameters: InsertNode, LName, FName, Sex, Grade 
       Display: the order list of people that are saved to the file.  
       File: data.txt. the PERSONS STU in ordered link list format Lname, FName,
             Sex, Grade 
    CALLS TO:
    ***************************************************************/
    void show_file ( char file[], PERSON *STU )
    {
      assert(STU != NULL);
      int b = 1;
      FILE *fin = fopen(file, "rb");
        
      if (file == NULL)
      {
        perror(file);
        exit(EXIT_FAILURE);
      }
      while (fscanf(fin, "%s %s %c %c", STU->FName, STU->LName, &STU->Sex,
         &STU->Grade)== 4)
      {                   
        
          InsertNode (Top, Node);
          b++;          
        printf("Record %d: %s %s %c %c\n", b, STU->FName, STU->LName,
           STU->Sex, STU->Grade);
      }
      fclose(fin);
    }

Page 3 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 11:03 AM
  2. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 02:48 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21