Sunday, September 10, 2017

Program to Implement Singly Linked List in C

/*==========================================================================================
 **
 **  File Name     : sll.c
 **  Creation Date : Sun 10 Sep 2017 05:30:03 PM IST
 **  Last Modified : Sun 10 Sep 2017 09:03:24 PM IST
 **  Compiler      : gcc
 **  Author        : Manoj Kumar Patra, Asst. Professor
 **  Organization  : Vignan University, Guntur, India.
 **
 **=========================================================================================*/
/*C Program to Implement Singly linked List*/

#include<stdio.h>
#include<stdlib.h>
struct node
{
    int info;
    struct node *next;
};

struct node *create_list(struct node *start);
void display(struct node *start);
void search(struct node *start, int data);
struct node *add_at_beg(struct node *start, int data);
struct node *add_at_end(struct node *start, int data);
struct node *add_after_node(struct node *start, int data, int item);
struct node *add_before_node(struct node *start, int data, int item );
struct node *add_at_position(struct node *start, int data, int pos);
struct node *del_at_beg(struct node *start);
struct node *del_at_end(struct node *start);
struct node *del_a_node(struct node *start, int data);
struct node *reverse(struct node *start);
int main()
{
    struct node *start=NULL;  
    int choice,data,item,pos;
    printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
    printf("= = = = = = = = = = Singly Linked List in C = = = = = = = = = =\n");
    while(1)
    {
        printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
        printf("1. Create Linked List\n");
        printf("2. Display Linked List\n");
        printf("3. Search Linked List\n");
        printf("4. Add at beginning\n");
        printf("5. Add at end\n");
        printf("6. Add after node\n");
        printf("7. Add before node\n");
        printf("8. Add at position\n");
        printf("9. Delete from beg\n");
        printf("10. Delete from end\n");
        printf("11. Delete a node\n");
        printf("12. Reverse Linked List\n");
        printf("13. Exit\n\n");
        printf("Enter your choice : ");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:
                start=create_list(start);
                break;
            case 2:
                display(start);
                break;
            case 3:
                printf("Enter the element to be searched: ");
                scanf("%d",&data);
                search(start,data);
                break;
            case 4:
                printf("Enter the element to be inserted: ");
                scanf("%d",&data);
                start=add_at_beg(start,data);
                break;
            case 5:
                printf("Enter the element to be inserted: ");
                scanf("%d",&data);
                start=add_at_end(start,data);
                break;
            case 6:
                printf("Enter the element to be inserted: ");
                scanf("%d",&data);
                printf("Enter the element after which to insert: ");
                scanf("%d",&item);
                start=add_after_node(start,data,item);
                break;
            case 7:
                printf("Enter the element to be inserted: ");
                scanf("%d",&data);
                printf("Enter the element before which to insert: ");
                scanf("%d",&item);
                start=add_before_node(start,data,item);
                break;
            case 8:
                printf("Enter the element to be inserted: ");
                scanf("%d",&data);
                printf("Enter the position at which to insert: ");
                scanf("%d",&pos);
                start=add_at_position(start,data,pos);
                break;
            case 9:
                start=del_at_beg(start);  
                break;
            case 10:
                start=del_at_end(start);  
                break;
            case 11:
                printf("Enter the element to be deleted: ");
                scanf("%d",&data);
                start=del_a_node(start, data);  
                break;
            case 12:
                start=reverse(start);
                break;
            case 13:
                printf("Program Terminated..!!\n");
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
                exit(1);
            default:
                printf("Oops.. Wrong choice !\n");
        }
    }
    printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n\n");
}

//1
struct node *create_list(struct node *start)
{
    int i,n,data;
    printf("Enter the number of nodes: ");
    scanf("%d",&n);
    start=NULL;
    if(n==0)
        return start;
    printf("Enter the element to be inserted: ");
    scanf("%d",&data);
    start=add_at_beg(start,data);
    for(i=2; i<=n; i++)
    {
        printf("Enter the element to be inserted: ");
        scanf("%d",&data);
        start=add_at_end(start,data);  
    }
    printf("Linked list created with %d nodes\n\n",n);
    return start;
}

//2
void display(struct node *start)
{
    struct node *p;
    if(start==NULL)
    {
        printf("List is empty\n");
        return;
    }
    p=start;
    printf("The Elements of the Linked List are:\n");
    while(p!=NULL)
    {
        printf("%d\t",p->info);
        p=p->next;
    }
    printf("\n\n");
}

//3
void search(struct node *start, int item)
{
    struct node *p=start;
    int pos=1;
    while(p!=NULL)
    {
        if(p->info==item)
        {
            printf("Item %d found at position %d\n", item, pos);
            return;
        }
        p=p->next;
        pos++;
    }
    printf("Item %d not found in list\n",item);
}

//4
struct node *add_at_beg(struct node *start, int data)
{
    struct node *tmp;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->info=data;
    tmp->next=start;
    start=tmp;
    return start;
}

//5
struct node *add_at_end(struct node *start, int data)
{
    struct node *p, *tmp;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->info=data;
    p=start;
    while(p->next!=NULL)
        p=p->next;
    p->next=tmp;
    tmp->next=NULL;
    return start;
}

//6
struct node *add_after_node(struct node *start, int data, int item)
{
    struct node *tmp, *p;
    p=start;
    while(p!=NULL)
    {
        if(p->info==item)
        {
            tmp=(struct node *)malloc(sizeof(struct node));
            tmp->info=data;
            tmp->next=p->next;
            p->next=tmp;
            return start;
        }
        p=p->next;
    }
    printf("%d not present in the list\n",item);
    return start;
}

//7
struct node *add_before_node(struct node *start, int data, int item)
{
    struct node *tmp, *p;
    if(start==NULL )
    {
        printf("List is empty\n");
        return start;
    }  
    /*If data to be inserted before first node*/
    if(item==start->info)
    {
        tmp=(struct node *)malloc(sizeof(struct node));
        tmp->info=data;
        tmp->next=start;
        start=tmp;
        return start;
    }  
    p=start;
    while(p->next!=NULL)
    {
        if(p->next->info==item)
        {
            tmp=(struct node *)malloc(sizeof(struct node));
            tmp->info=data;
            tmp->next=p->next;
            p->next=tmp;
            return start;
        }
        p=p->next;
    }
    printf("%d not present in the list\n",item);
    return start;
}

//8
struct node *add_at_position(struct node *start, int data, int pos)
{
    struct node *tmp, *p;
    int i;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->info=data;
    if(pos==1)
    {
        tmp->next=start;
        start=tmp;
        return start;
    }
    p=start;
    for(i=1; i<pos-1 && p!=NULL; i++)
        p=p->next;
    if(p==NULL)
        printf("There are less than %d elements\n",pos);
    else
    {
        tmp->next=p->next;
        p->next=tmp;
    }  
    return start;
}

//9
struct node *del_at_beg(struct node *start)
{
    struct node *tmp, *p;
    if(start==NULL)
    {
        printf("List is empty\n");
        return start;
    }
    tmp=start;
    start=start->next;
    free(tmp);
    printf("First node has been deleted..!\n");
    return start;
}

//10
struct node *del_at_end(struct node *start)
{
    struct node *pp, *p;
    p=start;
    while(p->next!=NULL)
    {
        pp=p;
        p=p->next;
    }
    pp->next=NULL;
    free(p);
    printf("Last node has been deleted..!\n");
    return start;
}

//11
struct node *del_a_node(struct node *start, int data)
{
    struct node *tmp, *p;
    if(start==NULL)
    {
        printf("List is empty\n");
        return start;
    }
    p=start;
    while(p->next!=NULL)
    {
        if(p->next->info==data) 
        {
            tmp=p->next;
            p->next=tmp->next;
            free(tmp);
            return start;
        }
        p=p->next;
    }
    printf("Element %d not found\n",data);
    return start;
}

struct node *reverse(struct node *start)
{
    struct node *prev, *ptr, *next;
    prev=NULL;
    ptr=start;
    while(ptr!=NULL)
    {
        next=ptr->next;
        ptr->next=prev;
        prev=ptr;
        ptr=next;
    }
    start=prev;
    return start;
}

OutPut:

 manoj@ubuntu:~/cp/linkedlist$ gcc sll.c -o sll
manoj@ubuntu:~/cp/linkedlist$ ./sll
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
= = = = = = = = = = Singly Linked List in C = = = = = = = = = =
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Create Linked List
2. Display Linked List
3. Search Linked List
4. Add at beginning
5. Add at end
6. Add after node
7. Add before node
8. Add at position
9. Delete from beg
10. Delete from end
11. Delete a node
12. Reverse Linked List
13. Exit

Enter your choice : 1
Enter the number of nodes: 3
Enter the element to be inserted: 4
Enter the element to be inserted: 6
Enter the element to be inserted: 8
Linked list created with 3 nodes

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Create Linked List
2. Display Linked List
3. Search Linked List
4. Add at beginning
5. Add at end
6. Add after node
7. Add before node
8. Add at position
9. Delete from beg
10. Delete from end
11. Delete a node
12. Reverse Linked List
13. Exit

Enter your choice : 2
The Elements of the Linked List are:
4    6    8   

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Create Linked List
2. Display Linked List
3. Search Linked List
4. Add at beginning
5. Add at end
6. Add after node
7. Add before node
8. Add at position
9. Delete from beg
10. Delete from end
11. Delete a node
12. Reverse Linked List
13. Exit

Enter your choice : 3
Enter the element to be searched: 6
Item 6 found at position 2
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Create Linked List
2. Display Linked List
3. Search Linked List
4. Add at beginning
5. Add at end
6. Add after node
7. Add before node
8. Add at position
9. Delete from beg
10. Delete from end
11. Delete a node
12. Reverse Linked List
13. Exit

Enter your choice : 4
Enter the element to be inserted: 10
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Create Linked List
2. Display Linked List
3. Search Linked List
4. Add at beginning
5. Add at end
6. Add after node
7. Add before node
8. Add at position
9. Delete from beg
10. Delete from end
11. Delete a node
12. Reverse Linked List
13. Exit

Enter your choice : 2
The Elements of the Linked List are:
10    4    6    8   

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Create Linked List
2. Display Linked List
3. Search Linked List
4. Add at beginning
5. Add at end
6. Add after node
7. Add before node
8. Add at position
9. Delete from beg
10. Delete from end
11. Delete a node
12. Reverse Linked List
13. Exit

Enter your choice : 5
Enter the element to be inserted: 20
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Create Linked List
2. Display Linked List
3. Search Linked List
4. Add at beginning
5. Add at end
6. Add after node
7. Add before node
8. Add at position
9. Delete from beg
10. Delete from end
11. Delete a node
12. Reverse Linked List
13. Exit

Enter your choice : 2
The Elements of the Linked List are:
10    4    6    8    20   

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Create Linked List
2. Display Linked List
3. Search Linked List
4. Add at beginning
5. Add at end
6. Add after node
7. Add before node
8. Add at position
9. Delete from beg
10. Delete from end
11. Delete a node
12. Reverse Linked List
13. Exit

Enter your choice : 6
Enter the element to be inserted: 30
Enter the element after which to insert: 4
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Create Linked List
2. Display Linked List
3. Search Linked List
4. Add at beginning
5. Add at end
6. Add after node
7. Add before node
8. Add at position
9. Delete from beg
10. Delete from end
11. Delete a node
12. Reverse Linked List
13. Exit

Enter your choice : 2
The Elements of the Linked List are:
10    4    30    6    8    20   

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Create Linked List
2. Display Linked List
3. Search Linked List
4. Add at beginning
5. Add at end
6. Add after node
7. Add before node
8. Add at position
9. Delete from beg
10. Delete from end
11. Delete a node
12. Reverse Linked List
13. Exit

Enter your choice : 7
Enter the element to be inserted: 40
Enter the element before which to insert: 4
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Create Linked List
2. Display Linked List
3. Search Linked List
4. Add at beginning
5. Add at end
6. Add after node
7. Add before node
8. Add at position
9. Delete from beg
10. Delete from end
11. Delete a node
12. Reverse Linked List
13. Exit

Enter your choice : 2
The Elements of the Linked List are:
10    40    4    30    6    8    20   

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Create Linked List
2. Display Linked List
3. Search Linked List
4. Add at beginning
5. Add at end
6. Add after node
7. Add before node
8. Add at position
9. Delete from beg
10. Delete from end
11. Delete a node
12. Reverse Linked List
13. Exit

Enter your choice : 8
Enter the element to be inserted: 50
Enter the position at which to insert: 5
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Create Linked List
2. Display Linked List
3. Search Linked List
4. Add at beginning
5. Add at end
6. Add after node
7. Add before node
8. Add at position
9. Delete from beg
10. Delete from end
11. Delete a node
12. Reverse Linked List
13. Exit

Enter your choice : 2
The Elements of the Linked List are:
10    40    4    30    50    6    8    20   

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Create Linked List
2. Display Linked List
3. Search Linked List
4. Add at beginning
5. Add at end
6. Add after node
7. Add before node
8. Add at position
9. Delete from beg
10. Delete from end
11. Delete a node
12. Reverse Linked List
13. Exit

Enter your choice : 9
First node has been deleted..!
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Create Linked List
2. Display Linked List
3. Search Linked List
4. Add at beginning
5. Add at end
6. Add after node
7. Add before node
8. Add at position
9. Delete from beg
10. Delete from end
11. Delete a node
12. Reverse Linked List
13. Exit

Enter your choice : 2
The Elements of the Linked List are:
40    4    30    50    6    8    20   

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Create Linked List
2. Display Linked List
3. Search Linked List
4. Add at beginning
5. Add at end
6. Add after node
7. Add before node
8. Add at position
9. Delete from beg
10. Delete from end
11. Delete a node
12. Reverse Linked List
13. Exit

Enter your choice : 10
Last node has been deleted..!
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Create Linked List
2. Display Linked List
3. Search Linked List
4. Add at beginning
5. Add at end
6. Add after node
7. Add before node
8. Add at position
9. Delete from beg
10. Delete from end
11. Delete a node
12. Reverse Linked List
13. Exit

Enter your choice : 2
The Elements of the Linked List are:
40    4    30    50    6    8   

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Create Linked List
2. Display Linked List
3. Search Linked List
4. Add at beginning
5. Add at end
6. Add after node
7. Add before node
8. Add at position
9. Delete from beg
10. Delete from end
11. Delete a node
12. Reverse Linked List
13. Exit

Enter your choice : 11
Enter the element to be deleted: 50
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Create Linked List
2. Display Linked List
3. Search Linked List
4. Add at beginning
5. Add at end
6. Add after node
7. Add before node
8. Add at position
9. Delete from beg
10. Delete from end
11. Delete a node
12. Reverse Linked List
13. Exit

Enter your choice : 2
The Elements of the Linked List are:
40    4    30    6    8   

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Create Linked List
2. Display Linked List
3. Search Linked List
4. Add at beginning
5. Add at end
6. Add after node
7. Add before node
8. Add at position
9. Delete from beg
10. Delete from end
11. Delete a node
12. Reverse Linked List
13. Exit

Enter your choice : 12
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Create Linked List
2. Display Linked List
3. Search Linked List
4. Add at beginning
5. Add at end
6. Add after node
7. Add before node
8. Add at position
9. Delete from beg
10. Delete from end
11. Delete a node
12. Reverse Linked List
13. Exit

Enter your choice : 2
The Elements of the Linked List are:
8    6    30    4    40   

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Create Linked List
2. Display Linked List
3. Search Linked List
4. Add at beginning
5. Add at end
6. Add after node
7. Add before node
8. Add at position
9. Delete from beg
10. Delete from end
11. Delete a node
12. Reverse Linked List
13. Exit

Enter your choice : 13
Program Terminated..!!
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
manoj@ubuntu:~/cp/linkedlist$

No comments:

Post a Comment