1
\$\begingroup\$

Can someone give me any suggestions over my code that I made for this task? Also how much of 10 would rate it ?

Suppose a simply linked list whose node is of type node and contains a student's name, registration number, and grade(vathmos), such as shown below:

struct node
{
char name [30];
int ΑΜ;
float vathmos;
struct node *next;
};

. Write a consonant

struct node *delete (struct node *head)

which runs through the list that starts at the head and deletes the penultimate one node of the list that starts at the head.

. Write a function

display (struct node *head) 

which runs through all the items in the list that starts at the head, from the beginning to the end, and for each node with a score from 0.0 up to 4.9 displays on the screen name, registration number and grade. It basically prints its data students who do not pass the course.

ATTENTION: You do NOT need to write the main () function and create it list. We believe that our list has already been created, some have already been registered items and we have a pointer to the beginning of the list.

 struct node{
 char name [30];
 int AM;
 float vathmos;
 struct node *next;
};

struct node *delete (struct node *head){
 struct node *temp = head;
 struct node *temp1;
 if(temp == NULL){
     return head;
 }else if(temp->next == NULL){
     return head;
 }else if(temp->next->next == NULL){
     head = temp->next;
     free(temp);
     return head;
 }else{
     while(temp->next->next->next != NULL){
         temp = temp->next;
     }
     temp1 = temp->next;
     temp->next = temp->next->next;
     free(temp1);
     return head;
 }
}

void display (struct node * head){
 struct node *temp = head;
 while(temp != NULL){
     if(temp->vathmos <= 4.9){
        printf("NAME: %s\nAM: %d\nVathmos:%f\n", temp->name, temp->AM, temp->vathmos);
     }
     temp = temp->next;
 }
}

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Welcome to Code Review! The current question title, which states your concerns about the code, is too general to be useful here. Please edit to the site standard, which is for the title to simply state the task accomplished by the code. Please see How to get the best value out of Code Review: Asking Questions for guidance on writing good question titles. \$\endgroup\$ – Toby Speight Jun 5 at 9:07
  • \$\begingroup\$ Rikos Sina, "how much of 10 would rate it ?" --> Use an auto-formatter for the code. It saves time and makes your code more understandable and professional. \$\endgroup\$ – chux - Reinstate Monica Jun 5 at 17:38
2
\$\begingroup\$
  • I suspect that the code works. However, most people will frown at ->next->next->next. If you'd need to delete the third from last (or 6th, or 42nd), what would you do?

    Consider writing a more generic function,

      struct node * delete(struct node * head, size_t distance_from_end)
    

    A small hint: having a helper function struct node * advance(struct node *, size_t) will be very beneficial.

  • Since we don't know how the list was constructed, we shouldn't blindly free the deleted node. Are we sure it was indeed dynamically allocated?

  • Technically testing for temp->vathmos <= 4.9 is wrong. The problem statement asks for nodes with a score from 0.0 up to 4.9, that is nodes with negative score should not be displayed. Ask your teacher for clarification on what to do with negative scores.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks you for your opinion , it helps a lot @vnp \$\endgroup\$ – Rikos Sina Jun 5 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.