skip to Main Content

Trouble with single linked list

So I was doing the removing node for single linked list, I made 3 functions but somehow it created infinite data although it deletes the data I need. Here is my code:

Node delHead(Node head){
    if(head==NULL){
        printf("There is nothing to delete!");
    }
    else{
        head=head->next;
    }
    return head;
}

Node delTail(Node head){
    if(head==NULL||head->next==NULL){
        return delHead(head);
    }else{
        Node p = head;
        Node prev = NULL;
        while(p->next !=NULL){
            prev=p;
            p=p->next;
        }
        prev->next= NULL;
        free(p);
        return head;
    }
}
Node delAt(Node head, int position){
    if(position == 0 || head == NULL || head->next == NULL){
        head = delHead(head); 
    }else{
        int k = 0;
        Node p = head;
        Node prev = NULL;
        while(p != NULL && k != position){
            prev = p;
            p=p->next;
            k++;
        }
        if(k != position){
            head = delTail(head);
        }else{
            prev->next=p->next;
            free(p);
        }
    }
    return head;
}

I’m using visual studio code, so I hope someone can fix me with this. I try p->next->next but it didn’t work out so appreciate you guys so much. Thank you!

2

Answers


  1. There seems to be an issue with your code in the delAt function. The problem lies in the condition if (k != position). This condition checks whether the loop terminated because k reached the desired position. However, the loop could also terminate if p becomes NULL before reaching the desired position. In that case, you should delete the tail instead of the node at the specified position.

    To fix this issue, you can modify the delAt function as follows:

    Node delAt(Node head, int position) {
        if (position == 0 || head == NULL || head->next == NULL) {
            head = delHead(head);
        } else {
            int k = 0;
            Node p = head;
            Node prev = NULL;
            while (p != NULL && k != position) {
                prev = p;
                p = p->next;
                k++;
            }
            if (p == NULL) {
                head = delTail(head);
            } else {
                prev->next = p->next;
                free(p);
            }
        }
        return head;
    }
    

    In the modified code, the condition if (p == NULL) is used to check if the loop terminated because p became NULL. In that case, we delete the tail of the list using the delTail function.

    I hope this helps to resolve the issue you were facing.

    Login or Signup to reply.
  2. As mentioned in another comment, you should have provided an example of how you code and the output it gave you. I only noticed 1 issue, you were using a Node instead of a Node* for your paramaters, return values, and local variables. I changed the code and validated that your original functions were correct.

    Tip 1: You can only use the arrow notation StructPtr->variable if you are referencing a pointer, otherwise you have to use the dot notation Struct.variable

    Tip 2: You can only set a pointer equal to NULL. This means that you can set Node* node to NULL, but you cannot set "Node node" equal to NULL

    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct Node{
        int data;
        struct Node* next;
    }Node;
    
    Node* delHead(Node* head);
    Node* delTail(Node* head);
    Node* delAt(Node* head, int position);
    void displayNode(Node* head, char* type);
    
    int main()
    {
        Node* head = malloc(sizeof(Node));
        Node* tmpNext = head;
        
        for(int i = 9; i >= 0; i--){
            tmpNext->data = i;
            tmpNext->next = malloc(sizeof(Node));
            tmpNext = tmpNext->next;
        }
        tmpNext->next = NULL;
        
        displayNode(head, "Original");
        
        tmpNext = head;
        tmpNext = delHead(tmpNext);
        displayNode(tmpNext, "Delete Head");
        
        tmpNext = head;
        tmpNext = delTail(tmpNext);
        displayNode(tmpNext, "Delete Tail");
        
        tmpNext = head;
        tmpNext = delAt(tmpNext, 3);
        displayNode(tmpNext, "Delete at 3");
    
        return 0;
    }
    
    
    Node* delHead(Node* head){
        if(head==NULL){
            printf("There is nothing to delete!");
        }
        else{
            head=head->next;
        }
        return head;
    }
    
    Node* delTail(Node* head){
        if(head==NULL||head->next==NULL){
            return delHead(head);
        }else{
            Node* p = head;
            Node* prev = NULL;
            while(p->next !=NULL){
                prev=p;
                p=p->next;
            }
            prev->next= NULL;
            free(p);
            return head;
        }
    }
    
    Node* delAt(Node* head, int position){
        if(position == 0 || head == NULL || head->next == NULL){
            head = delHead(head); 
        }else{
            int k = 0;
            Node* p = head;
            Node* prev = NULL;
            while(p != NULL && k != position){
                prev = p;
                p=p->next;
                k++;
            }
            if(k != position){
                head = delTail(head);
            }else{
                prev->next=p->next;
                free(p);
            }
        }
        return head;
    }
    
    void displayNode(Node* head, char* type){
        printf("n%s:n", type);
        while(head->next != NULL){
            printf("[%d]", head->data);
            head = head->next;
            if(head->next != NULL)
                printf(" -> ");
        }
        printf("n");
    }
    
    

    Output:

    Original:
    [9] -> [8] -> [7] -> [6] -> [5] -> [4] -> [3] -> [2] -> [1] -> [0]
    
    Delete Head:
    [8] -> [7] -> [6] -> [5] -> [4] -> [3] -> [2] -> [1] -> [0]
    
    Delete Tail:
    [9] -> [8] -> [7] -> [6] -> [5] -> [4] -> [3] -> [2] -> [1]
    
    Delete at 3:
    [9] -> [8] -> [7] -> [5] -> [4] -> [3] -> [2] -> [1]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search