Thread: ordered linked list

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Don't cast malloc.

    Other than that, why don't you just run the program to see if you get expected results ?
    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

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    I get about 30 error messages. I know that there is a differnt way to wrote this linked like. This way i only get the error that NExt is not defined any ideas
    Code:
     #include <assert.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define Nlen    26
    
    typedef struct person_t {
       char FName[Nlen];
       char LName[Nlen];
       char Sex;
       char Grade;
    } PERSON;
    
    void amend_file( char file[], PERSON *STU );
    void show_file( char file[], PERSON *STU );
    
    //Funstion to Ordered linked list
    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;
    if ( Here->LName > Node->LName ) {/* Age less than first node’s age insert node at top of list */
    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;
    }
    
    int main (void)
    {
       PERSON *STU;
       int Ans;
       struct listNode  *start = NULL;         
    	struct listNode  *last = NULL;    
    	struct listNode  *data = NULL;
    	struct listNode  *tempnode  = NULL;  
          char file[] = "data.txt";
        int Records [100];
        
        
      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 = (PERSON*) malloc (sizeof (PERSON));
             if (STU == NULL)
             {
                perror("PERSON");
                exit(EXIT_FAILURE);
             }
             switch(Ans)
             {
                case 1:
                   amend_file(file, STU);
                  break;
                case 2:
                   show_file(file, STU);
                   break;
                case 3: default:
                   Ans = 3;
                   break;
             }
          } /* /get answer */
       }
        while ( Ans != 3 );
        
          fflush(stdout);
       free(STU);
       return EXIT_SUCCESS;
    }
    /*
    * amend_file: Enter stuff into the working file.
    * Very much a bare bones function so far; work on making this better
    * Perhaps make it work with a linked list, as your struct suggests?
    */
    void amend_file( char file[], PERSON *HEAD )
    {
       assert(HEAD != NULL);
       FILE *fin = fopen(file, "a");
       if ( fin == NULL )
       {
          perror(file);
          exit(EXIT_FAILURE);
       }
       printf("Enter new record: ");
       printf ("\nEnter students First name ( up to %d letters):", Nlen);
       printf ("\nEnter students  Last name  ( up to %d letters):", Nlen);
       printf ("\nPlease enter Sex");
       printf ("\nPlease enter Grade\n");
       
       scanf("%s %s %c %c", HEAD->FName, HEAD->LName, &HEAD->Sex,
          &HEAD->Grade);
          
    fprintf(fin, "%s %s %c %c\n", HEAD->LName, HEAD->FName, HEAD->Sex, HEAD->Grade);
         fflush(fin);
       fclose(fin);
    }
    /*
    * show_file: Reads the working file.
    * Bare bones again...
    */
    void show_file ( char file[], PERSON *HEAD )
    {
      assert(HEAD != NULL);
      int b = 1;
      FILE *fout = fopen(file, "r");
        
      if (file == NULL)
      {
        perror(file);
        exit(EXIT_FAILURE);
      }
       while (fscanf(fout, "%s %s %c %c", HEAD->FName, HEAD->LName, &HEAD->Sex,
         &HEAD->Grade) == 4)
      {
        printf("Record %d: %s %s %c %c\n", b, HEAD->FName, HEAD->LName,
           HEAD->Sex, HEAD->Grade);
        b++;
      }
      fclose(fout);
    }

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