Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^10: [OT:] Is this Curriculum right?

by karlgoethebier (Abbot)
on Dec 09, 2021 at 11:05 UTC ( [id://11139496]=note: print w/replies, xml ) Need Help??


in reply to Re^9: [OT:] Is this Curriculum right?
in thread [OT:] Is this Curriculum right?

Thank you very much for your kind reply.

The kid performed once more. And as you might have noticed already my C skills aren’t so good. I think the code he provided isn’t really good. I fear he simply cheated it from somewhere out there. If you still have the patience take a look…

#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; void reverseList(struct Node** headref); void _delLesserNodes(struct Node* head); void delLesserNodes(struct Node** head_ref) { reverseList(head_ref); _delLesserNodes(*head_ref); reverseList(head_ref); } void _delLesserNodes(struct Node* head) { struct Node* current = head; struct Node* maxnode = head; struct Node* temp; while (current != NULL && current->next != NULL) { if (current->next->data < maxnode->data) { temp = current->next; current->next = temp->next; free(temp); } else { current = current->next; maxnode = current; } } } void push(struct Node** head_ref, int new_data) { struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); new_node->data = new_data; new_node->next = *head_ref; *head_ref = new_node; } /* Utility function to reverse a linked list */ void reverseList(struct Node** headref) { struct Node* current = *headref; struct Node* prev = NULL; struct Node* next; while (current != NULL) { next = current->next; current->next = prev; prev = current; current = next; } *headref = prev; } void printList(struct Node* head) { while (head != NULL) { printf("%d ", head->data); head = head->next; } printf("\n"); } int main() { struct Node* head = NULL; push(&head, 33); push(&head, -6); push(&head, 77); push(&head, -4); push(&head, 7); push(&head, 9); push(&head, 76); push(&head, 2); printf("Given Linked List \n"); printList(head); delLesserNodes(&head); printf("Modified Linked List \n"); printList(head); return 0; }

Best regards, Karl

«The Crux of the Biscuit is the Apostrophe»

Replies are listed 'Best First'.
Re^11: [OT:] Is this Curriculum right?
by Marshall (Canon) on Dec 13, 2021 at 02:04 UTC
    Well the code does run.

    Of course the first thing missing is short description of what the code does.
    I had to run it to see what it did.
    like perhaps:

    /* Makes a linked list with new items inserted at the front. The list is then searched to find the last item (the item first inserted) and then the list is modified to remove all entries with data less than that item. The list is printed after all insertions and again after the deletions. */
    The code does demo some basic understanding of linked lists.
    However there is some bizarre stuff...
    reverseList() is not at all necessary (unless that was part of the problem assignment).
    If that was part of the assignment, then I would expect that there would have been a requirement to print the reversed list.

    The algorithm to delete all items less than the last one, traverses the list 3 times. Once to reverse the list to find the "last"; process list to delete items less than the "last"; reverse the list again.
    It is only necessary to traverse the list twice. Once to find the "tail". And once to "shorten" the list. reverseList() appears to be unnecessary code.

    Update: If anybody out there wants to take on a significant C freeware project, I am interested in re-coding agrep, a approximate matching version of grep. The speed of this thing is stunning. The algorithms are excellent and generated 2 Phd's. Unfortunately, the authors weren't very good C coders and the software has issues. This thing will occasionally miss matches that it should have found. The last I heard from the maintainer, a complete re-write is in order.

      «… some bizarre stuff...»

      Yes. I told the kid something similar. One of his teachers which is more ambitious and very kind told him as well. You told him. I guess some insights need a little while. Or let us call it maturity. Is this «Reife»? Thanks for advice and best regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11139496]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-03-30 03:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found