0% found this document useful (0 votes)
20 views26 pages

How To C

This document discusses how to create a database to store student information including name, student number, and marks in a linked list data structure. It outlines the key functions needed: find a student, add a student to the start or end of the list, print the list, remove a student, change a mark, and sort the list by mark. The main tasks are to create a student structure, find function, add functions to add to the start or end of the list while checking for duplicates, print function, remove function, change mark function, and sort function. Pseudocode and explanations are provided for each function.

Uploaded by

Rémy Thijssen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views26 pages

How To C

This document discusses how to create a database to store student information including name, student number, and marks in a linked list data structure. It outlines the key functions needed: find a student, add a student to the start or end of the list, print the list, remove a student, change a mark, and sort the list by mark. The main tasks are to create a student structure, find function, add functions to add to the start or end of the list while checking for duplicates, print function, remove function, change mark function, and sort function. Pseudocode and explanations are provided for each function.

Uploaded by

Rémy Thijssen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Contents

Explanation ........................................................................................................................... 2
1. The start ......................................................................................................................... 6
1.1. Starting off ............................................................................................................... 6
1.2. Creating the main function....................................................................................... 6
2. Find function................................................................................................................... 8
3. Add function ................................................................................................................... 9
3.1. General ................................................................................................................... 9
3.2. Add to start .............................................................................................................11
3.3. Add to end ..............................................................................................................12
4. Print function .................................................................................................................14
5. Remove function ...........................................................................................................15
6. Change Mark function ...................................................................................................18
7. Sort function ..................................................................................................................19
Entire code ...........................................................................................................................21

1
Explanation
We are going to create a database that can store students (including their name and student
number) and store their marks. The students can be added to and removed from the list.
Besides that, their marks can be changed, and the list can be sorted on marks from low to
high.
Command Explanation
a add a student
b add a student to the start of the linked list
e add a student to the end of the linked list
p print an overview of all students
r remove a student from the linked list
c change a student’s mark
s sort the students on marks from low to high

Task 0 – Create a structure


Must be able to store the name (type char*), student number (type int), the student’s mark
(type float) and a pointer to the next element in the list
Task 1 – Find function
Returns a pointer to the student if it is found, else it returns NULL)
Task 2 – Add a student to the start of the list
Don’t add if name and student ID already exist and print a message that the student already
exists
Task 3 – Add a student to the end of the list
Don’t add if name and student ID already exist and print a message that the student already
exists
Task 4 – Print the list of students
Should say that the list is empty if it is
Task 5 – Remove student
Should say if a student is not found
Task 6 – Change mark
Should say if a student is not found
Task 7 – Sort on mark
Sort from low to high
On the next 3 pages you can see what the end result looks like.
Note that for adding we ask a second question, so therefore we use an indentation of 1 space
here.

2
3
4
5
1. The start
1.1. Starting off
First of all, we include the most common libraries and then we define our structure student. In
the structure we have to store the name, the student number and the mark the student got.
These are of type char*, int and float respectively. Besides that, we need a pointer to the next
element in the linked list. To make the typing easier, we use typedef to give the structure
another name that will replace struct student in the code (Student).

1. #include <stdio.h>
2. #include <stdlib.h>
3. #include <string.h>
4. #include <math.h>
5.
6. typedef struct student {
7.
8. char *name;
9. int number;
10. float mark;
11. struct student *next;
12.
13. } Student; // typing "Student" is shorter than "struct student"

1.2. Creating the main function


After that, we create our main function, in which we will deal with the user inputs. When creating
a new function, you can add a (few) comment(s) above the function, briefly describing what it
does. In our main function we first of all declare a number of variables, to be used later in the
function. We should declare the following things:

 An initial structure to store our linked list. Always set this equal to NULL, because
otherwise weird things can happen;
 A character to store the user’s command input;
 A string (array of characters) to store the student’s name given as input. Make sure
there is enough space for a long name. Set the array on for instance 50 or 100;
 An integer to store the student number that is given as an input;
 A floating point number to store the student’s mark.
Later on, when extending our code, we might need to add more variables, but this is a good
start.
After having declared our variables, we write the code that repeatedly asks the user to give an
input, until the user presses ‘q’ (for ‘quit’). There are multiple ways to do this and I think by now
you have found a method that suits you best. Some options:

 Use while(command != ‘q’) { … }


 Set an integer loop equal to 1 and use while(loop == 1) { … case ‘q’: loop = 0; }
Then we have to check what we should do with this command, for which we will use a switch-
statement. It is recommended to when you write down “case:” you immediately add “break;”
on the line below, to make sure you do not forget it later on. The same applies for any function:
make sure to type a closing curly bracket right away to make sure you do not forget it. As you
know, “one curlyboy without a friend is stronger than a million lines of code”.

6
1. void main(void) {
2.
3. Student *list = NULL;
4.
5. char command;
6. char adder;
7.
8. char name[100];
9. int number;
10. float mark;
11.
12. while(command != 'q') {
13. printf("Select a command (a/p/r/c/s/q) ");
14. scanf(" %c", &command);
15.
16. switch(command) {
17.
18. }
19.
20. }
21. }

With every function we create, we obviously should update our main function accordingly.
Steps you should remember:

 Include all libraries from the cheat sheet;


 In the structure you use type char *name, and in the main function char name[100].

7
2. Find function
We have to create a function to find a student: Student *findStudent(Student *list, char *name,
int number). If the student is found, a pointer to the student should be returned. If it is not found,
the function should return NULL. To find the student, we need to iterate through the linked list
until we find an item that has the same name and same student number. This can be done
using a while loop.

1. Student *temp = list;


2.
3. while (temp != NULL) {
4. // Check if name and number are equal
5. temp = temp->next; // Go to next element
6. }

To check whether the name is the same as the name of the element from temp, we use the
standard function strcmp that compares the two strings. To check if the numbers are the same,
we can simply compare them with ==. The code to check whether the name and number are
the same is then:

1. if (strcmp(temp->name, name) == 0 && number == temp->number) { }

The only thing left then is to add the return pointers. If the student is found, temp should be
returned. If there is no match, NULL should be returned. The entire function then is:

1. Student *findStudent(Student *list, char *name, int number) {


2.
3. Student *temp = list;
4.
5. while (temp != NULL) {
6. if (strcmp(temp->name, name) == 0 && number == temp->number) {
7. return temp;
8. }
9.
10. temp = temp->next;
11. }
12.
13. return NULL;
14.
15. }

Steps you should remember:

 Iterate through all elements with temp != NULL and temp = tempnext;
 Compare using strcmp for strings/names and == for numbers;
 Return temp if they give a match with the input name;
 Return NULL in other cases.

8
3. Add function
3.1. General
We have to create two functions, one to add a student to the start of the linked list and one to
add to the end: Student *addToStart(Student *list, char *name, int number, float mark) and
Student *addToEnd(Student *list, char *name, int number, float mark). (At the test you only
have to create one, but here both will be discussed.)
In your main function you should ask the user for input. This will be done using printf and scanf.
For the different types of data there are different ways to ask for input. Below is a short overview
of the four most commonly used in Computation, with scanf(scanning part, saving part) :
Type Scanning part Saving part
String (array of letters) " %[^\n]s" stringName
Integer " %i" &integerName
Float " %f" &floatName
Character " %c" &characterName

The first space in the scanning part is to skip all white space the user enters, before his actual
input. The [^\n] part for the string makes sure that the input is read until the newline (\n)
character. A string is stored in a regular variable (that is why we later have to allocate memory),
and the character, float and integer are stored in a location, hence the &.

1. // Mind the indentation of 1 space at the questions here!


2.
3. case 'a':
4.
5. printf(" Name? ");
6. scanf(" %[^\n]s", name);
7. printf(" Number? ");
8. scanf(" %i", &number);
9. printf(" Mark? ");
10. scanf(" %f", &mark);
11.
12. break;

Now there are two more things we need to take into account:
1. A person should not be added twice. Therefore we need to check (using the find
function) if the student already exists and if it does, we should not add it;
2. Should we add to the beginning or the end of the list?
The first problem can be solved using an if-statement. The find function returns NULL if there
is no match and it returns a pointer to the student if there is a match. So we should call the
function to add the student to the list only if findStudent(list, name, number) == NULL. Else we
should print “This student already exists.”
The second problem can be solved by asking the user. Then we can use – just like when
asking for commands – use a switch statement. Combining all gives the following:

1. // Mind the indentation of 1 space at the questions here!


2.
3. case 'a':
4. printf(" Add to start or end? (b/e) ");
5. scanf(" %c", &adder);
6.
7. printf(" Name? ");
8. scanf(" %[^\n]s", name);

9
9. printf(" Number? ");
10. scanf(" %i", &number);
11. printf(" Mark? ");
12. scanf(" %f", &mark);
13.
14. switch(adder) {
15.
16. case 'b':
17. if(findStudent(list, name, number) == NULL) {
18. list = addToStart(list, name, number, mark);
19. }
20.
21. else {
22. printf("This student already exists.\n");
23. }
24.
25. break;
26.
27. case 'e':
28. if(findStudent(list, name, number) == NULL) {
29. list = addToEnd(list, name, number, mark);
30. }
31.
32. else {
33. printf("This student already exists.\n");
34. }
35.
36. break;
37.
38. }
39.
40. break;

In the adding functions we copy this information into list items.


First of all, we create a new, initial structure to store our new student. To make sure nothing
goes wrong, we have to set it to NULL first. The function is of type Student * as well.

1. Student *addToStart(Student *list, char *name, int number, float mark) {


2.
3. Student *newStudent = NULL;
4.
5. }

Secondly, we have to allocate memory for the newStudent and the name of the newStudent.
The required space for the structure is equal to its size, so sizeof(Student). For the name, we
have to allocate size for the length of the name, and the terminating NULL-character, so
strlen(name) + 1. After that we have to copy all the data to the structure. For the name (which
is a string) we have to use the function strcpy(target, text). For the number and mark we can
simply use =.

1. Student *addToStart(Student *list, char *name, int number, float mark) {


2.
3. Student *newStudent = NULL;
4.
5. // Create memory space for the new structure and the name
6. newStudent = (Student *) malloc(sizeof(Student));
7. newStudent->name = (char *) malloc(strlen(name) + 1);
8.
9. // Set the values of the new student equal to the input values
10. strcpy(newStudent->name, name);
11. newStudent->number = number;

10
12. newStudent->mark = mark;
13.
14. }

Steps you should remember:

 What to use when asking for a type of input;


 Only add if the student does not exist yet, so if the find function returns NULL;
 Use ([struct]* ) malloc(sizeof([struct])) to allocate memory for a list item;
 Use (char* ) malloc(strlen([name] + 1)) to allocate memory for the name;
 Copy all input to the list items (using strcpy for names and = for numbers).

3.2. Add to start

Figure 1: Adding to the start of a linked list.

As you can see in Figure 1, the item following the newly created item (its next item) should be
the original linked list. So newnext = list. Then we should return the new.

1. Student *addToStart(Student *list, char *name, int number, float mark) {


2.
3. Student *newStudent = NULL;
4.
5. // Create memory space for the new structure and the name
6. newStudent = (Student *) malloc(sizeof(Student));
7. newStudent->name = (char *) malloc(strlen(name) + 1);
8.
9. // Set the values of the new student equal to the input values
10. strcpy(newStudent->name, name);
11. newStudent->number = number;
12. newStudent->mark = mark;
13.
14. // Set the next pointer of the new element to the original structure, so it is a
dded in front
15. newStudent->next = list;
16.
17. // Return the complete list
18. return newStudent;
19. }

11
This function also works if the list was empty initially, since the next-item of a list can be NULL.
Steps you should remember:

 Set the original list as the next element for the new item;
 Return the new item.

3.3. Add to end

Figure 2: Adding to the end of a linked list.

Adding to the end of a linked list is slightly more difficult than adding to the start of a list,
because adding to the end of NULL is not possible (segmentation faults will happen). Therefore
you first need to check whether the original list is empty (NULL) or not. If it is, you can simply
set the next-pointer of newStudent to NULL. Then we have to set the list to the newStudent.
If the list is not empty, we have to iterate through the linked list until we have found the end.
This can be done using a temporary which we set equal to the list. The end is where tempnext
== NULL, and while that is not the case, we have to move to the next item. If we have reached
it, we have to set tempnext to the newStudent, as can be seen in the Figure above. To be
completely sure we still set newStudentnext to NULL. In the end, we have to return the list. The
function code eventually becomes the following:

1. Student *addToEnd(Student *list, char *name, int number, float mark) {


2.
3. Student *newStudent = NULL;
4.
5. // Create memory space for the new structure and the name
6. newStudent = (Student *) malloc(sizeof(Student));
7. newStudent->name = (char *) malloc(strlen(name) + 1);
8.
9. // Set the values of the new student equal to the input values
10. strcpy(newStudent->name, name);
11. newStudent->number = number;
12. newStudent->mark = mark;
13.
14. if(list == NULL) {
15.
16. newStudent->next = NULL;;
17. list = newStudent;

12
18.
19. }
20.
21. else {
22.
23. Student *temp = list;
24.
25. while(temp->next != NULL) {
26. temp = temp->next;
27. }
28.
29. temp->next = newStudent;
30. newStudent->next = NULL;
31.
32. }
33.
34. return list;
35.
36. }

Steps you should remember:

 Check if the list is empty, if it is, set the next-pointer to NULL and the list to the new
item;
 Iterate through the list to the last item and set its next-pointer to the new item and the
next-pointer of the new item to NULL;
 Return the list.

13
4. Print function
The print function does not return anything, so is of type void and it takes as input only the
linked list. The function therefore is void printList(Student *list). Initially we create a
temporary again, which we set equal to the list.
After that, we have to check whether the list is empty or not. If it is empty, it should print “The
list is empty.”
If the list is not empty, we should iterate through the list and print the data for each element in
the list, as shown in the first picture in the document, which can be done with temp[data].
Besides that, we should print the element’s number (1, 2, 3, …). So first we print the data, and
then we go to the next list element and increment our counter. We have to repeat this as long
as the element is not NULL (in other words, when we have finished printing the entire list). The
function then looks like this:

1. void printList(Student *list) {


2.
3. Student *temp = list;
4.
5. int i = 1;
6.
7. if(temp == NULL) {
8. printf("The list is empty!\n");
9. }
10.
11. while(temp != NULL) {
12.
13. printf("Student %d:\n", i);
14. printf("- Name: %s\n", temp->name);
15. printf("- Number: %d\n", temp->number);
16. printf("- Mark: %.1f\n", temp->mark);
17.
18. temp = temp->next;
19. i++;
20.
21. }
22.
23. }

Steps you should remember:

 Check if the list is empty and if it is, print that it is empty;


 Iterate through the list and print each element, until the last item (temp != NULL).

14
5. Remove function
The function Student *removeStudent(Student *list, char *name, int number) removes one
student from the list and prints an error message if the student does not exist.
For removing we should – just like when adding the student – first ask the user for input and
then check whether the student exists. However, the difference between them is that we should
remove the student if it is found and print an error message if he is not found (“Student [name]
([studentnumber]) not found.”). The code for in the main function then becomes:

1. case 'r':
2.
3. printf("Name? ");
4. scanf(" %[^\n]s", name);
5. printf("Number? ");
6. scanf(" %i", &number);
7.
8. if(findStudent(list, name, number) != NULL) {
9. list = removeStudent(list, name, number);
10. }
11. else {
12. printf("Student %s (%i) not found.\n", name, number);
13. }
14.
15. break;

When creating the function to remove an item from the list, there are two cases which we have
to consider:
1. The item to be removed being the first item, or
2. The item to be removed being any other item in the list.
Why these two cases? Because if the first item is just removed, we will have nothing (NULL)
pointing at the second item in the list, which will not work. For all other, non-first, items, we will
always have something point at it.
So first of all, we create a temporary and a ‘bridge’, where the temp is equal to the given list
and the bridge (here called prev) is equal to NULL to avoid weird things from happening.

Figure 3: Removing from the beginning of a linked list.

In the first case, we check whether the name and the number of the student to be removed
match the name and number of the first item. Besides that, we need to check whether the list
is not empty (and if we don’t, we will get a segmentation fault).

15
If this is all true, then we have to set the start of our list to the next element (tempnext), and
then free up the space we allocated for the name and then the list-item itself (in this order,
because segmentation faults). After that we set the temp back to the list.

1. Student *removeStudent(Student *list, char *name, int number) {


2.
3. Student *temp = list;
4. Student *prev = NULL;
5.
6. while(temp != NULL && strcmp(temp->name, name) == 0 && temp-
>number == number){
7. list = temp->next;
8. free(temp->name);
9. free(temp);
10. temp = list;
11. }
12.
13. }

Figure 4: Removing from anywhere but the beginning of a linked list.

For removing in any other place in the list, we have to iterate through the list again, but only
moving on to the first item after having checked whether the current item is not the one that
has to be removed. If it is not the one that has to be removed, we set the bridge (which acts
as the previous item to the one that has to be removed) equal to the current item, and then set
the temporary to the next item. If we continue to do this until we have a match, then temp is
the item that has to be removed (#2 in Figure 4) and prev is the item previous to that (#1).
In addition to that, we need to check whether the item to be removed is NULL (already). If that
is the case, we have to return the regular list we got as input. (And leaving out this will cause
segmentation faults as well, again.)
Next, we set the next-element for prev to the element that would originally come after temp,
which is #3. So prevnext = tempnext and then they are connected as shown in Figure 4.
If it is the last element in the list which will be removed, then tempnext is equal to NULL. If
this is the case, we can return the list.
In the end, the space has to be freed, just like explained above. However, now we need to set
the temp to prevnext to join the ends together.
The entire remove-function then turns out to be the following:

1. Student *removeStudent(Student *list, char *name, int number) {


2.
3. Student *temp = list;

16
4. Student *prev = NULL;
5.
6. while(temp != NULL && strcmp(temp->name, name) == 0 && temp-
>number == number){
7. list = temp->next;
8. free(temp->name);
9. free(temp);
10. temp = list;
11. }
12.
13.
14. while(temp != NULL){
15. while(temp != NULL && strcmp(temp->name, name) != 0 && temp-
>number != number){
16. prev = temp;
17. temp = temp->next;
18. }
19.
20.
21. if(temp == NULL){
22. return list;
23. }
24.
25.
26. prev->next = temp->next;
27. if(temp->next == NULL){
28. return list;
29. }
30.
31. free(temp->name);
32. free(temp);
33. temp = prev->next;
34. }
35.
36. }

Steps you should remember:

 Create a ‘bridge’ and set it to NULL initially;


 Check if the item to be removed is the first element. If it is, set the list equal to the
second element. Then free the allocated space. Set the temp equal to the list;
 If it is not the first item:
o Iterate through the list until the correct item is found. Set the bridge equal to this
item and the temp to the next item;
o Check if temp is equal to NULL and if it is, return the list;
o Set the next-pointer of the bridge to the next-pointer of temp (if that is NULL,
return the list);
o Free the allocated space;
 Do not forget to use while(temp != NULL) for both!

17
6. Change Mark function
The function Student *changeMark(Student *list, char *name, int number, float newMark) will
change the original mark of the student by another one, which the user gives as input.
In the main function we start off by asking the user for the student’s name and number, and of
course the new mark, which will replace the old one. Once we have this information, we have
to check whether the student exists, because if we want to change a student’s mark, we first
have to check whether this is actually a student. This is done in exactly the same way as we
did in the remove function, because we should do something if the student is found (so the find
function does not return NULL).

1. case 'c':
2.
3. printf("Name? ");
4. scanf(" %[^\n]s", name);
5. printf("Number? ");
6. scanf(" %i", &number);
7. printf("New mark? ");
8. scanf(" %f", &mark);
9.
10. if(findStudent(list, name, number) != NULL) {
11. list = changeMark(list, name, number, mark);
12. }
13. else {
14. printf("Student %s (%i) not found.\n", name, number);
15. }
16.
17. break;

Then in the function we have to iterate through the list again like we did in the find function.
The difference now is that if we have found the student, we should replace the mark of the
item. This can simply be done by tempmark = newMark. When we are done, we should
return the original list.

1. Student *changeMark(Student *list, char *name, int number, float newMark) {


2.
3. Student *temp = list;
4.
5. // Iterates through the linked list
6. while (temp != NULL) {
7. if (strcmp(temp->name, name) == 0 && number == temp->number) {
8. temp->mark = newMark;
9. }
10.
11. temp = temp->next;
12. }
13.
14. return list;
15. }

Steps you should remember:

 Iterate through the entire list;


 Set tempmark to the new mark the user has given as input;
 Return the list.

18
7. Sort function
The only thing we need to sort our list, is the list itself. Calling the function is easy:

1. case 's':
2.
3. list = sortStudents(list);
4.
5. break;

At first we create a temporary which is equal to the original list and two structures that are
equal to NULL that we will use for the sorting.
The easiest way of sorting is probably bubblesort. We have to loop over the list a number of
times, and for this we need 2 loops. (Instead of for-loops you can also use while-loops, if you
prefer them.)
The outer loop starts at temp and continues until the last element (tempnext). The inner loop
starts at the second element (tempnext) and continues until the last element as well.
As we want to sort from low to high, we check inside the inner loop if the first element of the
two to be compared is greater than the last one. If it is not, then they are sorted already like
we want them, so do not do anything. If it is, then we have to swap the two elements. You can
swap the list elements like Kees once explained, but if you do not like pointers very much, it is
probably easier to copy the data like described below in the code.
In the end, we return the temporary.

1. Student *sortStudents(Student *list) {


2.
3. Student *temp = list;
4.
5. Student *i = NULL;
6. Student *j = NULL;
7.
8. for(i = temp; i->next != NULL; i=i->next) {
9.
10. for(j = i->next; j != NULL; j=j->next) {
11.
12. if(i->mark > j->mark) {
13.
14. char *swapName;
15. int swapNumber;
16. float swapMark;
17.
18. swapName = i->name;
19. swapNumber = i->number;
20. swapMark = i->mark;
21.
22. i->name = j->name;
23. i->number = j->number;
24. i->mark = j->mark;
25.
26. j->name = swapName;
27. j->number = swapNumber;
28. j->mark = swapMark;
29.
30. }
31.
32. }
33.
34. }

19
35.
36. return temp;
37.
38. }

Steps you should remember:

 In the outer loop, iterate from the first element to the last element;
 In the inner loop, iterate from the second element to the last element;
 Check in the inner loop if the first item is greater than the second item, and if it is,
swap them.

20
Entire code

howtoc.c
Download:

1. #include <stdio.h>
2. #include <stdlib.h>
3. #include <string.h>
4. #include <math.h>
5.
6. typedef struct student {
7.
8. char *name;
9. int number;
10. float mark;
11. struct student *next;
12.
13. } Student; // typing "Student" is shorter than "struct student"
14.
15. /*
16. * Find function
17. * If the item is found, a pointer to that item is returned
18. * If it is not found, NULL is returned
19. */
20.
21. Student *findStudent(Student *list, char *name, int number) {
22.
23. // At first we create a temp to make sure nothing will be changed to our origina
l list
24. Student *temp = list;
25.
26. // Iterates through the linked list
27. // If there is a match, a pointer is returned to the struct where the match is
28. while (temp != NULL) {
29. if (strcmp(temp->name, name) == 0 && number == temp->number) {
30. return temp;
31. }
32.
33. temp = temp->next;
34. }
35.
36. return NULL;
37.
38. }
39.
40. /*
41. * Add To Start Function
42. * Adds a student to the start of the linked list
43. *
44. */
45.
46. Student *addToStart(Student *list, char *name, int number, float mark) {
47.
48. Student *newStudent = NULL;
49.
50. // Create memory space for the new structure and the name
51. newStudent = (Student *) malloc(sizeof(Student));
52. newStudent->name = (char *) malloc(strlen(name) + 1);
53.
54. // Set the values of the new student equal to the input values
55. strcpy(newStudent->name, name);
56. newStudent->number = number;
57. newStudent->mark = mark;

21
58.
59. // Set the next pointer of the new element to the original structure, so it is a
dded in front
60. newStudent->next = list;
61.
62. // Return the complete list
63. return newStudent;
64. }
65.
66. /*
67. * Add To End Function
68. * Adds a student to the end of the linked list
69. *
70. */
71.
72. Student *addToEnd(Student *list, char *name, int number, float mark) {
73.
74. Student *newStudent = NULL;
75.
76. // Create memory space for the new structure and the name
77. newStudent = (Student *) malloc(sizeof(Student));
78. newStudent->name = (char *) malloc(strlen(name) + 1);
79.
80. // Set the values of the new student equal to the input values
81. strcpy(newStudent->name, name);
82. newStudent->number = number;
83. newStudent->mark = mark;
84.
85. if(list == NULL) {
86.
87. newStudent->next = NULL;;
88. list = newStudent;
89.
90. }
91.
92. else {
93.
94. Student *temp = list;
95.
96. while(temp->next != NULL) {
97. temp = temp->next;
98. }
99.
100. temp->next = newStudent;
101. newStudent->next = NULL;
102.
103. }
104.
105. return list;
106.
107. }
108.
109. /*
110. * Remove Student Function
111. * Removes a student from the list
112. *
113. */
114.
115. Student *removeStudent(Student *list, char *name, int number) {
116.
117. Student *temp = list;
118. Student *prev = NULL;
119.
120. while(temp != NULL && strcmp(temp->name, name) == 0 && temp-
>number == number){
121. list = temp->next;

22
122. free(temp->name);
123. free(temp);
124. temp = list;
125. }
126.
127.
128. while(temp != NULL){
129. while(temp != NULL && strcmp(temp->name, name) != 0 && temp-
>number != number){
130. prev = temp;
131. temp = temp->next;
132. }
133.
134.
135. if(temp == NULL){
136. return list;
137. }
138.
139.
140. prev->next = temp->next;
141. if(temp->next == NULL){
142. return list;
143. }
144.
145. free(temp->name);
146. free(temp);
147. temp = prev->next;
148. }
149.
150. }
151.
152. /*
153. * Change Mark Function
154. * Changes the mark of a student
155. *
156. */
157.
158. Student *changeMark(Student *list, char *name, int number, float newMark) {
159.
160. Student *temp = list;
161.
162. // Iterates through the linked list
163. while (temp != NULL) {
164. if (strcmp(temp->name, name) == 0 && number == temp->number) {
165. temp->mark = newMark;
166. }
167.
168. temp = temp->next;
169. }
170.
171. return list;
172. }
173.
174. /*
175. * Print Function
176. * Prints the information of each student
177. *
178. */
179.
180. void printList(Student *list) {
181.
182. Student *temp = list;
183.
184. int i = 1;
185.
186. if(temp == NULL) {

23
187. printf("The list is empty!\n");
188. }
189.
190. while(temp != NULL) {
191.
192. printf("Student %d:\n", i);
193. printf("- Name: %s\n", temp->name);
194. printf("- Number: %d\n", temp->number);
195. printf("- Mark: %.1f\n", temp->mark);
196.
197. temp = temp->next;
198. i++;
199.
200. }
201.
202. }
203.
204. /*
205. * Sort Function
206. * Sorts students on marks from low to high
207. *
208. */
209.
210. Student *sortStudents(Student *list) {
211.
212. Student *temp = list;
213.
214. Student *i = NULL;
215. Student *j = NULL;
216.
217. for(i = temp; i->next != NULL; i=i->next) {
218.
219. for(j = i->next; j != NULL; j=j->next) {
220.
221. if(i->mark > j->mark) {
222.
223. char *swapName;
224. int swapNumber;
225. float swapMark;
226.
227. swapName = i->name;
228. swapNumber = i->number;
229. swapMark = i->mark;
230.
231. i->name = j->name;
232. i->number = j->number;
233. i->mark = j->mark;
234.
235. j->name = swapName;
236. j->number = swapNumber;
237. j->mark = swapMark;
238.
239. }
240.
241. }
242.
243. }
244.
245. return temp;
246.
247. }
248.
249.
250.
251. void main(void) {
252.

24
253. Student *list = NULL;
254.
255. char command;
256. char adder;
257.
258. char name[100];
259. int number;
260. float mark;
261.
262. while(command != 'q') {
263. printf("Select a command (a/p/r/c/s/q) ");
264. scanf(" %c", &command);
265.
266. switch(command) {
267.
268. case 'a':
269.
270. printf(" Add to start or end? (b/e) ");
271. scanf(" %c", &adder);
272.
273. printf(" Name? ");
274. scanf(" %[^\n]s", name);
275. printf(" Number? ");
276. scanf(" %i", &number);
277. printf(" Mark? ");
278. scanf(" %f", &mark);
279.
280. switch(adder) {
281.
282. case 'b':
283. if(findStudent(list, name, number) == NULL) {
284. list = addToStart(list, name, number, mark);
285. }
286.
287. else {
288. printf("This student already exists.\n");
289. }
290.
291. break;
292.
293. case 'e':
294. if(findStudent(list, name, number) == NULL) {
295. list = addToEnd(list, name, number, mark);
296. }
297.
298. else {
299. printf("This student already exists.\n");
300. }
301.
302. break;
303.
304. }
305.
306. break;
307.
308. case 'p':
309.
310. printList(list);
311.
312. break;
313.
314. case 'r':
315.
316. printf("Name? ");
317. scanf(" %[^\n]s", name);
318. printf("Number? ");

25
319. scanf(" %i", &number);
320.
321. if(findStudent(list, name, number) != NULL) {
322. list = removeStudent(list, name, number);
323. }
324. else {
325. printf("Student %s (%i) not found.\n", name, number);
326. }
327.
328. break;
329.
330. case 'c':
331.
332. printf("Name? ");
333. scanf(" %[^\n]s", name);
334. printf("Number? ");
335. scanf(" %i", &number);
336. printf("New mark? ");
337. scanf(" %f", &mark);
338.
339. if(findStudent(list, name, number) != NULL) {
340. list = changeMark(list, name, number, mark);
341. }
342. else {
343. printf("Student %s (%i) not found.\n", name, number);
344. }
345.
346. break;
347.
348. case 's':
349.
350. list = sortStudents(list);
351.
352. break;
353.
354. }
355.
356. }
357. }

26

You might also like