DreamsCoder.com

We Code your Dreams


home > Singly Link List program in C along with code - [Explained in Video]




Singly Link List program in C along with code - [Explained in Video]









Source code






You can Download code here - 
http://dreamscoder.com/examples/singlyll.c
OR 
Click above on Source code

    #include
    #include

    struct node {
        int data;
        struct node *next;
    } *head, *temp, *curr, *left;

/* To add new node in Link list */

    void add(int num) {
        int i=0;
        temp = (struct node *) malloc(sizeof(struct node));
        temp->data = num;

            if(head == NULL) {
                head = temp;
                head->next = NULL;
            }
            else
            {
                temp->next = head;                
                head = temp;
            }

    }//add

/* To add new node in Link list after the
 specified location */

    void add_after(int num, int loc)
    {
        int i=0;
        temp = (struct node *) malloc(sizeof(struct node));
        temp->data = num;
        curr = head;
            if(head == NULL) {
                    head = temp;
                    head->next = NULL;
            }
           else {
                for(i=0; inext;
                }

                left->next = temp;
                temp->next = curr;
            }//else
    }

/* To append new node in Link list */

    void append(int data) {

        temp = (struct node *) malloc(sizeof(struct node));
        temp->data = data;
        curr = head;

        if(curr == NULL) {
            head = temp; 
            head->next = NULL;
        }
        
        while(curr->next != NULL) {
            curr = curr->next;
        }
        curr->next = temp;
        curr = temp;
        curr->next = NULL;
    }

/* To delete node in Link list which contains the 
specified number */

    void delete(int num) {
        curr = head;

        if(curr == NULL) {
            printf("Link List is empty");
        }
        else
        {
            while(curr != NULL) {
               
                if(curr->data == num) {

                    if(curr == head) {
                    head = curr->next;
                    free(curr);
                    break;
                }
                    left->next = curr->next;
                    free(curr);
                    break;
                }
                else {
                    left = curr;
                    curr = curr->next;
                }
            } //while loop
        }
    }

/* To delete the entire Link list */

    void deleteAll() {
        curr = head;
        if(curr == NULL) {
            printf("Link list is empty");
        }
        else {
            while(curr != NULL) {
                left = curr->next;
                free(curr);
                curr = left;
            }
            head = NULL;
            printf("Deleted Link list");
        }
    }

/* To display all nodes in Link list */

    void disp() {
        if(head == NULL) {
            printf("Link list is empty");
            return;
        }
        else { 
            curr = head;
                 printf("Data is");
                while(curr != NULL) {
                    printf("%d	 ",curr->data);
                    curr = curr->next;
                } //while
            }//else
        }


    int main() {

        int choice;
        int num,loc;

        while(1)
        {
            printf("1. Add");
            printf("2. Add after");
            printf("3. Append");
            printf("4. Delete");
            printf("5. Delete All");
            printf("6. Display");
            printf("7. Exit");
            printf("---------------------");
            printf("Enter choice");
            scanf("%d",&choice);
             printf("");
            

        switch(choice) {
            case 1: printf("Enter data");
                    scanf("%d",&num);
                    add(num);
                    break;
            case 2: printf("Enter data and location ");
                    scanf("%d%d",&num,&loc);
                    add_after(num,loc);
                    break;
            case 3: printf("Enter the data");
                    scanf("%d",&num);
                    append(num);
                    break;
            case 4: printf("Enter the number to delete ");
                    scanf("%d",&num);
                    delete(num);
                    break;
            case 5: deleteAll();
                    break;
            case 6: disp();
                    break;
            case 7: return 0;
            
            default: printf("Incorrect choice");
                    break;

        }
    }//while

        return 0;
    }

Label - data-structure





by - Chinmay Mahajan





Are you looking for something else?


Suggested Programs




Simple code in C for Binary tree traversal: Preorder, Inorder and Postorder


Printing the singly linked list in reverse order using recursion in C


Breadth first search in C Language


Program To show whether expression have matching parenthesis or not using C++


Dynamic Queue program using C++