Thread: Need help with Single Linked List Database Assignment

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    12

    Need help with Single Linked List Database Assignment

    I have to write a menu UI program that adds student's name, score, average, letter grade, show all data for 1 particular student, and show all the data. the name has to be a string, id is a string of 4, and score is an int array of 4. The program is to be implemented using linked-list.
    This is what I got so far and it's not looking good... We had a similar assignment but using structures instead of linked-lists and I attempted to convert it with no luck

    Code:
    #include <stdio.h>
    #include <windows.h>
    #include <stdlib.h>
    
    #define FLUSH while (getchar() != '\n')
    #define M 3
    #define N 4
    
    typedef struct node *nd;
    struct node
    {
            char sId[4];
            char name[40];
            int score[N];
            char grade;
            struct node *next;
            
    }STUD;
    
    void inputData(nd *head);
    void inquireScores(nd head);
    void inquireAverage(nd head);
    void inquireGrade(nd head);
    void inquireStudent(nd head);
    void showAll(nd head);
    int search(nd head);
    void showScore(nd head);
    void showName(nd head);
    float calcAverage(nd head);
    char getGrade(float);
    void showError(void);
    
    int main(void)
    {
        struct STUD *nd;
        int ans,n;
        
        inputData(&nd);
        
        do{
     
             system("cls");            
             printf("1] Student Score\n");           
             printf("2] Student Average:\n");
             printf("3] Student Grade\n");
             printf("4] Student Academic Record\n");
             printf("5] Student List\n");
             printf("6] Exit\n\n");
             printf("Choice: ");
             scanf("%d",&ans);
             switch(ans)
           {
                  case 1:   inquireScores(nd);   
                            break;
                  case 2:   inquireAverage(nd);
                            break;
                  case 3:   inquireGrade(nd);
                            break;
                  case 4:   inquireStudent(nd);
                            break;
                  case 5:   showAll(nd);
                            break;
                  case 6:   printf("\n\nClosing program...");
                            sleep(3000);
                            break;
                  default:  printf("\n\nInvalid choice...");
                            sleep(2000);
             }
        }while (ans != 6);
        return 0;
    
    }
    
        
    void inputData(nd *head)
    {
         char sId[4];
         char name[30];
         nd t, t2, temp;
         temp=malloc(sizeof(STUD));
         temp -> next = NULL;
         
         while(temp->next != NULL)    
         {
               t = temp->next;
               t2 = temp->next;
               system("cls");
               
               printf("ID: ");
               strcpy(t->sId, sId);
               printf("\nName: ");
               strcpy(temp->name, name);
               printf("\nscore [%d]: ");
               scanf("%d",&t2->score);
               
               FLUSH;
         }
         return;
    }
    
    void showName(nd head)
    {
         nd temp;
         temp=malloc(sizeof(STUD));
         temp -> next = NULL;
         
         while(temp->next != NULL)
         {
         system("cls");
         printf("Student ID: %s\n",temp->sId); 
         printf("Student Name: %s\n",temp->name);    
         }
         return;
    }
    
    void showScore(nd head)
    {
         nd temp;
         temp=malloc(sizeof(STUD));
         temp -> next = NULL;
         printf("Scores: \n");
         while(temp->next != NULL)
         {
         printf("     %d\n",temp->score);      
         }
         return;
    }
    
    void showError(void)
    {
         printf("Student ID not found...");
         getch();
         
         return;
    }
       
    void inquireScores(nd head)
    {
         char name[30];
         nd temp;
         temp=malloc(sizeof(STUD));
         temp -> next = NULL;
         
         while (temp != NULL && strcmp(temp->name, name) != 0) 
         {
               temp = temp->next;
         }
    
         if (temp == NULL) 
         {
            showError();
            return;
         }
         else
         {
               showName(head);           
               showScore(head);
               getch();
         }
         return;
    }
    
    void inquireAverage(nd head)
    {
         
         nd temp;
         char name[30],gr;
         float avg;
         temp=malloc(sizeof(STUD));
         temp -> next = NULL;
         
         while (temp != NULL && strcmp(temp->name, name) != 0) 
         {
               temp = temp->next;
         }
    
         if (temp == NULL) 
         {
            showError();
            return;
         }
         else
         {
               
               avg = calcAverage(head);
               showName(head);
               printf("Average: %0.2f",avg);
               getch();
         }
         return;
        
    }
    
    void inquireGrade(nd head)
    {
          nd temp;
         char name[30],gr;
         float avg;
         temp=malloc(sizeof(STUD));
         temp -> next = NULL;
         
         while (temp != NULL && strcmp(temp->name, name) != 0) 
         {
               temp = temp->next;
         }
    
         if (temp == NULL) 
         {
            showError();
            return;
         }
         else
         {
               avg = calcAverage(head);
               gr = getGrade(avg);
               showName(head);
               printf("\nGrade: %c",gr);
               getch();
         }
         return;
    }
    
    char getGrade(float avg)
    {
         char gr;
         
         if (avg >= 91)
            gr = 'A';
         else if ((gr >= 81) && (gr <= 90))
              gr = 'B';
         else if ((gr >= 71) && (gr <= 80))
              gr = 'C';
         else if ((gr >= 61) && (gr <= 70))
              gr = 'P';
         else
             gr = 'F';
         return gr;
    }
    
    float calcAverage(nd head)
    {
          nd temp;
          temp=head;
          int sum = 0, num;
          float avg;
          
          
          while ( temp->next != NULL ) 
          {
           	sum+=temp->score;
            num+=1;
            avg = (float) sum / num;
            return avg;
            
          }
          
    }
    
    int search(nd head)
    {
        int i,res = -1;
        nd temp;
        temp=malloc(sizeof(STUD));
        temp -> next = NULL;
        char id[5];
        
        FLUSH;
        printf("ID: ");    
        gets(temp -> sId);
        while(temp->next != NULL)
        {
              if (strcmp(temp->sId,temp->next->sId) == 0)
              {
                    res = i;
                    break;
              }
        }                     
        return res;
    } 
    
    void inquireStudent(nd head)
    {
         
         nd temp;
         char name[30],gr;
         float avg;
         temp=malloc(sizeof(STUD));
         temp -> next = NULL;
         
         while (temp != NULL && strcmp(temp->name, name) != 0) 
         {
               temp = temp->next;
         }
    
         if (temp == NULL) 
         {
            showError();
            return;
         }
         else
         {
               showName(head);
               showScore(head);
               avg = calcAverage(head);
               printf("\nAverage: %0.2f",avg);
               gr = getGrade(avg);
               printf("\nGrade: %c",gr);
               getch();
         }
         return;
    } 
    
    void showAll(nd head)
    {
        nd temp;
        temp=malloc(sizeof(STUD));
        temp -> next = NULL;
        if (head==NULL)
            printf ("No elements to print!\n");
        else {
            temp=head;
            do {
                 system("cls");
                  printf("Student List:\n\n");
                  printf("Student ID\tStudent Name\n\n");
                  printf("%s\t\t%s\n",temp -> sId, temp -> name);
                temp=temp->next;
            } while (temp!=NULL);
        }     
               
         getch();
         return;
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    In most of your functions, you take a "head" pointer as a parameter to the function; then you allocate a node called temp, and set its next to NULL, and proceed to iterate through temp. Since you just allocated temp, the function will behave the same way no matter what head you pass in. For the functions that are simply examining the linked list, you probably want to set temp to point to head (instead of allocating a new node with malloc). And for the functions that are supposed to create a node, you need to add temp back into the head somehow. Whenever you call malloc(), you're grabbing new memory and leaving the existing head and existing list alone.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    12
    how do you recommend me pointing temp back into head?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Presumably something akin to "temp->next = head" or to "head->next = temp" will be relevant. There's more to it than that but that's something for you to work out. Don't rely on guess work. Reason it out.

    There is also the less-than-incidental problem that your code wouldn't compile anyway. Since compilers tend to tell you what's wrong when they refuse to compile your code (albeit, in compiler-speak so some effort to interpret the messages will be needed), your first step needs to be understanding the error messages, and the second needs to be correcting the problems that cause them.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  2. Help with Insertion sort on a single linked list
    By goron350 in forum C++ Programming
    Replies: 4
    Last Post: 07-11-2005, 08:38 PM
  3. single linked list
    By Max in forum C Programming
    Replies: 1
    Last Post: 11-21-2002, 10:27 AM
  4. Changing single to double linked list
    By BR7 in forum C Programming
    Replies: 1
    Last Post: 09-06-2002, 07:21 PM
  5. Single-linked list help.
    By Nit in forum C Programming
    Replies: 3
    Last Post: 03-21-2002, 06:08 PM