Thread: ordered linked list

  1. #46
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Code:
     
      STU = (PERSON *) malloc (sizeof (person_t));   //allocate  memory space for the STU

  2. #47
    Registered User
    Join Date
    Sep 2006
    Posts
    104

    Smile

    what am i doing worng. I cant get the file to go into the insert linnked list any ideas
    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 ()
    {
      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);
         
         
         
        
           STU = (PERSON *) malloc (sizeof (person_t));
             
          if ( scanf("%d", &Ans) == 1 ) /* get answer */
         {
             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);
    }

  3. #48
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Stop compiling as C++! .cpp is short for "C Plus Plus". Stop typecasting your allocations. Start writing small functions and testing them one at a time. There's no way I'm going to read and debug that entire thing. Anyone who does is far more bored than I.


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

  4. #49
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Quote Originally Posted by redmondtab
    how do i do that. Im using the dev-c ++ complier
    For you, the simplest way would be to create a new project but this time make sure you select the 'c project' radio button in the 'new project' dialog box. For good measure you could always pop open the 'project options' dialog from the 'project' menu, select the 'parameters' tab and put either -std=c89 or -std=c99 in the 'c compiler' field, depending on which standard you wish to conform to. Then change those file extensions from 'cpp' to 'c' as quzah has repeatedly and helpfully advised you, before adding them to this new 'c' project.

    Most compilers will regard source files with a 'cpp' extension as c++ code and will compile them accordingly. Similarly, most compilers will recognise source files with a 'c' extension as c code and will compile them accordingly. Despite similarities, C and C++ are two distinct and different languages; by persistently building your 'c' program as 'c++' as you are currently doing, all you're succeeding in accomplishing is learning how to write an arguably functional mishmash while exasperating those best able to help you.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

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, 10: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, 10: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, 10:21 PM