Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added implementation for LinkedList in C #57

Merged
merged 1 commit into from
Oct 31, 2024
Merged

Conversation

Sanjeet4567
Copy link
Contributor

@Sanjeet4567 Sanjeet4567 commented Oct 31, 2024

Added the implementation for Linked List Data Structure in C Language

Summary by Sourcery

New Features:

  • Implement a basic linked list data structure in C, including functions for insertion, deletion, and searching.

Copy link
Contributor

sourcery-ai bot commented Oct 31, 2024

Reviewer's Guide by Sourcery

This PR implements a singly linked list data structure in C with basic operations. The implementation includes a Node structure and functions for insertion, deletion, searching, and printing. Memory management is handled using malloc for allocations and free for deallocations, with appropriate NULL checks throughout the code.

Class diagram for LinkedList implementation in C

classDiagram
    class Node {
        int val
        Node* next
    }
    class LinkedList {
        +Node* insertBegin(Node* head, int val)
        +Node* deleteBegin(Node* head)
        +Node* search(Node* head, int val)
        +Node* insertAfter(Node* head, int s, int val)
        +void printLL(Node* head)
        +Node* deleteAfter(Node* head, int val)
    }
Loading

File-Level Changes

Change Details Files
Implemented the basic linked list node structure and type definition
  • Defined a Node struct with an integer value and next pointer
  • Created a type alias 'LL' for the Node struct
C/LinkedList.c
Implemented insertion operations for the linked list
  • Added insertBegin function to add nodes at the start of the list
  • Added insertAfter function to add nodes after a specific value
  • Included memory allocation checks and error handling
C/LinkedList.c
Implemented deletion operations for the linked list
  • Added deleteBegin function to remove the first node
  • Added deleteAfter function to remove a node after a specific value
  • Included proper memory deallocation using free()
C/LinkedList.c
Added utility functions for list operations
  • Implemented search function to find a node with a specific value
  • Added printLL function to display the entire list
  • Included a main function with test cases
C/LinkedList.c

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Sanjeet4567 - I've reviewed your changes and found some issues that need to be addressed.

Blocking issues:

  • The search function misses checking the last node in the list due to incorrect loop condition (link)

Overall Comments:

  • The search() function has a bug - it doesn't check the last node in the list, which could lead to false negatives when searching for values in the final node.
  • Consider adding function documentation to explain the purpose, parameters, and return values of each function. This will improve maintainability and usability of the code.
Here's what I looked at during the review
  • 🔴 General issues: 1 blocking issue, 2 other issues
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

return head;
}
LL* temp = head;
while(temp->next!=NULL){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): The search function misses checking the last node in the list due to incorrect loop condition

Change the condition to while(temp != NULL) to ensure the last node is also checked

printf("NO Node found\n");
return head;
}
LL* node=temp->next;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: Missing NULL check for temp->next in deleteAfter() could cause segmentation fault

Add a check for temp->next == NULL before attempting to delete the node

};
LL* insertBegin(LL* head,int val){
LL* node =(LL*)malloc(sizeof(LL));
if(node==NULL){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): insertBegin() returns NULL on malloc failure, potentially losing the original list

Consider returning the original head pointer when malloc fails to preserve the existing list

@x0lg0n x0lg0n merged commit f60e974 into x0lg0n:main Oct 31, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants