Solved

link list problem

Posted on 1998-03-15
22
256 Views
Last Modified: 2013-12-14
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
0
Comment
Question by:trying
  • 8
  • 8
  • 3
  • +3
22 Comments
 
LVL 5

Expert Comment

by:julio011597
Comment Utility
This smells like a homework; post some code first, then you'll get advice.
0
 

Author Comment

by:trying
Comment Utility
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);
}
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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]) );
 ----------------------^

0
 
LVL 5

Expert Comment

by:julio011597
Comment Utility
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! :)
0
 
LVL 5

Expert Comment

by:julio011597
Comment Utility
I was not talking about you, ozo! ;)
0
 

Expert Comment

by:keaster
Comment Utility
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.
0
 
LVL 1

Expert Comment

by:GregL
Comment Utility
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
0
 
LVL 3

Accepted Solution

by:
q2guo earned 150 total points
Comment Utility
Hi trying

I fixed up your program a bit.  The program should construct the
linked list and store names in each node.  Let me know if it works.  If it works, I can help you with the next part of
the program (assign quiz scores).  If it doesn't work, can you please post your 'name.dat' file here.

------------------------------------------------------------
#include<stdio.h>
#include<stdlib.h>

FILE *fp;       //FILE POINTER

int ctr,x,y;                   //GENERAL PURPOSE COUNTERS

struct Entry
{
    int  scores[4];
    char name[26]; //ACCEPT UP TO 25 char IN NAME
    struct Entry *next;
};

typedef struct Entry NODE;


int OpenFile(void);

//MAIN
int main(void)
{
    int i;
    NODE *Head, *ptr;
 
    //ALLOCATE STORAGE FOR 15 NODES (1 FOR EACH STUDENT)        
    //AND LINK THEM TOGETHER

    Head = (NODE *) malloc (sizeof(NODE));
    Head->next = 0;
    ptr = Head;
   
    for (i=0; i < 15; i++) {
       ptr = ptr->next;
       ptr = (NODE *) malloc (sizeof(NODE));        
       ptr->next = 0;
    }

    //OPEN FILE FOR READING
    OpenFile();
                       
    //READ NAME INTO THE LINKED LIST
    ptr = Head;
    for(x=0;x<15;x++)
    {
        fgets(ptr->name, 25, fp);
        puts(ptr->name);
        ptr = ptr->next;
    }
           
    //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);
}







0
 
LVL 3

Expert Comment

by:q2guo
Comment Utility
Did it work, trying?
0
 

Author Comment

by:trying
Comment Utility
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?
0
 
LVL 3

Expert Comment

by:q2guo
Comment Utility
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);
}
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:trying
Comment Utility
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
0
 
LVL 3

Expert Comment

by:q2guo
Comment Utility
what compiler did the program work on?
0
 

Author Comment

by:trying
Comment Utility
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?

0
 
LVL 3

Expert Comment

by:q2guo
Comment Utility
Can you post your names.dat file here?  It 's probably not
reading the data file.
0
 

Author Comment

by:trying
Comment Utility
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
0
 
LVL 3

Expert Comment

by:q2guo
Comment Utility
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);
}
0
 

Author Comment

by:trying
Comment Utility
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.
0
 
LVL 3

Expert Comment

by:q2guo
Comment Utility
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);
       }
0
 

Author Comment

by:trying
Comment Utility
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.
0
 
LVL 3

Expert Comment

by:q2guo
Comment Utility
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;
}

0
 

Author Comment

by:trying
Comment Utility
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.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now