ordered linked list

This is a discussion on ordered linked list within the C Programming forums, part of the General Programming Boards category; Typically, you'd want to find the correct location of the node you're trying to insert, then insert it....

  1. #16
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Typically, you'd want to find the correct location of the node you're trying to insert, then insert it.
    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. #17
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    Yes that is what I'm trying to do. So I need to make sure that the data is also in alphabetic order. So i enter in the data it looks at whats there and then prints it to the file in the order. Or is there a differnt way that i should be doing in. I have changed my insert funcation to this. I added the void.
    Code:
    void 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 */
    }
    I'm calling it by
    Code:
      InsertNode (Top, Node);  //read insernode function
    I still do not thin that Im calling in the funcation. Or if Im calling it at the wrong place of my program.

    ok so im not passign the data. I htough that I could use this to pass the data. But am having trouble definging it any help please.
    Code:
        	for(i = 0; i< count ;i++)
    	{
    		STU->LName = (struct person_t  *) malloc(sizeof(person_t));
    		if(!LName)
    			return -1;
    		LName->LName= Top ;
    		InsertNode(LName, &Top, &Node);
    		i++;
    	}
    here are my errors
    130 E:\lab4.cpp incompatible types in assignment of `person_t*' to `char[26]'
    131 E:\lab4.cpp `LName' undeclared (first use this function)
    (Each undeclared identifier is reported only once for each function it appears in.)
    132 E:\lab4.cpp return-statement with a value, in function returning 'void'
    Last edited by redmondtab; 10-17-2006 at 01:29 PM.

  3. #18
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Code:
    STU->LName = (struct person_t  *) malloc(sizeof(person_t));
    		if(!LName)
    			return -1;
    		LName->LName= Top ;
    		InsertNode(LName, &Top, &Node);
    		i++;
    You already declared this as an array in your struct. So space was already allocated to it when you declared LName. All you need to do is fill that array up.

    132 E:\lab4.cpp return-statement with a value, in function returning 'void'
    This error is pretty much self-explanitory. You have a function declared as void. You're returning something which is not void...

    Code:
    Here->LName > Node->LName
    Again, here you're comparing pointers, not strings. I've told you this before. Use strcmp() !

    The logic behind your insertion seems fine. Your understanding of C, not so fine.
    Anyhow, that should get you a little further.
    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

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    So what you are saying is that
    Code:
     while ( (Here != NULL) &&(Here->LName < Node->LName) ) 
    
    if (Here->LName > Node->LName
    i should be using strcmp to compare the two. Not to sure how to write that one

  5. #20
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Yes, otherwise you're comparing addresses.

    Here's how strcmp works. You call strcmp(s1,s2). The function will return 0 if both string are equal (AKA s1 = "abc" and s2 = "abc")
    It will return smaller than zero if s1 is lexicographically inferior to s2 (s1 = "abc", s2 = "bc")
    It will return greater than zero if s1 is lexicographically superior to s2 (s1 = "bc", s2 = "abc").

    Oh, and strcmp is in <string.h>, so make sure to include the header.
    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

  6. #21
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    so instead of if i would do
    Code:
    strcmp(Here->LName ,Node->LName)
    thatis here my if sttement was. thisis how ichanged it in the code. I still do nto see it going into the funcation. am i wrong fro putting the Insertfuncation into the void amend_file( char file[], PERSON *STU ); and void show_file I should have it in the case can add it then a statment to print to the screen or an i all the way off.

    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;
              
        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  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 )
    {
         int i, count;
       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);
          InsertNode (Top, Node);
            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, "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);  //read insernode function
        b++;          
        printf("Record %d: %s %s %c %c\n", b, STU->FName, STU->LName,
           STU->Sex, STU->Grade);
      }
      fclose(fin);
    }
    Last edited by redmondtab; 10-18-2006 at 02:41 PM.

  7. #22
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Look at post #15. You aren't creating the list.

  8. #23
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    i did put the void in. but I'm still having the same issue. Im not putting the data into the insertNode function.

  9. #24
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Oops, brain fart. . . sorry didn't read your code enough. . . You are creating the list. . . Didn't see that whole return thing. ::Blushes::

  10. #25
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by redmondtab
    Code:
    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);
    Where are you allocating memory for STU?

  11. #26
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    ugh.

    Code:
    if(strcmp(s1,s2) < 0)
    {
        //  s1 goes before s2
    }
    else
    {
        // s1 goes after s2
    }
    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

  12. #27
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    Quote Originally Posted by Kennedy
    Where are you allocating memory for STU?
    I know that I'm missing that part. sorry. Trying to figure out where I should do that. I know that I need a macoll statement. Just not to sure where in the progrma i need to add it. I think that I should put it in the main part of the program

  13. #28
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Remember, you'll need a malloc() for every new record you create. You should put it just before the input statements.

  14. #29
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    so what i need its
    Code:
     LName = (struct person_t ) malloc(sizeof(lperson_t))
    so need to have this definned. and i have to have it as something as well right but that needs to be defined in the structure persons t as well. or can I use the LNane =.

  15. #30
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    You want to allocate memory for the new node STU, not for a portion of it.

    LName (if I've got my head on straight this morning) is statically defined in your struct. If you allocate memory for type PERSON, you'll get two arrays of char.

Page 2 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, 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