Typically, you'd want to find the correct location of the node you're trying to insert, then insert it.
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
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.
I'm calling it byCode: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 still do not thin that Im calling in the funcation. Or if Im calling it at the wrong place of my program.Code:InsertNode (Top, Node); //read insernode function
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.here are my errorsCode: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++; }
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.
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.Code:STU->LName = (struct person_t *) malloc(sizeof(person_t)); if(!LName) return -1; LName->LName= Top ; InsertNode(LName, &Top, &Node); i++;
This error is pretty much self-explanitory. You have a function declared as void. You're returning something which is not void...132 E:\lab4.cpp return-statement with a value, in function returning 'void'
Again, here you're comparing pointers, not strings. I've told you this before. Use strcmp() !Code:Here->LName > Node->LName
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
So what you are saying is thati should be using strcmp to compare the two. Not to sure how to write that oneCode:while ( (Here != NULL) &&(Here->LName < Node->LName) ) if (Here->LName > Node->LName
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
so instead of if i would dothatis 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:strcmp(Here->LName ,Node->LName)
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.
Look at post #15. You aren't creating the list.
i did put the void in. but I'm still having the same issue. Im not putting the data into the insertNode function.
Oops, brain fart. . . sorry didn't read your code enough. . . You are creating the list. . . Didn't see that whole return thing. ::Blushes::
Where are you allocating memory for STU?Originally Posted by redmondtab
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
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 programOriginally Posted by Kennedy
Remember, you'll need a malloc() for every new record you create. You should put it just before the input statements.
so what i need itsso 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 =.Code:LName = (struct person_t ) malloc(sizeof(lperson_t))
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.