Saturday, 26 April 2014

Binary Search

PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
    int c, first, last, middle, n, f=0, search, array[100];
    clrscr();
    printf("\n\n\n Binary Search using Array \n\n\n");
    printf("Enter number of elements:\t");
    scanf("%d",&n);

    printf("Enter %d integers\n", n);
    for(c=0;c<n;c++)
    {
        scanf("%d",&array[c]);
    }
    printf("Enter value to find:\t");
    scanf("%d",&search);

    first = 0;
    last = n - 1;


    while( first <= last )
    {
        middle = (first+last)/2;
        if ( search < array[middle] )
        {
            last = middle - 1;
        }
        else if( search > array[middle] )
        {
            first= middle + 1;
        }
        else
        {
            printf("%d found at location %d.\n", search, middle+1);
            f=1;
            break;
        }
    }
    if ( f==0 )
    {
        printf("Not found! %d is not present in the list.\n", search);
    }
    getch();
}


OUTPUT:


Binary Search using Array

Enter number of elements:       5
Enter 5 integers
2
4
6
8
10
Enter value to find: 10
10 found at location 5.


 Binary Search using Array
 
Enter number of elements:       5
Enter 5 integers
1
3
5
7
9
Enter value to find: 8
Not found! 8 is not present in the list.

Linear Search


PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
    int a[10],i,n,key,f=0;
    clrscr();
    printf("\n\n LINEAR SEARCH \n\n");
    printf("Enter the array limit:\t");
    scanf("%d",&n);
    printf("Enter array elements:\n");
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    printf("Enter the element to be searched:\t");
    scanf("%d",&key);
    for(i=0;i<n;i++)
    {
        if(a[i]==key)
        {
            printf("Element found\n");
            f=1;
            break;
        }
    }
    if(f==0)
    {
        printf("Element not found\n");
    }
    getch();
}


OUTPUT:


LINEAR SEARCH

Enter the array limit:  5
Enter array elements:
1
3
5
7
9
Enter the element to be searched:       9
Element found



LINEAR SEARCH

Enter the array limit:  5
Enter array elements:
2
4
6
8
10
Enter the element to be searched:       9
Element not found

Sunday, 13 April 2014

Circular Singly Linked List

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
typedef struct node
{
int data;
struct node *next;
}node;
node *head=NULL,*newn,*temp,*t;
void insert();
void del();
void display();
void main()
{
int ch;
clrscr();
printf("\n\n$$$ CIRCULARLY LINKED LIST $$$\n\n");
while(1)
{
printf("\n\nCIRCULARLY LINKED LIST OPERATIONS ARE\n\n");
printf("1.Insert\n2.Delete\n3.Display\n4.Exit\n");
printf("Enter Ur Choice:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:insert();break;
case 2:del();break;
case 3:display();break;
case 4:exit(0);
default:printf("Enter the correct option\n");
break;
}
}
}
void insert()
{
int no,op;
newn=(node *)malloc(sizeof(node));
printf("Enter the element to be inserted:\t");
scanf("%d",&newn->data);
printf("1.Insert at First\n2.Insert at Middle\n3.Insert at Last\n");
printf("Enter the option:\t");
scanf("%d",&op);
switch(op)
{
case 1:
if(head==NULL)
{
head=newn;
newn->next=newn;
}
else
{
temp=head;
while(temp->next!=head)
{
temp=temp->next;
}
temp->next=newn;
newn->next=head;
head=newn;
}
break;

case 2:
if(head->next==NULL)
{
printf("Single node entry so insertion is not allowed in the middle\n");
}
else
{
printf("Enter the data after which the element to be inserted:\t");
scanf("%d",&no);
temp=head;
while(temp->data!=no)
{
temp=temp->next;
}
newn->next=temp->next;
temp->next=newn;
}
break;

case 3:
if(head==NULL)
{
head=newn;
newn->next=newn;
}
else
{
temp=head;
while(temp->next!=head)
{
temp=temp->next;
}
temp->next=newn;
newn->next=head;
}
break;
}
}
void del()
{
int op,no;
if(head==NULL)
{
printf("List is empty\n");
}
else
{
printf("1.Deletion at first\n2.Deletion at middle\n3.Deletion at last\n");
printf("Enter your choice:\t");
scanf("%d",&op);
switch(op)
{
case 1:
if(head->next==head)
{
printf("The deleted element is: %d",head->data);
head=NULL;
}
else
{
temp=head;
printf("The deleted element is: %d",temp->data);
while(temp->next!=head)
{
temp=temp->next;
}
temp->next=head->next;
head=head->next;
free(temp);
}
break;

case 2:
if(head->next==head)
{
printf("Single node entry cannot perform deletion in the middle\n");
}
else
{
printf("Enter the element to be deleted:\t");
scanf("%d",&no);
temp=head;
while(temp->data!=no)
{
t=temp;
temp=temp->next;
}
printf("The deleted element is: %d",temp->data);
t->next=temp->next;
free(temp);
}
break;

case 3:
if(head->next==head)
{
printf("The deleted element is: %d",head->data);
head=NULL;
}
else
{
temp=head;
while(temp->next!=head)
{
t=temp;
temp=temp->next;
}
printf("The deleted element is: %d",temp->data);
t->next=head;
free(temp);
}
break;
}
}
}
void display()
{
if(head==NULL)
{
printf("List is empty\n");
}
else
{
temp=head;
printf("The elements in a list are:\n");
do
{
printf("%d\n",temp->data);
temp=temp->next;
}while(temp!=head);
}

}


OUTPUT:

$$$ CIRCULARLY LINKED LIST $$$

CIRCULARLY LINKED LIST OPERATIONS ARE

1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice:        1
Enter the element to be inserted:       11
1.Insert at First
2.Insert at Middle
3.Insert at Last
Enter the option:       1

CIRCULARLY LINKED LIST OPERATIONS ARE

1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice:        1
Enter the element to be inserted:       33
1.Insert at First
2.Insert at Middle
3.Insert at Last
Enter the option:       3

CIRCULARLY LINKED LIST OPERATIONS ARE

1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice:        1
Enter the element to be inserted:       22
1.Insert at First
2.Insert at Middle
3.Insert at Last
Enter the option:       2
Enter the data after which the element to be inserted:  11

CIRCULARLY LINKED LIST OPERATIONS ARE

1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice:        3
The elements in a list are:
11
22
33

CIRCULARLY LINKED LIST OPERATIONS ARE

1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice:        2
1.Deletion at first
2.Deletion at middle
3.Deletion at last
Enter your choice:      2
Enter the element to be deleted:        22
The deleted element is: 22

CIRCULARLY LINKED LIST OPERATIONS ARE

1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice:        3
The elements in a list are:
11
33

CIRCULARLY LINKED LIST OPERATIONS ARE

1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice:        4

Doubly Linked List

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
typedef struct node
{
int data;
struct node *next,*prev;
}node;
node *head=NULL,*newn,*temp,*t;
void insert();
void del();
void display();
void main()
{
int ch;
clrscr();
printf("\n\n$$$ DOUBLY LINKED LIST $$$\n\n");
while(1)
{
printf("\n\nDOUBLY LINKED LIST OPERATIONS ARE\n\n");
printf("1.Insert\n2.Delete\n3.Display\n4.Exit\n");
printf("Enter Ur Choice:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:insert();break;
case 2:del();break;
case 3:display();break;
case 4:exit(0);
default:printf("Enter the correct option\n");
break;
}
}
}
void insert()
{
int no,op;
newn=(node *)malloc(sizeof(node));
printf("Enter the element to be inserted:\t");
scanf("%d",&newn->data);
printf("1.Insert at First\n2.Insert at Middle\n3.Insert at Last\n");
printf("Enter the option:\t");
scanf("%d",&op);
switch(op)
{
case 1:
if(head==NULL)
{
newn->next=NULL;
newn->prev=NULL;
head=newn;
}
else
{
newn->next=head;
head->prev=newn;
head=newn;
newn->prev=NULL;
}
break;

case 2:
if(head->next==NULL)
{
printf("Single node entry so insertion is not allowed in the middle\n");
}
else
{
printf("Enter the data after which the element to be inserted:\t");
scanf("%d",&no);
temp=head;
while(temp->data!=no)
{
temp=temp->next;
}
newn->next=temp->next;
temp->next->prev=newn;
temp->next=newn;
newn->prev=temp;
}
break;

case 3:
if(head==NULL)
{
newn->next=NULL;
newn->prev=NULL;
head=newn;
}
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newn;
newn->prev=temp;
newn->next=NULL;
}
break;
}
}
void del()
{
int op,no;
if(head==NULL)
{
printf("List is empty\n");
}
else
{
printf("1.Deletion at first\n2.Deletion at middle\n3.Deletion at last\n");
printf("Enter your choice:\t");
scanf("%d",&op);
switch(op)
{
case 1:
if(head->next==NULL)
{
printf("The deleted element is: %d",head->data);
head=NULL;
}
else
{
temp=head;
head=head->next;
printf("The deleted element is: %d",temp->data);
free(temp);
}
break;

case 2:
if(head->next==NULL)
{
printf("Single node entry cannot perform deletion in the middle\n");
}
else
{
printf("Enter the element to be deleted:\t");
scanf("%d",&no);
temp=head;
while(temp->data!=no)
{
t=temp;
temp=temp->next;
}
temp->prev=t;
printf("The deleted element is: %d",temp->data);
t->next=temp->next;
temp->next->prev=t;
free(temp);
}
break;

case 3:
if(head->next==NULL)
{
printf("The deleted element is: %d",head->data);
head=NULL;
}
else
{
temp=head;
while(temp->next!=NULL)
{
t=temp;
temp=temp->next;
}
t->next=NULL;
printf("The deleted element is: %d",temp->data);
free(temp);
}
break;
}
}
}
void display()
{
if(head==NULL)
{
printf("List is empty\n");
}
else
{
temp=head;
printf("The elements in a list are:\n");
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->next;
}
}

}


OUTPUT:

$$$ DOUBLY LINKED LIST $$$

DOUBLY LINKED LIST OPERATIONS ARE

1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice:        1
Enter the element to be inserted:       11
1.Insert at First
2.Insert at Middle
3.Insert at Last
Enter the option:       1

DOUBLY LINKED LIST OPERATIONS ARE

1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice:        1
Enter the element to be inserted:       33
1.Insert at First
2.Insert at Middle
3.Insert at Last
Enter the option:       3

DOUBLY LINKED LIST OPERATIONS ARE

1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice:        1
Enter the element to be inserted:       22
1.Insert at First
2.Insert at Middle
3.Insert at Last
Enter the option:       2
Enter the data after which the element to be inserted:  11

DOUBLY LINKED LIST OPERATIONS ARE

1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice:        3
The elements in a list are:
11
22
33

DOUBLY LINKED LIST OPERATIONS ARE

1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice:        2
1.Deletion at first
2.Deletion at middle
3.Deletion at last
Enter your choice:      2
Enter the element to be deleted:        22
The deleted element is: 22

DOUBLY LINKED LIST OPERATIONS ARE

1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice:        3
The elements in a list are:
11
33

DOUBLY LINKED LIST OPERATIONS ARE

1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice:        4

Singly Linked List

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
typedef struct node
{
int data;
struct node *next;
}node;
node *head=NULL,*newn,*temp,*t;
void insert();
void del();
void display();
void main()
{
int ch;
clrscr();
printf("\n\n$$$ SINGLY LINKED LIST $$$\n\n");
while(1)
{
printf("\n\nSINGLY LINKED LIST OPERATIONS ARE\n\n");
printf("1.Insert\n2.Delete\n3.Display\n4.Exit\n");
printf("Enter Ur Choice:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:insert();break;
case 2:del();break;
case 3:display();break;
case 4:exit(0);
default:printf("Enter the correct option\n");
break;
}
}
}
void insert()
{
int no,op;
newn=(node *)malloc(sizeof(node));
printf("Enter the element to be inserted:\t");
scanf("%d",&newn->data);
printf("1.Insert at First\n2.Insert at Middle\n3.Insert at Last\n");
printf("Enter the option:\t");
scanf("%d",&op);
switch(op)
{
case 1:
if(head==NULL)
{
head=newn;
head->next=NULL;
}
else
{
newn->next=head;
head=newn;
}
break;

case 2:
if(head->next==NULL)
{
printf("Single node entry so insertion is not allowed in the middle\n");
}
else
{
printf("Enter the data after which the element to be inserted:\t");
scanf("%d",&no);
temp=head;
while(temp->data!=no)
{
temp=temp->next;
}
newn->next=temp->next;
temp->next=newn;
}
break;

case 3:
temp=head;
if(head==NULL)
{
head=newn;
newn->next=NULL;
}
else
{
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newn;
newn->next=NULL;
}
break;
}
}
void del()
{
int op,no;
if(head==NULL)
{
printf("List is empty\n");
}
else
{
printf("1.Deletion at first\n2.Deletion at middle\n3.Deletion at last\n");
printf("Enter your choice:\t");
scanf("%d",&op);
switch(op)
{
case 1:
if(head->next==NULL)
{
printf("The deleted element is: %d",head->data);
head=NULL;
}
else
{
temp=head;
head=head->next;
printf("The deleted element is: %d",temp->data);
free(temp);
}
break;

case 2:
if(head->next==NULL)
{
printf("Single node entry cannot perform deletion in the middle\n");
}
else
{
printf("Enter the element to be deleted:\t");
scanf("%d",&no);
temp=head;
while(temp->data!=no)
{
t=temp;
temp=temp->next;
}
printf("The deleted element is: %d",temp->data);
t->next=temp->next;
}
break;

case 3:
temp=head;
if(head->next==NULL)
{
printf("The deleted element is: %d",head->data);
head=NULL;
}
else
{
while(temp->next!=NULL)
{
t=temp;
temp=temp->next;
}
t->next=NULL;
printf("The deleted element is: %d",temp->data);
}
break;
}
}
}
void display()
{
if(head==NULL)
{
printf("List is empty\n");
}
else
{
temp=head;
printf("The elements in a list are:\n");
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->next;
}
}

}



OUTPUT:

$$$ SINGLY LINKED LIST $$$

SINGLY LINKED LIST OPERATIONS ARE

1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice:        1
Enter the element to be inserted:       11
1.Insert at First
2.Insert at Middle
3.Insert at Last
Enter the option:       1

SINGLY LINKED LIST OPERATIONS ARE

1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice:        1
Enter the element to be inserted:       33
1.Insert at First
2.Insert at Middle
3.Insert at Last
Enter the option:       3

SINGLY LINKED LIST OPERATIONS ARE

1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice:        1
Enter the element to be inserted:       22
1.Insert at First
2.Insert at Middle
3.Insert at Last
Enter the option:       2
Enter the data after which the element to be inserted:  11

SINGLY LINKED LIST OPERATIONS ARE

1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice:        3
The elements in a list are:
11
22
33

SINGLY LINKED LIST OPERATIONS ARE

1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice:        2
1.Deletion at first
2.Deletion at middle
3.Deletion at last
Enter your choice:      2
Enter the element to be deleted:        22
The deleted element is: 22

SINGLY LINKED LIST OPERATIONS ARE

1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice:        3
The elements in a list are:
11
33

SINGLY LINKED LIST OPERATIONS ARE

1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice:       4

Queue Implementation Using Linked List or Pointer

PROGRAM:


#include<stdio.h>
#include<conio.h>
typedef struct queue
{
                int data;
                struct queue *next;
}node;
node *newn,*front,*rear,*temp;
void enqueue();
void dequeue();
void display();
void main()
{
                int ch;
                clrscr();
                printf("\n\n\n$$$ QUEUE IMPLEMENTATION USING LINKED LIST OR POINTER $$$\n\n\n");
                while(1)
                {
                                printf("QUEUE OPERATIONS ARE\n");
                                printf("1.ENQUEUE\n2.DEQUEUE\n3.DISPLAY\n4.EXIT\n");
                                printf("Enter your choice:\t");
                                scanf("%d",&ch);
                                switch(ch)
                                {
                                                case 1:enqueue();break;
                                                case 2:dequeue();break;
                                                case 3:display();break;
                                                case 4:exit(0);
                                                default:printf("Enter the correct option\n");
                                                break;
                                }
                }
}
void enqueue()
{
                newn=(node *)malloc(sizeof(node));
                printf("Enter the element to be inserted:\t");
                scanf("%d",&newn->data);
                newn->next=NULL;
                if(front==NULL)
                {
                                front=newn;
                                rear=newn;
                }
                else
                {
                                rear->next=newn;
                                rear=newn;
                }
}
void dequeue()
{
                if(front==NULL)
                {
                                printf("Queue is empty or underflow\n");
                }
                else
                {
                                temp=front;
                                front=front->next;
                                printf("The deleted element is: %d",temp->data);
                }
                free(temp);
}
void display()
{
                if(front==NULL)
                {
                                printf("Queue is empty or underflow\n");
                }
                else
                {
                                temp=front;
                                printf("Content of the queue is:\n");
                                while(temp!=NULL)
                                {
                                                printf("%d\n",temp->data);
                                                temp=temp->next;
                                }
                }
}


 OUTPUT:

$$$ QUEUE IMPLEMENTATION USING LINKED LIST OR POINTER $$$

 QUEUE OPERATIONS ARE
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your choice:      1
Enter the element to be inserted:       11

QUEUE OPERATIONS ARE
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your choice:      1
Enter the element to be inserted:       22

QUEUE OPERATIONS ARE
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your choice:      1
Enter the element to be inserted:       33

QUEUE OPERATIONS ARE
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your choice:      3
Content of the queue is:
11
22
33

QUEUE OPERATIONS ARE
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your choice:      2
The deleted element is: 11

QUEUE OPERATIONS ARE
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your choice:           3
Content of the queue is:
22
33

QUEUE OPERATIONS ARE
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT

Enter your choice:      4