trying
asked on
link list problem
i need a way to assign 15 names, originally scaned in from a file, to nodes, then ask user to assign up to 4 quiz scores for each name; assign the pointer to each name, and their score
This smells like a homework; post some code first, then you'll get advice.
ASKER
ok, here is what i have so far, (yes, it's homework)
i can't get this to work the way i want it to....
#include<stdio.h>
#include<stdlib.h>
FILE *fp; //FILE POINTER
int ctr,x,y; //GENERAL PURPOSE COUNTERS
struct Entry
{
int scores[4];
char name[25]; //ACCEPT UP TO 25 char IN NAME
struct Entry *next;
};
typedef struct Entry NODE;
int OpenFile(void);
//MAIN
int main(void)
{
//DECLARE STORAGE FOR 15 NODES (1 FOR EACH STUDENT)
NODE N[15];
NODE *Head, *ptr;
//STUDENT NAMES
OpenFile();
//STORE NULL IN EACH POINTER
for(ctr=0;ctr<15;ctr++)
{
N[ctr].next=(NODE *)NULL;
}
//CREATE ADD'L POINTER TO POINT TO Head *(FIRST NODE) OF LIST
Head = &N[0];
//SET A POINTER TO HEAD OF LIST AND ADVANCE THOUGH LIST VIA THE NEXT
//POINTER; UNTIL NULL IS ENCOUNTERED. NULL INDICATES THE END OF LIST
ptr = Head;
for(x=0;x<15;x++)
{
while(fgets(N[ctr].name[x] , 180, fp) != NULL)
{
fputs(N[ctr].name[x], stdout);//DOES NOT WORK????
}
}
printf("\nValue of N[0]=%s", N[0].name[x], stdout); //DON'T WORK????
//CLOSE FILES PREVIOUSLY OPENED
fclose(fp);
return(0);
}
// OPEN FILE
int OpenFile(void)
{
//OPEN FILE FOR READ & CHECK FOR ERRORS
if(!(fp = fopen("d:\\MSVC\\names.dat ", "r")))
{
puts("\n\a***ERROR OPENING FILE***");
//EXIT PROGRAM IF FAILURE
exit(0);
}
return(0);
}
i can't get this to work the way i want it to....
#include<stdio.h>
#include<stdlib.h>
FILE *fp; //FILE POINTER
int ctr,x,y; //GENERAL PURPOSE COUNTERS
struct Entry
{
int scores[4];
char name[25]; //ACCEPT UP TO 25 char IN NAME
struct Entry *next;
};
typedef struct Entry NODE;
int OpenFile(void);
//MAIN
int main(void)
{
//DECLARE STORAGE FOR 15 NODES (1 FOR EACH STUDENT)
NODE N[15];
NODE *Head, *ptr;
//STUDENT NAMES
OpenFile();
//STORE NULL IN EACH POINTER
for(ctr=0;ctr<15;ctr++)
{
N[ctr].next=(NODE *)NULL;
}
//CREATE ADD'L POINTER TO POINT TO Head *(FIRST NODE) OF LIST
Head = &N[0];
//SET A POINTER TO HEAD OF LIST AND ADVANCE THOUGH LIST VIA THE NEXT
//POINTER; UNTIL NULL IS ENCOUNTERED. NULL INDICATES THE END OF LIST
ptr = Head;
for(x=0;x<15;x++)
{
while(fgets(N[ctr].name[x]
{
fputs(N[ctr].name[x], stdout);//DOES NOT WORK????
}
}
printf("\nValue of N[0]=%s", N[0].name[x], stdout); //DON'T WORK????
//CLOSE FILES PREVIOUSLY OPENED
fclose(fp);
return(0);
}
// OPEN FILE
int OpenFile(void)
{
//OPEN FILE FOR READ & CHECK FOR ERRORS
if(!(fp = fopen("d:\\MSVC\\names.dat
{
puts("\n\a***ERROR OPENING FILE***");
//EXIT PROGRAM IF FAILURE
exit(0);
}
return(0);
}
cfe: Warning 712: trying.c, line 48: illegal combination of pointer and integer
while(fgets(N[ctr].name[x] , 180, fp) != 0 )
-------------------------- ------^
cfe: Warning 712: trying.c, line 50: illegal combination of pointer and integer
fputs(N[ctr].name[x], (&__iob[1]) );
----------------------^
while(fgets(N[ctr].name[x]
--------------------------
cfe: Warning 712: trying.c, line 50: illegal combination of pointer and integer
fputs(N[ctr].name[x], (&__iob[1]) );
----------------------^
Hmmm, i see there's some work in progress, but i cannot see big errors other than your struct-typedef couple which should be:
typedef struct Entry {
int scores[4];
char name[25];
struct Entry *next;
} NODE;
Ok, i'll leave this open, so that you can keep developing your code and coming back here for support; i just hope you won't grade the first "Cossak" who comes here and submits a couple of lines as answer, only because you don't feel like hitting the "reject" button! :)
typedef struct Entry {
int scores[4];
char name[25];
struct Entry *next;
} NODE;
Ok, i'll leave this open, so that you can keep developing your code and coming back here for support; i just hope you won't grade the first "Cossak" who comes here and submits a couple of lines as answer, only because you don't feel like hitting the "reject" button! :)
I was not talking about you, ozo! ;)
Hi trying,
I will try to point out some problems to you:
1) You have declared an array of 15 nodes, that makes it an array of 15 nodes, not a linked list. You would realize this when you see that the the "struct Entry *next" is superfluous in the struct. Try to figure out(lots of books) what a linked list is.
2) "while(fgets(N[ctr].name[x ], 180, fp) != NULL)"
is wrong..
you get compilation errors because you should be typing:
while(fgets(N[x].name, 25, fp) != NULL) instead.
Note that N[x].name[ctr] refers to a character in the string "name" as upposed to N[x].name which is equivalent to "char *".
3) Before you use a function, try to find out what the parameters are and how to use it. Start with "fgets".
(Note: &__iob[1] is your "stdout" buffer)
I feel it woud not be appropriate for me to correct your code for you since you are learning.
Regards.
I will try to point out some problems to you:
1) You have declared an array of 15 nodes, that makes it an array of 15 nodes, not a linked list. You would realize this when you see that the the "struct Entry *next" is superfluous in the struct. Try to figure out(lots of books) what a linked list is.
2) "while(fgets(N[ctr].name[x
is wrong..
you get compilation errors because you should be typing:
while(fgets(N[x].name, 25, fp) != NULL) instead.
Note that N[x].name[ctr] refers to a character in the string "name" as upposed to N[x].name which is equivalent to "char *".
3) Before you use a function, try to find out what the parameters are and how to use it. Start with "fgets".
(Note: &__iob[1] is your "stdout" buffer)
I feel it woud not be appropriate for me to correct your code for you since you are learning.
Regards.
I just thought I would give you some tidbits on linked lists. keaster is correct, your N[15] is just an array of Nodes not a link. A link is where each node is attached to the next via the internal next pointer. In your case you would malloc() the first node and then set the next pointer to null. When a new node is needed you malloc another one and set the previous node to the new nodes pointer value. You should then set the tail pointer to the newly malloced pointer. This is called a singly linked list. If you want a double linked list you would also have prev pointer that has the pointer value of the prevoius entry in the list. Also, don't forget that you MUST free() all the nodes in sequence. That is, start at the head and store the next pointer and free the current node then move to the next node until the next pointer is null.
Good Luck
Good Luck
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Did it work, trying?
ASKER
yes! it worked! just fine, best answer i've received yet
(in the past, your answers have been nothing less than perfect.
i'm not just saying that. you've helped me more than anyone. at times, better than my teacher! thanks a mil)
i'm kinda lost though, can you tell me what's happening in
the code?
(in the past, your answers have been nothing less than perfect.
i'm not just saying that. you've helped me more than anyone. at times, better than my teacher! thanks a mil)
i'm kinda lost though, can you tell me what's happening in
the code?
int main(void)
{
int i;
NODE *Head, *ptr;
//ALLOCATE STORAGE FOR 15 NODES (1 FOR EACH STUDENT)
//AND LINK THEM TOGETHER
//Allocate space for one node and make head point to it
Head = (NODE *) malloc (sizeof(NODE));
Head->next = 0;
ptr = Head;
for (i=0; i < 15; i++) {
ptr = ptr->next;
//Allocate space for one more node and link it with
//the previous node
ptr = (NODE *) malloc (sizeof(NODE));
//Make sure the last node is pointing to Null
ptr->next = 0;
}
//OPEN FILE FOR READING
OpenFile();
//READ NAME INTO THE LINKED LIST
ptr = Head;
for(x=0;x<15;x++)
{
//Go through each node and store the name in it
fgets(ptr->name, 25, fp);
puts(ptr->name);
ptr = ptr->next;
}
//CLOSE FILES PREVIOUSLY OPENED
fclose(fp);
return(0);
}
{
int i;
NODE *Head, *ptr;
//ALLOCATE STORAGE FOR 15 NODES (1 FOR EACH STUDENT)
//AND LINK THEM TOGETHER
//Allocate space for one node and make head point to it
Head = (NODE *) malloc (sizeof(NODE));
Head->next = 0;
ptr = Head;
for (i=0; i < 15; i++) {
ptr = ptr->next;
//Allocate space for one more node and link it with
//the previous node
ptr = (NODE *) malloc (sizeof(NODE));
//Make sure the last node is pointing to Null
ptr->next = 0;
}
//OPEN FILE FOR READING
OpenFile();
//READ NAME INTO THE LINKED LIST
ptr = Head;
for(x=0;x<15;x++)
{
//Go through each node and store the name in it
fgets(ptr->name, 25, fp);
puts(ptr->name);
ptr = ptr->next;
}
//CLOSE FILES PREVIOUSLY OPENED
fclose(fp);
return(0);
}
ASKER
hi q2guo,
i don't understand. at first, when i ran the program with
your changes, it worked fine. now it won't compile under
turbo c++ 3.0 (lines with: fgets(ptr->name,25,fp); etc.)
the error is something "connot convert char * to char far *"
and under windows using msvc v1.52 it compiles ok. but when
i go to run it, it performs an illegal shutdown? at the same
lines???
PLEASE HELP
i don't understand. at first, when i ran the program with
your changes, it worked fine. now it won't compile under
turbo c++ 3.0 (lines with: fgets(ptr->name,25,fp); etc.)
the error is something "connot convert char * to char far *"
and under windows using msvc v1.52 it compiles ok. but when
i go to run it, it performs an illegal shutdown? at the same
lines???
PLEASE HELP
what compiler did the program work on?
ASKER
q2guo,
it worked on Microsoft Visual C++ v1.52 (real old, but one of the
best out there!)
but, then it didn't work any more
??
could it have something to do with the fgets, and puts statements?
it worked on Microsoft Visual C++ v1.52 (real old, but one of the
best out there!)
but, then it didn't work any more
??
could it have something to do with the fgets, and puts statements?
Can you post your names.dat file here? It 's probably not
reading the data file.
reading the data file.
ASKER
q2guo, here is the names.dat file: (no space at beginning or
end of file)
Albert Einstein
Steve Abrew
David Nagasake
Mike Black
Andrew Dijkstra
Joanne Nguyen
Chris Walljasper
Fred Albert
Dennis Dudley
Leo Rice
Fred Flintstone
Frances Dupre
Dave Light
Hua Tran
Sarah Trapp
end of file)
Albert Einstein
Steve Abrew
David Nagasake
Mike Black
Andrew Dijkstra
Joanne Nguyen
Chris Walljasper
Fred Albert
Dennis Dudley
Leo Rice
Fred Flintstone
Frances Dupre
Dave Light
Hua Tran
Sarah Trapp
try this
-------------------------- ---------- ---------
int main(void)
{
int i;
NODE *Head, *ptr;
//ALLOCATE STORAGE FOR 15 NODES (1 FOR EACH STUDENT)
//AND LINK THEM TOGETHER
//Allocate space for one node and make head point to it
Head = (NODE *) malloc (sizeof(NODE));
Head->next = 0;
ptr = Head;
for (i=0; i < 15; i++) {
//Allocate space for one more node and link it with
//the previous node
ptr->next = (NODE *) malloc (sizeof(NODE));
//Make sure the last node is pointing to Null
ptr = ptr->next;
ptr->next = 0;
}
//OPEN FILE FOR READING
OpenFile();
//READ NAME INTO THE LINKED LIST
ptr = Head;
for(x=0;x<15;x++)
{
//Go through each node and store the name in it
fgets(ptr->name, 25, fp);
puts(ptr->name);
ptr = ptr->next;
}
//CLOSE FILES PREVIOUSLY OPENED
fclose(fp);
//DELETE THE LIST
ptr = Head;
for(x=0;x<15;x++)
{
Head = ptr->next;
delete ptr;
ptr = Head->next;
}
return(0);
}
--------------------------
int main(void)
{
int i;
NODE *Head, *ptr;
//ALLOCATE STORAGE FOR 15 NODES (1 FOR EACH STUDENT)
//AND LINK THEM TOGETHER
//Allocate space for one node and make head point to it
Head = (NODE *) malloc (sizeof(NODE));
Head->next = 0;
ptr = Head;
for (i=0; i < 15; i++) {
//Allocate space for one more node and link it with
//the previous node
ptr->next = (NODE *) malloc (sizeof(NODE));
//Make sure the last node is pointing to Null
ptr = ptr->next;
ptr->next = 0;
}
//OPEN FILE FOR READING
OpenFile();
//READ NAME INTO THE LINKED LIST
ptr = Head;
for(x=0;x<15;x++)
{
//Go through each node and store the name in it
fgets(ptr->name, 25, fp);
puts(ptr->name);
ptr = ptr->next;
}
//CLOSE FILES PREVIOUSLY OPENED
fclose(fp);
//DELETE THE LIST
ptr = Head;
for(x=0;x<15;x++)
{
Head = ptr->next;
delete ptr;
ptr = Head->next;
}
return(0);
}
ASKER
q2guo,
it did not work, still getting program shutdown.
although, it begins to print the names in the file, then
shuts down after the 2nd or 3rd name.
it did not work, still getting program shutdown.
although, it begins to print the names in the file, then
shuts down after the 2nd or 3rd name.
I made some more changes, try this
-------------------------- ---------- ---------
int main(void)
{
int i;
NODE *Head, *ptr;
//ALLOCATE STORAGE FOR 15 NODES (1 FOR EACH STUDENT)
//AND LINK THEM TOGETHER
//Allocate space for one node and make head point to it
Head = (NODE *) malloc (sizeof(NODE));
Head->next = 0;
ptr = Head;
for (i=0; i < 14; i++) {
//Allocate space for one more node and link it with
//the previous node
ptr->next = (NODE *) malloc (sizeof(NODE));
//Make sure the last node is pointing to Null
ptr = ptr->next;
ptr->next = 0;
}
//OPEN FILE FOR READING
OpenFile();
//READ NAME INTO THE LINKED LIST
ptr = Head;
for(x=0;x<15;x++)
{
//Go through each node and store the name in it
fgets(ptr->name, 25, fp);
puts(ptr->name);
ptr = ptr->next;
}
//CLOSE FILES PREVIOUSLY OPENED
fclose(fp);
//DELETE THE LIST
for(x=0;x<15;x++)
{
ptr = Head;
Head = Head->next;
free ptr;
}
return(0);
}
--------------------------
int main(void)
{
int i;
NODE *Head, *ptr;
//ALLOCATE STORAGE FOR 15 NODES (1 FOR EACH STUDENT)
//AND LINK THEM TOGETHER
//Allocate space for one node and make head point to it
Head = (NODE *) malloc (sizeof(NODE));
Head->next = 0;
ptr = Head;
for (i=0; i < 14; i++) {
//Allocate space for one more node and link it with
//the previous node
ptr->next = (NODE *) malloc (sizeof(NODE));
//Make sure the last node is pointing to Null
ptr = ptr->next;
ptr->next = 0;
}
//OPEN FILE FOR READING
OpenFile();
//READ NAME INTO THE LINKED LIST
ptr = Head;
for(x=0;x<15;x++)
{
//Go through each node and store the name in it
fgets(ptr->name, 25, fp);
puts(ptr->name);
ptr = ptr->next;
}
//CLOSE FILES PREVIOUSLY OPENED
fclose(fp);
//DELETE THE LIST
for(x=0;x<15;x++)
{
ptr = Head;
Head = Head->next;
free ptr;
}
return(0);
}
ASKER
q2guo,
it worked! no problems! i ran it several times to make sure.
now all i need to do is have ptr also point to scores[] in
structure, and read those from the keyboard.
thanks a mil
trying.
it worked! no problems! i ran it several times to make sure.
now all i need to do is have ptr also point to scores[] in
structure, and read those from the keyboard.
thanks a mil
trying.
Add the code below before line
//DELETE THE LIST
-------------------------- ---------- ---------- ---------- --------
ptr = Head;
for (x=0; x <15; x++) {
printf("Enter 4 grades for %s separated by spaces\n", ptr->name);
scanf("%d %d %d %d", &ptr->scores[0], &ptr->scores[1], &ptr->scores[2], &ptr->scores[3]);;
ptr = ptr->next;
}
//DELETE THE LIST
--------------------------
ptr = Head;
for (x=0; x <15; x++) {
printf("Enter 4 grades for %s separated by spaces\n", ptr->name);
scanf("%d %d %d %d", &ptr->scores[0], &ptr->scores[1], &ptr->scores[2], &ptr->scores[3]);;
ptr = ptr->next;
}
ASKER
q2guo,
the last piece of code works perfectly!
I would like to thank you for all your help. You're a great
asset to Experts Exchange.
I look forward to working with you in the future.
the last piece of code works perfectly!
I would like to thank you for all your help. You're a great
asset to Experts Exchange.
I look forward to working with you in the future.