C program to implement interpolation search

This program implements interpolation search in c. Interpolation search makes log(log(n)) comparisons on average and O(n) comparisons in the worst case.Where  'n' is the number of elements to be searched. Interpolation search is used to search for a key value in an indexed array ordered by the values of the key.

Program


#include<stdio.h>

#include<conio.h>

void main()

{

int a[25],n,mid,low,high,f=0,item,i;

printf("Enter the size of the array");

scanf("%d",&n);

printf("Enter the elements in sorted order");

for(i=0;i<n;i++)

{

scanf("%d",&a[i]);

}

printf("Enter the item to be searched for");

scanf("%d",&item);

low=0;

high=n-1;

while(low<=high)

{

mid=(low+(high-low)*((item-a[low])/(a[high]-a[low])));

if(a[mid]==item)

{

printf("\n\nItem found at position %d",mid);

f=1;

break;

}

else if(a[mid]>item)

{

high=mid-1;

}

else

{

low=mid+1;

}}

if(f==0)

printf("\n\nItem not found in the array");

getch();

}

C Program to implement linked queue

This program implements linked queue in c. A linked queue uses linked list to insert and delete elements. A queue is a data structure where new items are entered on the rear and are removed form from the front. The advantage of linked queue is that the items need no be in contiguous memory locations. This program implements enqueue, dequeue and display operations of a linked queue. The terms enqueue and dequeue represents insertion and deletion operations respectively.


Program


//Demonstration of linked queue

#include<stdio.h>

#include<conio.h>

struct node

{

int data;

struct node *next;

}*f=NULL,*r=NULL,*p;

void main()

{

int ch,item;

printf("\n1.Enqueue\n2.Dequeue\n3.Display\n4.Exit ");

scanf("%d",&ch);

while(ch!=4)

{

switch(ch)

{

case 1:

printf("\nEnter the data to be inseted ");

scanf("%d",&item);

p=(struct node *)malloc(sizeof(struct node));

if(r==NULL)

{

f=p;

r=p;

p->data=item;

p->next=NULL;

break;

}

p->data=item;

p->next=NULL;

r->next=p;

r=p;

break;

case 2:

if(r==NULL&&f==NULL)

{

printf("\nQueue is empty\n");

break;

}

if(f->next==NULL&&r->next==NULL)

{

free(f);

f=NULL;

r=NULL;

}

p=f;

f=f->next;

free(p);

break;

case 3:

if(f==NULL&&r==NULL)

{

printf("\nQueue is Empty\n");

break;

}

p=f;

while(p!=NULL)

{

printf("%d->",p->data);

p=p->next;

}

break;

}

printf("\n1.Enqueue\n2.Dequeue\n3.Display\n4.Exit ");

scanf("%d",&ch);

}

}


C Program to implement linked stack

This program implements linked stack in c. A linked stack uses linked list to insert and delete elements in Last In First Out (LIFO) order. The items are entered and deleted form the top of the stack. The advantage of linked stack is that the items need not be entered in contiguous memory locations. This program implements push, pop and display operations in a linked stack. The stack uses the terms push and pop to denote insertion and deletion operation respectively.


Program


//Linked Stack
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *next;
}*top=NULL,*p;
void main()
{
int ch,item;
printf("\n1.Push\n2.Pop\n3.Display\n4.Exit\n");
scanf("%d",&ch);
while(ch!=4)
{
switch(ch)
{
case 1:
printf("\nEnter the data to be inserted ");
scanf("%d",&item);
p=(struct node *)malloc(sizeof(struct node));
p->data=item;
p->next=top;
top=p;
break;
case 2:
if(top==NULL)
{
printf("\nStack is empty!!!\n");
}
else
{
p=top;
top=top->next;
free(p);
}
break;
case 3:
p=top;
if(p==NULL)
{
printf("Stack is empty!!!");
break;
}
printf("Stack Now is..\n\n");
while(p!=NULL)
{
printf("%d->",p->data);
p=p->next;
}
break;
}
printf("\n\n1.Push\n2.Pop\n3.Display\n4.Exit\n");
scanf("%d",&ch);
}
}

C Program to implement singly linked list

This program implements linked list in c.  A linked list is a collection of nodes. Each node contains a data part and a pointer to the next node.The advantage of linked list is that the data items need not be stored in contiguous locations in the memory. This program implements insertion, deletion and display of a linked list.

Program


#include<stdio.h>

#include<conio.h>

struct node

{

    int data;

    struct node *next;

}*start=NULL,*p,*traverse,*temp;

void main()

{

    int ch,item;

    printf("1.Insert Begin\n2.Insert after\n3.Delete Begin");

    printf("\n4.Delete after\n5.Display\n6.Exit\nEnter Your Choice");

    scanf("%d",&ch);

    while(ch!=6)

    {

    switch(ch)

    {

        case 1:

        p=(struct node*)malloc(sizeof(struct node));

        printf("Enter the data\n");

        scanf("%d",&item);

        p->data=item;

        p->next=start;

        start=p;

        break;

        case 2:

        printf("Insert after which item\n");

        scanf("%d",&item);

        traverse=start;

        while(traverse->data!=item)

        traverse=traverse->next;

        temp=traverse->next;

        p=(struct node*)malloc(sizeof(struct node));

        printf("Enter the data\n");

        scanf("%d",&item);

        p->data=item;

        traverse->next=p;

        p->next=temp;

        break;

        case 3:

        temp=start->next;

        free(start);

        start=temp;

        break;

        case 4:

        printf("Delete after which item");

        scanf("%d",&item);

        traverse=start;

        while(traverse->data!=item)

        traverse=traverse->next;

        p=traverse->next;

        temp=p->next;

        traverse->next=temp;

        free(p);

        break;

        case 5:

        traverse=start;

        printf("\nCurrent List is\n");

        while(traverse!=NULL)

        {

            printf("%d -> ",traverse->data);

            traverse=traverse->next;

        }

        break;

    }

    printf("\n\n1.Insert Begin\n2.Insert after\n3.Delete Begin");

    printf("\n4.Delete after\n5.Display\n6.Exit\nEnter Your Choice");

    scanf("%d",&ch);

    }

}