Sheridan College Page 1 of 3
PROG20799: Assignments N3 Winter 2020
typedef struct node {
size_t id;
char* name;
float gpa;
struct node *next;
} student_t;
PROG20799: Data Structures and Algorithms in C
Evaluation: 10 points, 5% of your final grade.
Due date: See SLATE.
Late submission: 10% per day penalty, up to 3 days.
In this assignment you need to create a linked list of structures to store student’s info. Your
goal is to collect and sort students' records by GPA, and print them in a table-like output.
Please check the Demo version: https:
youtu.be/i-DKOCZVX7s
You must use a linked list and the following structure:
Requirements:
• Please download and use main.c.txt template file.
You must only use this template file with function prototypes or you’ll get a zero grade.
• You absolutely must use the structure mentioned above.
You’ll get a zero grade if you use anything else to store student’s info.
• Structures must be allocated on the HEAP. You’ll get a zero grade otherwise.
• You must set initial student id to 1000 and auto-increment it when a new student is added.
• You must use fgets() to get both the name and gpa of the student (check MAX_LEN macros).
• You must allocate student's name on the HEAP and use appropriate strto…() function to
convert input to floating-point value. Do NOT use scanf(), sscanf() or atof() anywhere!
• You must use Insertion into Sorted Linked List algorithm when you insert a new node (with
student’s info) into the Linked List: http:
abanski.com/files/prog20799/assignment3/
• You cannot modify the main() function given to you in the template file.
• You must use and implement the following functions (see template file):
student_t* createNode();
function to create the node
student_t* createList();
function to create the list
void displayList(…………..);
function to display the list
void insertNode(................);
function to insert node into the sorted list.
void removeList(................);
function to delete the linked list
• You must destroy the linked list and deallocate memory at the end of the program.
Please use function removeList(..) accordingly.
• Your program must work similarly to the Demo version posted on Youtube, ignore the inco
ect
input, have same e
or notifications, sort the records, etc.
• Your solution should have optimal time and space complexity.
https:
youtu.be/i-DKOCZVX7s
http:
abanski.com/files/prog20799/assignment3
Sheridan College Page 2 of 3
PROG20799: Assignments N3 Winter 2020
Hints:
1. The reason we want to use a linked list instead of an a
ay is because you can “sort” the linked
list during console input. I.e. you create a new node, get student’s GPA and insert the node
into the linked list in such a way that it is always sorted by students’ GPA.
A
ay of structures can't be used because you don't know how many students you have and
then you can't sort it on-the-fly as easy.
2. When you enter empty name then it is assumed there are no more nodes to add to the list.
3. Function createNode() must collect student’s info from the console, allocate memory on the
HEAP for the node and for the name, and return memory address of the node.
4. In the removeList() function you need to de-allocate memory for the name first and then for
the node itself.
5. You should allocate memory for student’s name on the HEAP using calloc() function.
Don’t forget to use strlen(name)+1 as size.
6. Do not forget about special cases in Step 5: http:
abanski.com/files/prog20799/assignment3/
7. Check the demo posted on YouTube. Demo input and output:
http:
abanski.com/files/prog20799/assignment3
Sheridan College Page 3 of 3
PROG20799: Assignments N3 Winter 2020
Submission:
This is an individual assignment. Even partially copied code will be subject to regulations against
academic integrity. Do NOT discuss your solution with anybody else.
Posting this assignment and/or solution on the Internet is a violation of the academic integrity policy.
• Please make sure your code is POSIX-compliant (works in NetBeans/Cygwin)!
• Please save main.c as a text with extension .txt
• You must upload main.txt file to the Assignment Dropbox by the due date.
• Your submission must be unique or have references.
• Please self-evaluate and self-grade your code in the comments section of the Dropbox.
• Late submissions are penalized 10% / day (1 point / day), up to -3 points.
Only submissions that are up to 3 days late will be accepted.
Grading Scheme:
• See Main requirements and also Course_Introduction.pdf.
Deductions will be applied if partial functionality is provided.
• You’ll get zero grade if your code doesn’t compile.
• Any compilation warning is a major mistake.
• "Debugging code" or commented out "old code" is a minor mistake.
• Please check assignment and submissions requirements for any additional deductions.
PROG20799: Data Structures and Algorithms in C
Assignment N3 Template
#include
#include
#include
#define FLUSH stdin=freopen(NULL,"r",stdin)
#define MAX_LEN 50
typedef struct node {
char *name;
float gpa;
size_t id;
struct node *next;
} student_t;
You must use the prototypes below. Do NOT declare any other functions!
You must fix the prototypes below: Make sure you use the co
ect parameter(s)
student_t* createNode();
function to create the node
student_t* createList();
function to create the list
void displayList(..........);
function to display the list
void insertNode(................);
function to insert node into the sorted list.
void removeList(................);
function to delete the linked list
int main() {
printf("\nCreating List of students:\n\n");
student_t *head = createList();
displayList(head);
removeList(&head);
return 0;
}
Your code goes below!!
Declare the functions according to prototypes.
Exercise 8.2: Template
#include
#include
#include
#define FLUSH stdin=freopen(NULL,"r",stdin)
#define MAX_LEN 50
typedef struct node {
char name[MAX_LEN];
struct node *next;
} student_t;
eturn HEAD of the linked list
student_t* createList();
create node of the linked list
student_t* createNode();
prints the linked list
void displayList(student_t*);
enters another value at the end of the linked list
void pushBack(student_t**);
enters another value at the beginning of the linked list
void pushFront(student_t**);
emove the first element of the linked list
void removeFirst(student_t**);
emove the last element of the linked list
void removeLast(student_t**);
emove any item in the linked list by its index
void removeByIndex(student_t**, int);
--------------------------------------
int main() {
printf("== Creating List:\n");
student_t* head = createList();
printf("== PushBack:\n");
pushBack(&head);
printf("== PushFront:\n");
pushFront(&head);
printf("== Printing Linked List:\n");
displayList(head);
printf("== Remove first:\n");
removeFirst(&head);
displayList(head);
printf("== Remove last:\n");
removeLast(&head);
displayList(head);
printf("== Remove by index:\n");
removeByIndex(&head, 1);
displayList(head);
return 0;
}
student_t* createList() {
student_t* head = NULL, *new_node, *cu
ent = NULL;
while ((new_node = createNode()) != NULL) {
XXXXXXXXXXif (cu
ent == NULL) {
XXXXXXXXXXhead = new_node;
} else {
XXXXXXXXXXcu
ent->next = new_node;
}
XXXXXXXXXXcu
ent = new_node;
}
return head;
}
student_t* createNode() {
student_t* record = NULL;
char tmp[MAX_LEN] = {0};
printf("Please enter name: ");
fgets(tmp, sizeof (tmp), stdin);
FLUSH;
We get rid of '\n' character at the end :
tmp[strcspn(tmp, "\n")] = 0;
if (strlen(tmp) > 1) {
XXXXXXXXXXrecord = (student_t*) malloc(sizeof (student_t));
XXXXXXXXXXif (record == NULL) {
XXXXXXXXXXprintf("Cannot allocate memory for person!");
XXXXXXXXXXexit(1);
}
XXXXXXXXXXstrncpy(record->name, tmp, sizeof (record->name));
XXXXXXXXXXrecord->next = NULL;
this MUST be done!
}
Returns NULL if no record!
return record;
}
void displayList(student_t* head) {
if (head == NULL) {
XXXXXXXXXXprintf("Linked list is empty\n");
} else {
XXXXXXXXXXstudent_t* cu
ent = head;
XXXXXXXXXXint i = 1;
XXXXXXXXXXwhile (cu
ent != NULL) {
XXXXXXXXXXprintf("%d. %s\n", i, cu
ent->name);
XXXXXXXXXXcu
ent = cu
ent->next;
XXXXXXXXXXi++;
}
}
}
void pushBack(student_t** head) {
if (*head == NULL) {