Solved

link list problem

Posted on 1998-03-15
22
265 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
ID: 1257709
This smells like a homework; post some code first, then you'll get advice.
0
 

Author Comment

by:trying
ID: 1257710
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
ID: 1257711
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
ID: 1257712
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
ID: 1257713
I was not talking about you, ozo! ;)
0
 

Expert Comment

by:keaster
ID: 1257714
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
ID: 1257715
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
ID: 1257716
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
ID: 1257717
Did it work, trying?
0
 

Author Comment

by:trying
ID: 1257718
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
ID: 1257719
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:trying
ID: 1257720
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
ID: 1257721
what compiler did the program work on?
0
 

Author Comment

by:trying
ID: 1257722
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
ID: 1257723
Can you post your names.dat file here?  It 's probably not
reading the data file.
0
 

Author Comment

by:trying
ID: 1257724
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
ID: 1257725
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
ID: 1257726
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
ID: 1257727
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
ID: 1257728
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
ID: 1257729
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
ID: 1257730
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

947 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

22 Experts available now in Live!

Get 1:1 Help Now