Thursday, October 19, 2017

Program to Implement Circular Linked List in C

/*==========================================================================================
 **
 **  File Name     : cll.c
 **  Creation Date : Sat 16 Sep 2017 10:18:25 PM IST
 **  Last Modified : Thu 19 Oct 2017 05:47:44 PM IST
 **  Compiler      : gcc
 **  Author        : Manoj Kumar Patra, Asst. Professor
 **  Organization  : Vignan University, Guntur, India.
 **
 **=========================================================================================*/

/*P3.3 Program of circular nexted 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);
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 *del_at_beg(struct node *start);
struct node *del_at_end(struct node *start);
struct node *del_a_node(struct node *start, int data);
void displayNtimes(struct node *start);

int main( )
{
    int choice,data,item;
    struct node *start=NULL;

    printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
    printf("= = = = = = = = = = Circular Linked List in C = = = = = = = = =\n");
    while(1)
    {
        printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
        printf(" 1. Create List\n");
        printf(" 2. Display CLL\n");
        printf(" 3. Add at Beg\n");
        printf(" 4. Add at End\n");
        printf(" 5. Add after node \n");
        printf(" 6. Delete at Beg\n");
        printf(" 7. Delete at End\n");
        printf(" 8. Delete a node\n");
        printf(" 9. Display CLL N Times\n");
        printf(" 10. Exit\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 inserted : ");
                scanf("%d",&data);
                start=add_at_beg(start,data);
                break;
            case 4:
                printf("Enter the element to be inserted : ");
                scanf("%d",&data);
                start=add_at_end(start,data);
                break;
            case 5:
                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 6:
                start=del_at_beg(start);
                printf("First node deleted..!\n");
                break;
            case 7:
                start=del_at_end(start);
                printf("Last node deleted..!\n");
                break;
            case 8:
                printf("Enter the element to be deleted : ");
                scanf("%d",&data);
                start=del_a_node(start,data);
                break;
            case 9:
                displayNtimes(start);
                break;
            case 10:
                printf("Program Terminated..!!\n");
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
                exit(1);
            default:
                printf("Oops.. Wrong choice\n");
        }
    }
}

//1 ok
struct node *create_list(struct node *start)
{
    if(start!=NULL)
    {
        printf("CLL already exist\n");
        return start;
    }
    int data;
    printf("Enter the element to be inserted: ");
    scanf("%d",&data);
    struct node *tmp;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->info=data;
    tmp->next=tmp;
    return tmp;
}

//2 ok
void display(struct node *s)
{
    struct node *p;
    if(s == NULL)
    {
        printf("CLL is empty!\n");
        return;
    }
    else
    {
        p=s;
        printf("\nThe elements of CLL are:\n");
        do
        {
            printf("%d\t",p->info);
            p=p->next;
        }while(p != s);
    }
    printf("\n");
}

//3 ok
struct node *add_at_beg(struct node *start,int data)
{
    struct node *tmp,*curr;
    curr=start;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->info=data;
    tmp->next=start;
    while(curr->next!=start)
    {
        curr=curr->next;
    }
    curr->next=tmp;
    return tmp;
}

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

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

//6 ok
struct node *del_at_beg(struct node *start)
{
    struct node *tmp,*last;
    if(start==NULL)
    {
        printf("CLL is empty, Deletion not possible\n");
        return start;
    }
    /*If only one node is there*/
    else if(start->next==start)
    {
        tmp=start;
        start=NULL;
        free(tmp);
        return start;
    }
    else
    {
        last=start;
        while(last->next!=start)
        {
            last=last->next;
        }
        tmp=start;
        start=start->next;
        last->next=start;
        free(tmp);
        return start;
    }
}

//7 ok
struct node *del_at_end(struct node *start)
{
    struct node *tmp, *last, *prev;
    if(start==NULL)
    {
        printf("CLL is empty, Deletion not possible\n");
        return start;
    }
    /*If only one node is there*/
    else if(start->next==start)
    {
        tmp=start;
        start=NULL;
        free(tmp);
        return start;
    }
    else
    {
        last=start;
        while(last->next != start)
        {
            prev=last;
            last=last->next;
        }
        tmp=last;
        prev->next=last->next;
        free(tmp);
        return start;
    }
}

//8 ok
struct node *del_a_node(struct node *start,int item)
{
    struct node *tmp,*p,*pp;
    if(start==NULL)
    {
        printf("CLL is empty, Deletion not possible\n");
        return start;
    }
    /*If only one node is there*/
    else if(start->info==item)
    {
        p=start;
        while(p->next!=start)
        {
            p=p->next;
        }
        tmp=start;
        start=start->next;
        p->next=start;
        free(tmp);
        return start;
    }

    /*Deletion in between*/
    else
    {
        p=start;
        while(p->next!=start)
        {
            pp=p;
            p=p->next;
            if(p->info==item)   
            {
                tmp=p;
                pp->next=p->next;
                free(tmp);
                return start;
            }
        }

    }
    printf("Element %d not found in CLL\n",item);
    return start;
}

//9 ok
void displayNtimes(struct node *s)
{
    int n,count=0;
    printf("How many times you wanna print CLL?: ");
    scanf("%d",&n);
    struct node *p;
    if(s == NULL)
    {
        printf("CLL is empty!\n");
        return;
    }
    else
    {
        p=s;
        printf("\nThe elements of CLL are:\n");
        do
        {
            if(p->next == s)
            {
                count++;
            }
            printf("%d\t",p->info);
            p=p->next;

        }while(count < n);
    }
    printf("\n");
}


OutPut:

manoj@ubuntu:~/ds/linkedlist$ gcc cll.c -o cll
manoj@ubuntu:~/ds/linkedlist$ ./cll
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
= = = = = = = = = = Circular Linked List in C = = = = = = = = =
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 1. Create List
 2. Display CLL
 3. Add at Beg
 4. Add at End
 5. Add after node
 6. Delete at Beg
 7. Delete at End
 8. Delete a node
 9. Display CLL N Times
 10. Exit
Enter your choice : 1
Enter the element to be inserted: 20
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 1. Create List
 2. Display CLL
 3. Add at Beg
 4. Add at End
 5. Add after node
 6. Delete at Beg
 7. Delete at End
 8. Delete a node
 9. Display CLL N Times
 10. Exit
Enter your choice : 2

The elements of CLL are:
20   
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 1. Create List
 2. Display CLL
 3. Add at Beg
 4. Add at End
 5. Add after node
 6. Delete at Beg
 7. Delete at End
 8. Delete a node
 9. Display CLL N Times
 10. Exit
Enter your choice : 3
Enter the element to be inserted : 10
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 1. Create List
 2. Display CLL
 3. Add at Beg
 4. Add at End
 5. Add after node
 6. Delete at Beg
 7. Delete at End
 8. Delete a node
 9. Display CLL N Times
 10. Exit
Enter your choice : 2

The elements of CLL are:
10    20   
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 1. Create List
 2. Display CLL
 3. Add at Beg
 4. Add at End
 5. Add after node
 6. Delete at Beg
 7. Delete at End
 8. Delete a node
 9. Display CLL N Times
 10. Exit
Enter your choice : 4
Enter the element to be inserted : 30
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 1. Create List
 2. Display CLL
 3. Add at Beg
 4. Add at End
 5. Add after node
 6. Delete at Beg
 7. Delete at End
 8. Delete a node
 9. Display CLL N Times
 10. Exit
Enter your choice : 2

The elements of CLL are:
10    20    30   
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 1. Create List
 2. Display CLL
 3. Add at Beg
 4. Add at End
 5. Add after node
 6. Delete at Beg
 7. Delete at End
 8. Delete a node
 9. Display CLL N Times
 10. Exit
Enter your choice : 5
Enter the element to be inserted : 50
Enter the element after which to insert : 20
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 1. Create List
 2. Display CLL
 3. Add at Beg
 4. Add at End
 5. Add after node
 6. Delete at Beg
 7. Delete at End
 8. Delete a node
 9. Display CLL N Times
 10. Exit
Enter your choice : 2

The elements of CLL are:
10    20    50    30   
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 1. Create List
 2. Display CLL
 3. Add at Beg
 4. Add at End
 5. Add after node
 6. Delete at Beg
 7. Delete at End
 8. Delete a node
 9. Display CLL N Times
 10. Exit
Enter your choice : 6
First node deleted..!
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 1. Create List
 2. Display CLL
 3. Add at Beg
 4. Add at End
 5. Add after node
 6. Delete at Beg
 7. Delete at End
 8. Delete a node
 9. Display CLL N Times
 10. Exit
Enter your choice : 2

The elements of CLL are:
20    50    30   
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 1. Create List
 2. Display CLL
 3. Add at Beg
 4. Add at End
 5. Add after node
 6. Delete at Beg
 7. Delete at End
 8. Delete a node
 9. Display CLL N Times
 10. Exit
Enter your choice : 7
Last node deleted..!
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 1. Create List
 2. Display CLL
 3. Add at Beg
 4. Add at End
 5. Add after node
 6. Delete at Beg
 7. Delete at End
 8. Delete a node
 9. Display CLL N Times
 10. Exit
Enter your choice : 2

The elements of CLL are:
20    50   
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 1. Create List
 2. Display CLL
 3. Add at Beg
 4. Add at End
 5. Add after node
 6. Delete at Beg
 7. Delete at End
 8. Delete a node
 9. Display CLL N Times
 10. Exit
Enter your choice : 8
Enter the element to be deleted : 30
Element 30 not found in CLL
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 1. Create List
 2. Display CLL
 3. Add at Beg
 4. Add at End
 5. Add after node
 6. Delete at Beg
 7. Delete at End
 8. Delete a node
 9. Display CLL N Times
 10. Exit
Enter your choice : 8
Enter the element to be deleted : 50
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 1. Create List
 2. Display CLL
 3. Add at Beg
 4. Add at End
 5. Add after node
 6. Delete at Beg
 7. Delete at End
 8. Delete a node
 9. Display CLL N Times
 10. Exit
Enter your choice : 2

The elements of CLL are:
20   
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 1. Create List
 2. Display CLL
 3. Add at Beg
 4. Add at End
 5. Add after node
 6. Delete at Beg
 7. Delete at End
 8. Delete a node
 9. Display CLL N Times
 10. Exit
Enter your choice : 9
How many times you wanna print CLL?: 5

The elements of CLL are:
20    20    20    20    20   
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 1. Create List
 2. Display CLL
 3. Add at Beg
 4. Add at End
 5. Add after node
 6. Delete at Beg
 7. Delete at End
 8. Delete a node
 9. Display CLL N Times
 10. Exit
Enter your choice : 10
Program Terminated..!!
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
manoj@ubuntu:~/ds/linkedlist$

No comments:

Post a Comment