ordered linked list

This is a discussion on ordered linked list within the C Programming forums, part of the General Programming Boards category; what to know if im on the right track for this ordered linked list ias i get each record i ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    104

    Question ordered linked list

    what to know if im on the right track for this ordered linked list ias i get each record i want to insert it into the linked list in order.

    Code:
    #include <assert.h>
    #include <stdio.h>
    #include <stdlib.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 );
    struct listNode
    {
        char data;
        struct listNode *priorPtr;
        struct listNode *nextPtr;
    
    }ListNode;
    
    struct listNode  *start = NULL;         
    struct listNode  *last = NULL;  
    
    int main (void)
    {
       PERSON *STU;
       int Ans;
          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);
                   HEAD->LName = data;
                      data = (struct person_t  *) malloc(sizeof(person_t));
            if(!data)
                return -1;
            data->data =LNAME ;
            insert(data, &start, &last);
        }
        tempnode = start;
       
         fprintf(fin, "%s %s %c %c\n", HEAD->LName, HEAD->FName, HEAD->Sex, HEAD->Grade);
         fflush(fin);
       fclose(fin);
                   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);
    
    }
    /*
    * 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);
    }
    
    
    //Ordered Linked list
    //*******************************************************************  
    void insert( struct listNode   *newnode,  /* new node */
                    struct listNode   **start, /* first node */
                    struct listNode   **last /* last node */
                    )
    {
        struct listNode  *oldptr, *ptr;
        if(*last == NULL) /* first node in linklist */
        {
            newnode->nextPtr = NULL;
            newnode->priorPtr = NULL;
            *last = newnode;
            *start = newnode;
            return;
        }
        ptr = *start; /* start at top of linklist */
        oldptr = NULL;
        while(ptr)
        {
            if(ptr->data < newnode->data )
            {
                oldptr = ptr;
                ptr = ptr->nextPtr;
            }
            else
            {
                if(ptr->priorPtr)
                {
                    ptr->priorPtr->nextPtr = newnode;
                    newnode->nextPtr = ptr;
                    newnode->priorPtr = ptr->priorPtr;
                    ptr->priorPtr = newnode;
                    return;
                }
                newnode->nextPtr = ptr; /* newnode first node */
                newnode->priorPtr = NULL;
                ptr->priorPtr = newnode;
                *start = newnode;
                return;
            }
        }
        oldptr->nextPtr = newnode;
        newnode->nextPtr = NULL;
        newnode->priorPtr = oldptr;
        *last = newnode;
    }

  2. #2
    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

  3. #3
    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);
    }

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by redmondtab
    Code:
    typedef struct person_t {
       char FName[Nlen];
       char LName[Nlen];
       char Sex;
       char Grade;
    } PERSON;
    There is no "next" in your def.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    so i need to add it like this this is ho that it will pass the last name to the next node right.
    i think I hace to define it in this funcation. But when i do it as *Next. It is gives me erroe mesages
    I get error
    H:\Untitled3.cpp In function `PERSON* InsertNode(PERSON*, PERSON*)':
    27 H:\Untitled3.cpp 'struct person_t' has no member named 'Next'

    Code:
    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;
    }
    Last edited by redmondtab; 10-12-2006 at 12:34 PM.

  6. #6
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    No, you want a pointer to your struct. You have learned pointers?
    You'd need to do something like:

    int NotAPointer;
    int *AnIntPointer;

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    i need help with getting the data into the insertnode funstion. this is what i have

    Code:
     #include <assert.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define Nlen    26
    
    typedef struct person_t {
       char FName[Nlen];
       char LName[Nlen];
       char Sex;
       char Grade;
       struct person_t *Next;
      } PERSON;
    
    //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 ) {
    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;
    }
    
    void amend_file( char file[], PERSON *STU );
    void show_file( char file[], PERSON *STU );
    void PRINT_file( char file[], PERSON *STU );
    
    int main (void)
    {
       PERSON *STU;
       int Ans;
            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);
                  InsertNode;  //where i'm trying to add my insert
                  print_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);
    }
     //just added the print statement
      void print_file( char file[], PERSON *HEAD )  
      {
         assert(HEAD != NULL);
       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);
    }
    Last edited by redmondtab; 10-12-2006 at 01:11 PM.

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Well, here is some the errors which i could able to find out.

    1. Head is not decalred with in the scope if main
    2. With out placing the information on to the data varibale u are intializing it. have a look at this
    HEAD->LName = data;
    3. So again what is this
    data->data =LNAME ;
    where have u declared data in the scopr of main or at least it should globally declared which
    can be acessed by all function ( Global idea is not quite good but still i mentioned it)
    4. Tempnode not declared

    Its better u have go through how to use a simple linked list program agian.

    ssharish2005

  9. #9
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Ok, I'm not going to go through all your code there. Show me the error messages you get instead.

    However, upon glancing through your code quickly, I noticed something that you may want to correct :

    Code:
    if ( Here->LName > Node->LName )
    Here you're comparing a character array with a character array...which is likely going to give you unexpected results. What you probably want is

    strcmp()
    Last edited by Happy_Reaper; 10-12-2006 at 02:26 PM.
    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

  10. #10
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    A simple insert-in-order app:
    Code:
    #include <stdio.h>
    
    typdef struct _astruct {
    	int data;
    	struct _astruct *next;
    } ASTRUCT;
    
    void addinorder(ASTRUCT **head, ASTRUCT *node)
    {
    	ASTRUCT *tmp;
    	int cmpret, found = 1;
    
    	if (node == NULL)
    		return;
    	if ((*head) == NULL){
    		*head = node;
    		node->next = NULL;
    		return;
    	}
    
    	tmp = *head;
    	found = 0;
    	while ((tmp->next != NULL) && (!found)){
    		cmpret = CMPFUNCTION(tmp->next, node); 
    		/* CMPFUNCTION should return -1, 0, 1 for tmp < node, tmp == node, 
    		    and tmp > node */
    		if (cmpret > 0)
    			found++;
    		else
    			tmp = tmp->next;
    	}
    	node->next = tmp->next;
    	tmp->next = node;
    }
    
    int main(void){
    	ASTRUCT tmp, *tmpptr, *list = NULL;
    
    	while (/* whatever is true */){
    		// Read data to tmp.
    		tmpptr = (ASTRUCT *) malloc(sizeof(ASTRUCT));
    		/* Nothing wrong with the above statement (as you wrote yours), however, some on this forum
    		   will tell you that this is not the best way to form the statement.  Those persons will tell
    		   you to do it tmpptr = malloc(sizeof(*tmpptr)); */
    		if (tmpptr == NULL){
    			//error checking
    		}
    		memcpy(tmpptr, &tmp, sizeof(ASTRUCT));
    		addinorder(&list, tmpptr);
    	}
    	// the output and clean-up go here.
    	// A simple clean-up would be:
    	tmpptr = list;
    	while (tmpptr != NULL){
    		tmpptr = list->next;
    		free(list);
    		list = tmpptr;
    	}
    	return 0;
    }
    Please note: I didn't compile this, nor is it complete!

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    Quote Originally Posted by Happy_Reaper
    Ok, I'm not going to go through all your code there. Show me the error messages you get instead.

    However, upon glancing through your code quickly, I noticed something that you may want to correct :

    Code:
    if ( Here->LName > Node->LName )
    Here you're comparing a character array with a character array...which is likely going to give you unexpected results. What you probably want is

    strcmp()
    Im not getting errors at this point. my biggets problem is that I can't get my InsertNode to run. OR print. I have tryed to create a print funation to put after my insert node. ANd the system did not like that. when i tried to change the line to
    Code:
    if (strcmp (Here->LName > Node->LName))
    Here are the error messages.
    25 H:\Untitled3.cpp cannot convert `bool' to `const char*' for argument `1' to `int strcmp(const char*, const char*)'

    76 H:\Untitled3.cpp [Warning] statement is a reference, not call, to function `InsertNode'

  12. #12
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Did you look at the link I provided ?

    It's not a line-per-line replacement

    it's more like :

    Code:
    if(strcmp(s1,s2) < 0)
    {
            //  s1 is lexicographically smaller than s2
    }
    else
    {
           //  s1 is lexicographically greater or equal to s2
    }
    That should eliminate those bugs.
    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. #13
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    you'll have to figure out the data sorting..

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define Nlen    26
    
    // Add bool type if needed
    #ifndef BOOL
    #define BOOL    int
    #define TRUE    1
    #define FALSE   0
    #endif
    
    typedef struct _node 
    {
        char FName[Nlen];
        char LName[Nlen];
        char Sex;
        char Grade;
        struct _node * Next;
    } PERSON;
    
    typedef struct _personlist
    {
        PERSON *    pNodeHead;
    } PersonList;
    
    BOOL AddaPerson(PersonList * pList , char *fname, char *lname, char sex, char grade)
    {
        PERSON * pNodeCurrent;
        PERSON * pNodeNew;
        PERSON * pNodePrevious;
        pNodeNew = (PERSON *)malloc(sizeof(PERSON));
    
        if (pNodeNew == NULL)
            return(FALSE);
        pNodeNew->Next = NULL;
        strcpy(pNodeNew->FName,fname);
        strcpy(pNodeNew->LName, lname);
        pNodeNew->Sex = sex;
        pNodeNew->Grade= grade;
        pNodePrevious       = NULL;
        pNodeCurrent        = pList->pNodeHead;
        while (pNodeCurrent != NULL)
        {
            pNodePrevious   = pNodeCurrent;
            pNodeCurrent    = pNodeCurrent->Next;
        }
        if (pNodePrevious != NULL)
        {
            pNodeNew->Next = pNodePrevious->Next;
            pNodePrevious->Next = pNodeNew;
        }
        else
        {
            pList->pNodeHead = pNodeNew;
        }
        return(TRUE);
    }
    
    void PrintAllPersons(PersonList * pList)
    {
        PERSON * pNodeCurrent;
    
        pNodeCurrent = pList->pNodeHead;
        while (pNodeCurrent != NULL)
        {
            printf("%s %s %c %c\n", pNodeCurrent->FName, pNodeCurrent->LName, pNodeCurrent->Grade, pNodeCurrent->Sex);
            pNodeCurrent = pNodeCurrent->Next;
        }
    }
    
    void RemoveAllPersons(PersonList * pList)
    {
        PERSON *    pNodeKill;
        PERSON *    pNodeCurrent;
    
        pNodeKill    = NULL;
        pNodeCurrent = pList->pNodeHead;
        while (pNodeCurrent != NULL)
        {
            pNodeKill       = pNodeCurrent;
            pNodeCurrent    = pNodeCurrent->Next;
            free(pNodeKill);
        }
        pList->pNodeHead = NULL;
    }
    
    BOOL SaveTheFile(PersonList * pList, char *fname)
    {
        PERSON * pNodeCurrent;
    
        FILE *fp;
        fp = fopen(fname, "wb");
        if(!fp)
            return FALSE;
        pNodeCurrent = pList->pNodeHead;
        while (pNodeCurrent != NULL)
        {
            fwrite(pNodeCurrent, sizeof(PERSON), 1 ,fp);
            pNodeCurrent = pNodeCurrent->Next;
        }
        return TRUE;
    }
    
    BOOL LoadTheFile( PersonList * pList, char *fname)
    {
        BOOL bReturn = TRUE;
        FILE *fp;
        PERSON TempPersonList;
        fp = fopen(fname, "rb");
        if(!fp)
            return FALSE;
        while(!feof(fp))
        {
            if(fread(&TempPersonList, sizeof(PERSON), 1, fp) != 1)
            {
                bReturn = FALSE;
                break;
            }
            if (!AddaPerson(pList, TempPersonList.FName,TempPersonList.LName,TempPersonList.Sex,TempPersonList.Grade ))
            {
                bReturn = FALSE;
                break;
            }      
        }
        fclose(fp);
        return bReturn;
    }
    
    int main (void) 
    {
        char file[] = "data.txt";
        PersonList MyPersonList;
        MyPersonList.pNodeHead = NULL;
        if (!AddaPerson(&MyPersonList, "BOB", "Doe", 'M', '4'))
        {
            fprintf(stderr, "Error adding a node to the linked list\n");
            exit(1);
        } 
        if (!AddaPerson(&MyPersonList, "Bill", "Jones", 'M', '5'))
        {
            fprintf(stderr, "Error adding a node to the linked list\n");
            exit(1);
        }   
        if (!AddaPerson(&MyPersonList, "Ted", "Smith", 'M', '6'))
        {
            fprintf(stderr, "Error adding a node to the linked list\n");
            exit(1);
        }   
        SaveTheFile(&MyPersonList, file);
        PrintAllPersons(&MyPersonList);
        RemoveAllPersons(&MyPersonList);
        LoadTheFile( &MyPersonList, file);
        PrintAllPersons(&MyPersonList);
        return(0);
    }

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    i changed the code. I know that I'm still having an issues reading the last name into the lineked list. Do i need to do a funcation that inserts it or do the ordered funcation i have do that for me. And if this is the case. Can someone please tell me what I'm missing. Im so lost.
    Code:
      #include <assert.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define Nlen    26
    
    typedef struct person_t {
       char FName[Nlen];
       char LName[Nlen];
       char Sex;
       char Grade;
       struct person_t *Next;
      } PERSON;
    
    struct person_t  *Top = NULL;         
    struct person_t  *Node = NULL;
    /*********************************************************************
     Funstion to Ordered linked list
     This is where i will call the Last name and put it into the linked   
     list ordered by last name. 
     
    ********************************************************************/
    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) {               
    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  Funstion to Ordered linked list
    ***************************************************************/
    
    void amend_file( char file[], PERSON *STU ); 
    void show_file( char file[], PERSON *STU );
    
    
    int main (void)
    {
       PERSON *STU;
       int Ans;
            char file[] = "e: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 */
          {
            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 funcation 
                  break;
                case 2:
                   show_file(file, STU);  //calls teh show file funcation
                   break;
                case 3: default:
                   Ans = 3;
                   break;
             }
          } /* /get answer */
       }
        while ( Ans != 3 );
        
          fflush(stdout);
          free(STU);
          getchar ();
       return EXIT_SUCCESS;
    }
    /*************************************************************************
    * amend_file: Enter stuff into the working file.
    *
    **************************************************************************/
    void amend_file( char file[], PERSON *STU )
    {
       assert(STU != 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", STU->FName, STU->LName, &STU->Sex,
          &STU->Grade);
           Top = InsertNode (Top, Node);  //calls the insert Node funcation  which it is not getting into becuase
                    //Im haveing issues with getting the file to print. 
                fprintf(fin, "%s %s %c %c\n", STU->LName, STU->FName, STU->Sex, STU->Grade);
               fflush(fin);
                fclose(fin); 
          }
    /******************************************************************************
    * show_file: Reads the working file.
    * Bare bones again...
    ******************************************************************************/
    void show_file ( char file[], PERSON *STU )
    {
      assert(STU != NULL);
      int b = 1;
      FILE *fin = fopen(file, "r");
        
      if (file == NULL)
      {
        perror(file);
        exit(EXIT_FAILURE);
      }
      while (fscanf(fin, "%s %s %c %c", STU->FName, STU->LName, &STU->Sex,
         &STU->Grade)== 4)
      {   
       Top = InsertNode (Top, Node);  //read insernode function
        b++;          
        printf("Record %d: %s %s %c %c\n", b, STU->FName, STU->LName,
           STU->Sex, STU->Grade);
      }
      fclose(fin);
    }

  15. #15
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by redmondtab
    Code:
    PERSON* InsertNode (PERSON *Top, PERSON *Node )
    Quote Originally Posted by kennedy
    Code:
    void addinorder(ASTRUCT **head, ASTRUCT *node)
    Look at the differences between mine and yours. Your problem is that you aren't passing the variable into the procedure, but the current pointer value. You'll need to pass this variable into the function via reference. Likewise, you'll need to accept a pointer reference into your function.

Page 1 of 4 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, 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, 01: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, 08:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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