need help with this program (student information manager with linkedlist)

this code stores information about students (with linked list) , i need to add these abilities to it :

1.add and remove course name and get the grade of the course
2.calculating the GPA of students based on their grades
3.search for a specific student



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170


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

#define TRUE 1
#define FALSE 0


struct node {                                                               /* node structure definition */
   int id;
   char name[20];
   float gpa;
   struct node *next;
};

typedef struct node * nodeptr;                                  /* data type for node ptrs */

nodeptr addnode(nodeptr list);                                /* allocate memory for new node */
nodeptr insert(nodeptr list, nodeptr p);                   /* insert a node */
int deletenode(nodeptr *,int);                                   /* locate & delete a node */
void printlist(nodeptr);                                               /* print list contents */

int main(void)
{

   nodeptr list = NULL;                                               /* pointer to list */
   int del;                                                                      /* student to be deleted from list */
   int sel;                                                                      /* menu selection */

   while(true)
   {
                        printf("\nMenu:\n");
                        printf("1 - Add a Student\n");
                        printf("2 - Delete a Student\n");
                        printf("3 - Print Students\n");
                        printf("4 - Exit\n");
                        printf("Make your selection ->");
                        fflush(stdin);
                        scanf("%d", &sel);
                        printf("\n");
                        switch(sel)
                        {
                                    case 1:
                                                list=addnode(list);
                                                break;

                                    case 2:
                                                printf("Enter Student ID to delete a student -> ");
                                                scanf("%d",&del);

                                                if (!deletenode(&list,del))      /* try to delete node, return flag */
                                                {
                                                            printf("Student ID %d was not on the list\n", del);
                                                }
                                                else
                                                {
                                                            printf("%d has been deleted\n",del);
                                                }
                                                break;

                                    case 3:
                                                printlist(list);
                                                break;

                                    case 4:
                                                printf("Bye!\n");
                                                exit(1);

                                    default:
                                                printf ("Invalid selection.  Please try again.\n\n\n");
                                                break;
                        }
   }
   return 0;
}


nodeptr addnode(nodeptr list)
{
   /* This function allocates memory for new node*/
   nodeptr p;

   if ((p = (nodeptr)malloc(sizeof(struct node))) != NULL)
            {
             p->next = NULL;                   /* initialize next to NULL */
             printf("Please enter student ID: ");
             fflush(stdin);
             scanf("%d", &p->id);
             printf("Please enter student name: ");
             fflush(stdin);
             gets(p->name);
             printf("Please enter student GPA: ");
             fflush(stdin);
             scanf("%f", &p->gpa);

             list = insert(list,p);                                         /* save p into list */
    }
    else
            {                                                                       /* else insufficient memory */
             printf("Insufficient memory to build entire list.\n");
            }
            return(list);
}

nodeptr insert(nodeptr list, nodeptr p)
{

   nodeptr q;

   if ((list == NULL) || (list->id >= p->id)) {                 /* does p go first? */
      p->next = list;                                                        /* put p on front */
      list = p;
   } else {                                                                                  /* else search for spot */
      q = list;                                                                              /* use q to locate spot */
      while ((q->next != NULL) && (q->next->id < p->id))
             q = q->next;
      p->next = q->next;                                                           /* attach back of list to p */
      q->next = p;                                                          /* attach front of list to p */
   }
   return(list);
}

int deletenode(nodeptr *list,int target)
{
  

   nodeptr p,q;                                                            /* p - target, q - predecessor */
   int found = FALSE;                                                /* flag if target found */

   if ((*list)->id == target)
   {                                                                                /* if target is at head of list */
      p = *list;                                                                /* p points to target node */
      *list = (*list)->next;                                               /* update external pointer */
      found = TRUE;                                                     /* set flag - target found */
   }
   else
   {                                                                                /* else use loop to search list */
      q = *list;
      while ((q->next != NULL) && (q->next->id < target))
              {
                          q = q->next;                                      /* stop if found or target not there*/
              }
      if (q->next != NULL)
              {                                                                     /* will be true if target not on list*/
         if (q->next->id == target)
                         {                                                          /* is this the target? */
                                    found = TRUE;                       /* set flag - target found */
                                    p = q->next;                            /* p points to target node */
                                    q->next = p->next;                 /* reattach remainder of list to q */
         }
      }
   }
   if (found)
      free(p);                                                                  /* free memory space if necessary */
   return(found);                                                          /* return flag to main */
}


void printlist(nodeptr list)
{
   /* Print all info about all students */
   while (list != NULL) {
      printf("ID: %d \n",list->id);
              printf("Name: %s \n",list->name);
              printf("GPA: %f \n\n",list->gpa);
      list = list->next;
   }
}
Last edited on
Topic archived. No new replies allowed.