Linked List - C Code - 14 Operations

Updated mayankjoin 0 Tallied Votes 1K Views Share

A complete C implementation of Linked List...tested on Linux GCC

#include <stdio.h>
#include <stdlib.h>

struct linked_list
{
    int data;
    struct linked_list *next ;
} ;

typedef struct linked_list NODE ;

NODE * getnode();
NODE * insert_at_beg(NODE *head ,int num);
NODE * insert_at_end(NODE *head , int num);
void insert_after_nth_node(head , node_num , num);
void display(NODE *head);
void delete_after_nth_node(head , node_num);
NODE * insert_before_nth_node(NODE *head , int node_num , int num);
NODE * delete_before_nth_node(NODE *head , int node_num , int num);
NODE * insert_after_element_id(NODE *head ,  int element , int num);
NODE * delete_after_element_id(NODE *head , int element);
NODE * insert_before_element_id(NODE *head , int element , int num);
NODE * delete_before_element_id(NODE *head , int element);
NODE * delete_at_beg(NODE *head);
NODE * delete_at_end(NODE *head);
void count_elements(NODE *head);

void main()
{
    NODE *head = NULL ;
    int input ;
    int num , node_num ,element;




    while(1)
    {

        printf("\n 1. Insert At Begin \n");
        printf("\n 2. Delete At Begin \n");
        printf("\n 3. Insert At End \n");
        printf("\n 4. Delete At End \n");
        printf("\n 5. Insert after nth node\n");
        printf("\n 6. Delete after nth node\n");
        printf("\n 7. Insert before nth node\n");
        printf("\n 8. Delete before nth node\n");
        printf("\n 9. Insert after element ID \n");
        printf("\n 10. Delete after element ID \n");
        printf("\n 11. Insert before element ID \n");
        printf("\n 12. Delete before element ID \n");
        printf("\n 13. Count total elements in Linked-List\n");
        printf("\n 14. Display The Linked-List\n");
        printf("\n 15. Exit\n");

        printf("\n Please type the appropriate option: \n\n");

        scanf("%d" , &input);

        switch(input)
        {
        case 1:
            //1. Insert At Begin
            printf("\nEnter number u wish to insert at start \n");
            scanf("%d" , &num);
            head = insert_at_beg(head , num);
            break;

        case 2:
            //   2. Delete At Begin
            head = delete_at_beg(head);
            break;


        case 3:
            // 3. Insert At End
            printf("\nEnter number u wish to insert at end \n");
            scanf("%d" , &num);
            head = insert_at_end(head , num);
            break;

        case 4:
            //  4. Delete At End

            head = delete_at_end(head);
            break;


        case 5:
            // 5. Insert after nth node
            printf("\n Enter the node number after which you wish to insert \n");
            scanf("%d" , &node_num);
            printf("\nEnter number u wish to insert  \n");
            scanf("%d" , &num);
            insert_after_nth_node(head , node_num , num);
            break;

        case 6:
            //  6. Delete after nth node
            printf("\n Enter the node number after which you wish to delete \n");
            scanf("%d" , &node_num);
            delete_after_nth_node(head , node_num);
            break;

        case 7:
            //  7. Insert before nth node
            printf("\n Enter the node number before which you wish to insert \n");
            scanf("%d" , &node_num);
            printf("\nEnter number u wish to insert  \n");
            scanf("%d" , &num);
            head = insert_before_nth_node(head , node_num , num);
            break;

        case 8:
            //  8. Delete before nth node
            printf("\n Enter the node number before which you wish to delete \n");
            scanf("%d" , &node_num);
            if(node_num == 1)
                printf("\n Not posible to delete before head node\n");
            else if (node_num == 2)
            {
                NODE *q;
                q = head ;
                head = head->next ;
                free(q);
            }
            else
            {
                head =  delete_before_nth_node(head , node_num , num);
            }

            break;

        case 9:
            //   9. Insert after element ID
            printf("\n The list is \n");
            display(head);
            printf("\n \n");
            printf("\n Enter the element after which the node is to be inserted \n");
            scanf("%d" , &element);
            printf("\nEnter number u wish to insert  \n");
            scanf("%d" , &num);
            head = insert_after_element_id(head , element , num);
            break;

        case 10:
            // 10. Delete after element ID
            printf("\n The list is \n");
            display(head);
            printf("\n \n");
            printf("\n Enter the element after which the node is to be deleted \n");
            scanf("%d" , &element);
            head = delete_after_element_id(head , element);
            break;

        case 11:
            // 11. Insert before element ID
            printf("\n The list is \n");
            display(head);
            printf("\n \n");
            printf("\n Enter the element ID before which the node is to be inserted \n");
            scanf("%d" , &element);
            printf("\nEnter number u wish to insert  \n");
            scanf("%d" , &num);
            head = insert_before_element_id(head , element , num);
            break;

        case 12:
            // 12. Delete before element ID
            printf("\n The list is \n");
            display(head);
            printf("\n \n");
            printf("\n Enter the element ID before which the node is to be deleted \n");
            scanf("%d" , &element);
            head = delete_before_element_id(head , element);
            break;

        case 13:
            // 13. Count total elements in Linked-List
            count_elements(head);
            break;

        case 14:
            // 14. Display The Linked-List
            display(head);
            break;



        case 15:
            //15. Exit
            exit(1);


        default:
            printf("\n Please Enter Correct Choice\n");
        }

    }


}

NODE * getnode()
{
    NODE *create ;
    create = (NODE *)(malloc(sizeof(NODE)));
    create->next = NULL ;
    return create;
}

NODE * insert_at_beg(NODE *head ,int num)
{
    NODE *makenode  ;
    makenode = getnode();

    if(head == NULL)
    {
        makenode->data = num;
        head = makenode;
    }

    else
    {
        makenode->data = num;
        makenode->next = head;
        head = makenode;
    }

    return head;


}

NODE *delete_at_beg(NODE *head)
{
    if (head == NULL)
    {
        printf("\n Deleting Not Possible \n");
    }


    else
    {
        NODE *q;
        q = head ;

        head=head->next;
        free(q);

    }

    return head ;
}

NODE * delete_at_end(NODE *head)
{
    if (head == NULL)
    {
        printf("\n Deleting Not Possible, List Empty \n");
    }

    else if (head->next == NULL )
    {
        free(head);
        return NULL;
    }
    else
    {
        NODE *curr = head , *prev;

        while(curr->next != NULL)
        {
            prev = curr;
            curr = curr->next ;
        }

        prev->next = NULL;
        free(curr);

    }
    return head;
}


NODE *insert_at_end(NODE *head , int num)
{
    NODE *makenode;
    NODE *head1 = head ;
    NODE *prev = NULL , *curr = NULL ;
    curr = head ;

    if(head==NULL)
    {

        makenode = getnode();
        makenode->data = num ;
        makenode->next  = NULL ; //actually makenode next is already made null in getnode
        head = makenode ;
        return head ;

    }


    while(curr != NULL)
    {
        prev = curr ;
        curr = curr->next ;
    }

    makenode = getnode();
    makenode->data = num ;
    makenode->next  = NULL ; //actually makenode next is already made null in getnode
    prev->next = makenode ;
}


void insert_after_nth_node(NODE *head , int node_num , int num)
{
    NODE *q;
    int count = 1 ;
    q = head;
    NODE *makenode ;

    while(count != node_num && q != NULL )
    {
        q=q->next ;
        count++;
    }

    if(q == NULL)
    {
        printf("\n Invalid Position Given \n");
        return;
    }

    else
    {
        makenode = getnode() ;
        makenode->data = num ;
        makenode->next = q->next ;
        q->next = makenode ;

    }

}

void delete_after_nth_node(NODE *head , int node_num)
{
    NODE *q , *next_q;
    q = head;

    int count = 1;

    while(count != node_num && q != NULL )
    {
        count++;
        q = q->next;
    }

    if (q == NULL || q->next == NULL)
    {
        printf("\n Invalid Position Given OR Last node specified\n");
        return;
    }

    else
    {
        next_q = q->next;
        q->next = next_q->next;
        free(next_q);
    }
}

NODE * insert_before_nth_node(NODE *head , int node_num , int num)
{
    NODE *q;
    int count = 1;
    NODE *curr , *prev ;
    curr = head ;

    if(node_num == 1)
    {
        // implies insert at the 1st node i.e. head
//                  printf("\n Comes Here \n");
//                  head = insert_at_beg(head , num);
//                  display(head);accessing

        q = getnode();
        q->data = num ;
        q->next = head ;
        head = q;

    }

    else
    {


        while (count != node_num && curr != NULL)
        {
            prev = curr ;
            curr = curr->next;
            count++;
        }

        // prev is node after which we need to insert
        //curr is the succedding node after our insert

        if (curr == NULL)
        {
            printf("\n Invalid Position Given \n");
            return head;
        }


        q = getnode();
        q->data = num ;
        prev->next = q ;
        q->next = curr ;

    }

    return head ;

}

NODE * delete_before_nth_node(NODE *head , int node_num , int num)
{
    int count = 1 ;
    NODE *curr , *prev ;
    curr = head ;



    while (count != node_num-1 && curr != NULL)  // node_num-1 since curr will point to node to be deleted
    {
        prev = curr ;
        count++ ;
        curr = curr->next ;
    }

    if (count <= node_num )
    {
        prev->next = curr->next ;
        free(curr);
    }

    else
    {
        printf("\n Invalid Position Given \n");
    }

    return head ;




}

NODE * insert_after_element_id(NODE *head , int element , int num)
{
    NODE *curr , *prev ;
    curr = head ;

    while( curr->data != element && curr != NULL)
    {
        prev = curr ;
        curr = curr->next ;
        if(curr == NULL)  // to prevent segmentation fault ; if the element is not present , the list is traversed till last,
            // and when curr = null , curr->data has segmentation fault error .
            break;
    }

    if(curr == NULL)
    {
        printf("\n Invalid Element ID Given \n");
    }
    else
    {
        NODE *q;
        q= getnode();
        q->data = num ;
        q->next = curr->next ;
        curr->next = q;
    }

    return head ;


}

NODE * delete_after_element_id(NODE *head , int element)
{
    NODE *curr = head ;
    NODE *adv = curr->next;

    while(curr->data != element && curr != NULL)
    {

        curr= curr->next ;
        adv = curr->next;
        if(curr == NULL)
            break ;
    }

    if(curr == NULL)
        printf("\n Invalid Element Given \n");

    else
    {
        curr->next = adv->next ;
        free(adv);
    }

    return head ;

}

NODE * insert_before_element_id(NODE *head , int element , int num)
{
    NODE *curr = head ,  *prev ;
    NODE *q;
    int count = 1;

    while (curr->data != element && curr != NULL)
    {
        prev= curr ;
        count++;
        curr = curr->next ;
        if(curr == NULL)
            break ;
    }

    if (count==1)
    {
        head = insert_at_beg(head , num);
        return head ;
    }

    else if (curr == NULL)
        printf("\n Invalid Element Given \n");

    else
    {
        q = getnode();
        q->data = num ;
        prev->next = q;
        q->next  = curr ;

    }

    return head ;
}

NODE * delete_before_element_id(NODE *head , int element)
{
    NODE *curr =  head , *prev , *pre_prev;

    int count = 1 ;
    int found = 0;

    while( curr->data != element && curr->next != NULL)
    {
        if(curr->data == element)
            found = 1;

        count++;
        pre_prev = prev ;
        prev = curr ;
        curr = curr->next;
        if(curr == NULL)
            break;
    }


    if(count == 1)
    {
        printf("\n Cannot delete before head node \n");
        return head;
    }
    else if (count == 2)
    {
        head = curr ;
        free(prev);
        return head ;
    }
    else if (curr == NULL || found == 0)
    {
        printf("\n Invalid Element Given \n");
        return head ;
    }
    else
    {
        pre_prev->next = curr;
        free(prev);
        //prev = curr ;
    }
    return head ;

}


void count_elements(NODE *head)
{
    NODE *q;
    int count = 0 ;
    q = head ;

    if(q == NULL)
        printf("\n Empty List -> 0 Elememts \n");

    while (q != NULL)
    {
        count++;
        q=q->next;

    }

    printf("\n  List has %d Elememts \n" , count);
}


void display(NODE *head)
{
    NODE *q;
    q = head;

    if(q == NULL)
    {
        printf("\n  List Empty \n");
        return;
    }

    while(q != NULL)
    {
        if(q->next == NULL)
        {
            printf("%d" , q->data );
            break;
        }
        else
        {
            printf("%d-->" , q->data );
            q = q->next ;
        }
    }
}
D33wakar 36 Posting Whiz in Training

So , what's wrong with your program ?
since we're not a psychic or in the mood of playing the"Guess what's wrong with my code" game, you have to tell us what exactly are you looking for.

Narue 5,707 Bad Cop Team Colleague

So , what's wrong with your program ?

I think he's just showing it off.

mayankjoin 0 Unverified User

I am really sorry . i wanted to help others with this code of mine. it was not a show off. i was new to this, and hence i thought of helping. Sorry if i wasted your time .

THE ABOVE CODE IS A 100% WORKING CODE UNDER gcc in linux

Narue 5,707 Bad Cop Team Colleague

i wanted to help others with this code of mine. it was not a show off.

That's what I meant. You had working code and wanted to show it off with the intention of helping other beginners who might be struggling with linked lists.

Niranjana_1 0 Newbie Poster

good work dude :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.