Thread: delete function in singly linked list

  1. #1
    Registered User
    Join Date
    Dec 2013
    Location
    Cebu City, Philippines
    Posts
    14

    delete function in singly linked list

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    
    
    struct node
    {
        int data;
        struct node *next;
    };
    typedef struct node NODE;
    
    
    NODE *create()
    { 
        return NULL;
    }
        
    int insert(int val, NODE *List);
    void displayList();
    int Length(NODE *head);
    int deleteNode(NODE *List, int x);
    
    
    int main()
    {
        NODE *L = create();
        int val, p, choice, dp;
        char again;
        while(choice!=4)
        {
        clrscr();
        displayList(L);
        printf("\nMENU\n[1] Insert\n[2] View\n[3] Delete\n[4] Exit\n");
        printf("\nChoice? ");
        scanf("%d", &choice);
        switch(choice)
        {
            case 1:
            
                printf("\nValue to insert? ");
                scanf("%d", &val);
                L = insert(val, L);
                getch();
                break;
            
            case 2:
                displayList(L);
                getch();
                break;
            
            case 3:
                printf("\nValue to delete: ");
                scanf("%d", &dp);
                L = deleteNode(L, dp);
                getch();
                break;
                
            case 4:
                return 0;
                
            default:
                printf("\nINVALID INPUT!\n");
                break;
        }
        }
    }
    
    
    
    
    
    
    /***************************************************************/
    void displayList(NODE *List)
    {    
        if(List==NULL)
        {
            printf("There are no elements in the list!!");
        }
        else
        {
            while(List!=NULL)
            {
                printf ("%d -> ", List->data);
                List=List->next;
            }
        }
    
    
    }
    
    
    
    
    
    
    /***************************************************************/
    int insert(int val, NODE *List)
    {
        NODE *new;
        new = (NODE*)malloc(sizeof(NODE));
        if (new)
        {
            new -> data = val;
            new ->next = NULL;
            if(List==NULL)
            {
                List=new;
            }
            else
            {
                new -> next = List;
                List = new;
            }
        }
        printf("\nInsert successful!\n");
        return List;
        
    }
      
      
    /***************************************************************/
    int Length(NODE *head)
    {
        NODE *current = head;
        int count = 0;
        while (current != NULL)
        {
            count ++;
            current = current -> next;
            
        }
        
        return count;
    }
    
    
    int deleteNode(NODE *List, int x)
    {
        NODE *tmp;
        tmp = List;
        if(tmp==NULL)
            printf("List is empty!");
        else
        {
            while(tmp->data!=x)
            {
                tmp = tmp ->next;
            }
            free(tmp);
            tmp = tmp ->next;
        }
        return tmp;
    }
    The above code is a SLL.
    I have a problem in my deleteNode function.
    I can't delete the exact data on the list.

    Can anybody help me how to do this right?

    Thanks

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You need to find the node before the one that has the x. You will take that node and set it's next pointer to the x node's next pointer. then you can free() the x node and return.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with singly linked list
    By saldar05 in forum C Programming
    Replies: 6
    Last Post: 03-08-2013, 12:30 AM
  2. Function Free() does not delete the linked list?
    By hefese in forum C Programming
    Replies: 5
    Last Post: 08-11-2012, 07:39 AM
  3. singly linked list
    By right2001 in forum C Programming
    Replies: 3
    Last Post: 08-20-2009, 10:21 AM
  4. Delete Function in Doubly Linked List
    By Dampecram in forum C Programming
    Replies: 5
    Last Post: 11-15-2008, 04:30 PM
  5. Singly Linked List Help
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 09-08-2001, 01:20 PM