Sunday, September 10, 2017

Program to Implement Doubly Linked List in C

/*==========================================================================================
 **
 **  File Name     : dll.c
 **  Creation Date : Sun 10 Sep 2017 09:22:08 PM IST
 **  Last Modified : Sun 10 Sep 2017 10:38:41 PM IST
 **  Compiler      : gcc
 **  Author        : Manoj Kumar Patra, Asst. Professor
 **  Organization  : Vignan University, Guntur, India.
 **
 **=========================================================================================*/

/*Program to Implement Doubly Linked List*/

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

struct node
{
    struct node *prev;
    int info;
    struct node *next;
};

struct node *create_dll(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 *add_before_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);
struct node *reverse(struct node *start);

main()
{
    int choice,data,item;
    struct node *start=NULL;
    printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
    printf("= = = = = = = = = = Doubly Linked List in C = = = = = = = = = =\n");
    while(1)
    {
        printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
        printf("1.Create DLL\n");
        printf("2.Display DLL\n");
        printf("3.Add at beg\n");
        printf("4.Add at end\n");
        printf("5.Add after node\n");
        printf("6.Add before node\n");
        printf("7.Delete from beg\n");
        printf("8.Delete from end\n");
        printf("9.Delete a node\n");
        printf("10.Reverse\n");
        printf("11.Exit\n");
        printf("\nEnter your choice : ");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:
                start=create_dll(start);
                display(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);
                printf("The resultant DLL\n");
                display(start);
                break;
            case 4:
                printf("Enter the element to be inserted: ");
                scanf("%d",&data);
                start=add_at_end(start,data);
                printf("The resultant DLL\n");
                display(start);
                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);
                printf("The resultant DLL\n");
                display(start);
                break;
            case 6:
                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);
                printf("The resultant DLL\n");
                display(start);
                break;
            case 7:
                start=del_at_beg(start);
                printf("The resultant DLL\n");
                display(start);
                break;
            case 8:
                start=del_at_end(start);
                printf("The resultant DLL\n");
                display(start);
                break;
            case 9:
                printf("Enter the element to be deleted: ");
                scanf("%d",&data);
                start=del_a_node(start,data);
                printf("The resultant DLL\n");
                display(start);
                break;
            case 10:
                start=reverse(start);
                printf("The resultant DLL\n");
                display(start);
                break;
            case 11:
                printf("Program Terminated..!!\n");
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
                exit(1);
            default:
                printf("Oops.. Wrong choice !\n");
        }
    }
    printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n\n");
}

//1
struct node *create_dll(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);
    struct node *tmp;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->info=data;
    tmp->prev=NULL;
    tmp->next=NULL;
    start=tmp;
    if(n>1)
    {
        for(i=2; i<=n; i++)
        {
            printf("Enter the element to be inserted : ");
            scanf("%d",&data);
            start=add_at_end(start,data);
        }

    }
    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 DLL are:\n");
    while(p!=NULL)
    {
        printf("%d\t",p->info);
        p=p->next;
    }
    printf("\n");
}

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

//4
struct node *add_at_end(struct node *start,int data)
{
    struct node *tmp,*p;
    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;
    tmp->prev=p;
    return start;
}

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

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

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

//8
struct node *del_at_end(struct node *start)
{
    if(start==NULL)
    {
        printf("List is empty\n");
        return start;
    }
    struct node *ptmp, *tmp;
    tmp=start;
    while(tmp->next!=NULL)
    {
        ptmp=tmp;
        tmp=tmp->next;
    }
    ptmp->next=NULL;
    free(tmp);
    return start;
}

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

//10
struct node *reverse(struct node *start)
{
    struct node *p1,*p2;
    p1=start;
    p2=p1->next;
    p1->next=NULL;
    p1->prev=p2;
    while(p2!=NULL)
    {
        p2->prev=p2->next;
        p2->next=p1;
        p1=p2;
        p2=p2->prev;
    }
    start=p1;
    printf("List reversed\n");
    return start;
}

OutPut:

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

Enter your choice : 1
Enter the number of nodes: 4
Enter the element to be inserted : 4
Enter the element to be inserted : 5
Enter the element to be inserted : 6
Enter the element to be inserted : 7
The elements of DLL are:
4    5    6    7   
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1.Create DLL
2.Display DLL
3.Add at beg
4.Add at end
5.Add after node
6.Add before node
7.Delete from beg
8.Delete from end
9.Delete a node
10.Reverse
11.Exit

Enter your choice : 2
The elements of DLL are:
4    5    6    7   
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1.Create DLL
2.Display DLL
3.Add at beg
4.Add at end
5.Add after node
6.Add before node
7.Delete from beg
8.Delete from end
9.Delete a node
10.Reverse
11.Exit

Enter your choice : 3
Enter the element to be inserted: 3
The resultant DLL
The elements of DLL are:
3    4    5    6    7   
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1.Create DLL
2.Display DLL
3.Add at beg
4.Add at end
5.Add after node
6.Add before node
7.Delete from beg
8.Delete from end
9.Delete a node
10.Reverse
11.Exit

Enter your choice : 4
Enter the element to be inserted: 8
The resultant DLL
The elements of DLL are:
3    4    5    6    7    8   
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1.Create DLL
2.Display DLL
3.Add at beg
4.Add at end
5.Add after node
6.Add before node
7.Delete from beg
8.Delete from end
9.Delete a node
10.Reverse
11.Exit

Enter your choice : 5
Enter the element to be inserted: 9
Enter the element after which to insert: 5
The resultant DLL
The elements of DLL are:
3    4    5    9    6    7    8   
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1.Create DLL
2.Display DLL
3.Add at beg
4.Add at end
5.Add after node
6.Add before node
7.Delete from beg
8.Delete from end
9.Delete a node
10.Reverse
11.Exit

Enter your choice : 6
Enter the element to be inserted: 10
Enter the element before which to insert: 5
The resultant DLL
The elements of DLL are:
3    4    10    5    9    6    7    8   
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1.Create DLL
2.Display DLL
3.Add at beg
4.Add at end
5.Add after node
6.Add before node
7.Delete from beg
8.Delete from end
9.Delete a node
10.Reverse
11.Exit

Enter your choice : 7
First node has been deleted..!
The resultant DLL
The elements of DLL are:
4    10    5    9    6    7    8   
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1.Create DLL
2.Display DLL
3.Add at beg
4.Add at end
5.Add after node
6.Add before node
7.Delete from beg
8.Delete from end
9.Delete a node
10.Reverse
11.Exit

Enter your choice : 8
The resultant DLL
The elements of DLL are:
4    10    5    9    6    7   
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1.Create DLL
2.Display DLL
3.Add at beg
4.Add at end
5.Add after node
6.Add before node
7.Delete from beg
8.Delete from end
9.Delete a node
10.Reverse
11.Exit

Enter your choice : 9
Enter the element to be deleted: 10
The resultant DLL
The elements of DLL are:
4    5    9    6    7   
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1.Create DLL
2.Display DLL
3.Add at beg
4.Add at end
5.Add after node
6.Add before node
7.Delete from beg
8.Delete from end
9.Delete a node
10.Reverse
11.Exit

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

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$

Program to implement FCFS CPU Scheduling Algorithm in C

#include<stdio.h>
struct fcfs
{
    int at;
    int bt;
    int st;
    int ct;
    int wt;
    int tt;
};

int main()
{
    int n;
    printf("\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
    printf("* * * * * * * FCFS CPU Scheduling Algorithm * * * * * * * *\n");
    printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
    int i, tot_wt=0,tot_tt=0,avgwt=0,avgtt=0;
    printf("How many processes you want to run: ");
    scanf("%d",&n);
    struct fcfs p[n];
    for(i=0; i<n; i++)
    {
        printf("\tEnter AT of Process %d:",i+1);
        scanf("%d",&p[i].at);
        printf("\tEnter BT of Process %d:",i+1);
        scanf("%d",&p[i].bt);
    }
    //Schedule Time and Completion Time Calculation  
    p[0].st=p[0].at;
    p[0].ct=p[0].st+p[0].bt;
    for(i=1; i<n; i++)
    {
        p[i].st=p[i-1].ct;
        p[i].ct=p[i].st+p[i].bt;
    }
    //Turn Around Time Calculation
    for(i=0; i<n; i++)
    {
        p[i].tt=p[i].ct-p[i].at;
        tot_tt=tot_tt+p[i].tt;//Total TAT
    }
    //Waiting Time Calculation
    for(i=0; i<n; i++)
    {
        p[i].wt=p[i].tt-p[i].bt;
        tot_wt=tot_wt+p[i].wt;//Total WT
    }
  
    printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
    printf("\tAT\tBT\tST\tCT\tWT\tTAT\n");
    printf("\t__\t__\t__\t__\t__\t__\n");
    for(i=0; i<n; i++)
    {
        printf("\t%d\t%d\t%d\t%d\t%d\t%d\n",p[i].at,p[i].bt,p[i].st,p[i].ct,p[i].wt,p[i].tt);
    }
    printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
    //Average Waiting Time Calculation
    avgwt=tot_wt/n;
    printf("\tTotal Waiting Time = %d\n",tot_wt);
    printf("\tAverage Waiting Time = %d\n",avgwt);
    printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
    avgtt=tot_tt/n;
    printf("\tTotal Turn Around Time = %d\n",tot_tt);
    printf("\tAverag Turn Around Time = %d\n",avgtt);
    printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
    printf("* * * * * * * End FCFS CPU Scheduling Algorithm * * * * * *\n");
    printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n\n");
}

OutPut:
manoj@ubuntu:~/os$ gcc fcfs.c -o fcfs
manoj@ubuntu:~/os$ ./fcfs

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
* * * * * * * FCFS CPU Scheduling Algorithm * * * * * * * *
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
How many processes you want to run: 5
    Enter AT of Process 1:1
    Enter BT of Process 1:2
    Enter AT of Process 2:2
    Enter BT of Process 2:3
    Enter AT of Process 3:3
    Enter BT of Process 3:4
    Enter AT of Process 4:4
    Enter BT of Process 4:5
    Enter AT of Process 5:5
    Enter BT of Process 5:6
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
    AT    BT    ST    CT    WT    TAT
    __    __    __    __    __    __
    1    2    1    3    0    2
    2    3    3    6    1    4
    3    4    6    10    3    7
    4    5    10    15    6    11
    5    6    15    21    10    16
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
    Total Waiting Time = 20
    Average Waiting Time = 4
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
    Total Turn Around Time = 40
    Averag Turn Around Time = 8
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
* * * * * * * End FCFS CPU Scheduling Algorithm * * * * * *
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

manoj@ubuntu:~/os$

Friday, September 1, 2017

Program to Implement Linear Search Using C

#include <stdio.h>
int linear_search(int a[], int k, int se);
int main()
{
    int arr[100], n, i, pos, se, choice;
    printf("\n= = = = = = = = = = Linear search in C = = = = = = = = = =\n");
    printf("How many integers you wanna store..? : ");
    scanf("%d", &n);
    printf("Enter %d integers into the array:\n", n);
    for(i=0; i<n; i++)
    {
        scanf("%d",&arr[i]);
    }
    do
    {
        printf("Press 1 to continue\n");
        printf("Press 0 to terminate\n");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");              
                printf("The array elements are:\n");
                for(i=0; i<n; i++)
                {
                    printf("%d\t",arr[i]);
                }
                printf("\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n");
                printf("Enter the elements you wanna search: ");
                scanf("%d",&se);
                pos=linear_search(arr,n,se);
                if(pos==0)
                {
                    printf("\n%d is not present in array.!!\n", se);
                    printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");      
                    continue;
                }
                printf("\nThe element %d is present at -%d- position:\n",se,pos);
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
                continue;
            case 0:
                printf("\nProgram Terminated\n");
                return;
            default:
                printf("\nInvalid Input..!!\n");
        }
      
    }while(1);
}

int linear_search(int arr[], int k, int se)
{
    int i;
    for (i=0; i<k; i++)
       {
              if (arr    [i]==se)
              {
                return i+1;
              }
       }
       return 0;  
}

OutOut:

manoj@ubuntu:~/cp$ gcc linearsearch.c -o linearsearch
manoj@ubuntu:~/cp$ ./linearsearch

= = = = = = = = = = Linear search in C = = = = = = = = = =
How many integers you wanna store..? : 5
Enter 5 integers into the array:
1
2
3
4
5
Press 1 to continue
Press 0 to terminate
1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
The array elements are:
1    2    3    4    5   
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Enter the elements you wanna search: 5

The element 5 is present at -5- position:
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Press 1 to continue
Press 0 to terminate
1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
The array elements are:
1    2    3    4    5   
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Enter the elements you wanna search: 1

The element 1 is present at -1- position:
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Press 1 to continue
Press 0 to terminate
1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
The array elements are:
1    2    3    4    5   
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Enter the elements you wanna search: 3

The element 3 is present at -3- position:
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Press 1 to continue
Press 0 to terminate
1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
The array elements are:
1    2    3    4    5   
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Enter the elements you wanna search: 10

10 is not present in array.!!
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Press 1 to continue
Press 0 to terminate
0

Program Terminated
manoj@ubuntu:~/cp$