0% found this document useful (0 votes)
53 views

Program To Find The Location of The Key Value in An Array

The document contains code for a queue data structure with functions to add elements to the queue, delete elements from the queue, and traverse/print the elements of the queue. It implements a queue using a linked list structure with a start pointer. The main function contains a loop that prompts the user for an operation choice and calls the corresponding function. The add function adds a new node to the end of the queue. The delete function removes a node from the front and returns the deleted value. The traverse function prints all elements currently in the queue.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Program To Find The Location of The Key Value in An Array

The document contains code for a queue data structure with functions to add elements to the queue, delete elements from the queue, and traverse/print the elements of the queue. It implements a queue using a linked list structure with a start pointer. The main function contains a loop that prompts the user for an operation choice and calls the corresponding function. The add function adds a new node to the end of the queue. The delete function removes a node from the front and returns the deleted value. The traverse function prints all elements currently in the queue.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 33

// Program to find the location of the key value in an array.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],key,i,n,loc=0;
clrscr();
printf("\n Enter no. of elements in array : ");
scanf("%d",&n);
printf("\n Enter the key value want to search : ");
scanf("%d",&key);
printf("\n Enter the elements of array : ");
for(i=1;i<=n;i++)
{
printf("\n Enter elements a[%d] \t",i);
scanf("%d",&a[i]);
}
for(i=1;i<=n;i++)
{
if(a[i]==key)
loc=i;
}
if(loc==0)
printf("\n Item want to search is not present");
else
printf("\n Location of the key value is =%d",loc);
getch();
}

Output :
// Program to insert an array in the given location in an array list.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],key,i,n,loc=0,ub,lb,mid;
clrscr();
printf("\n Enter no. of elements in array : ");
scanf("%d",&n);
printf("\n Enter the key value want to search : ");
scanf("%d",&key);
printf("\n Enter the elements of array : ");
for(i=1;i<=n;i++)
{
printf("\n Enter elements a[%d] \t",i);
scanf("%d",&a[i]);
}
printf("\n\n********************************\n");
lb=1;
ub=n;
mid=((lb+ub)/2);
while((lb<=ub)&&(a[mid]!=key))
{
if(a[mid]<key)
lb=mid+1;
else
ub=mid-1;
mid=((lb+ub)/2);
}
if(a[mid]==key)
{
loc=mid;
printf("\n Location of the key value =%d\n",loc);
}
else
{
printf("\n Item is not present");
}
printf("\n**********************************\n");
getch();
}
Output :
// Program to insert an array in the given location in an array list.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],i,item,n,k,j;
clrscr();
printf("\n Enter no. of elements in array : ");
scanf("%d",&n);
printf("\n Enter the item to be inserted in the array : ");
scanf("%d",&item);
printf("\n Enter the location of the insertion of item : ");
scanf("%d",&k);
printf("\n Enter the elements of the array");
for(i=1;i<=n;i++)
{
printf("\n Enter elements a[%d] \t",i);
scanf("%d",&a[i]);
}
printf("\n ARRAY BEFORE INSERTION \n");
for(i=1;i<=n;i++)
{
printf("\n%d",a[i]);
}
j=n;
while(j>=k)
{
a[j+1]=a[j];
j--;
}
a[k]=item;
n=n+1;
printf("\n ARRAY AFTER INSERTION \n");
for(i=1;i<=n;i++)
{
printf("\n%d",a[i]);
}
getch();
}
Output :
// Program to do bubble sorting.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,j,n,temp;
clrscr();
printf("\n Enter no. of elements in array : ");
scanf("%d",&n);
printf("\n Enter the elements of the array : \n");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<=n;i++)
{
for(j=1;j<n-i-1;j++);
{
if(a[j+1]<a[j])
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}
printf("\n The sorted array by bubble sort is");
for(i=1;i<=n;i++)
printf("\n\n%d",a[i]);
getch();
}
Output :
// Program to delete an array in an array list.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],i,item,n,k,j;
clrscr();
printf("\n Enter no. of elements in array : ");
scanf("%d",&n);
printf("\n Enter the item to be deleted in the array : ");
scanf("%d",&item);
printf("\n Enter the elements of the array : \n");
for(i=1;i<=n;i++)
{
printf("\n Enter elements a[%d] \t",i);
scanf("%d",&a[i]);
}
printf("\n ARRAY BEFORE DELETION \n");
for(i=1;i<=n;i++)
{
printf("\n%d",a[i]);
}
for(i=1;i<=n;i++)
{
if(a[i]==item)
k=i;
}
for(j=k;j<n;j++)
{
a[j]=a[j+1];
}
n--;
printf("\n ARRAY AFTER DELETION \n");
for(i=1;i<=n;i++)
{
printf("\n%d",a[i]);
}
getch();
}
Output :
// Program for doubly linklist.

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int info;
struct node*left;
struct node*right;
};
typedef struct node node;
node*head;
node*insert_cir(node*head)
{
node*ptr,*temp;
int item;
ptr=(node*)malloc(sizeof(node));
printf("\n Enter the no.");
scanf("%d",&item);
ptr->info=item;
temp=head->right;
head->right=ptr;
ptr->left=head;
ptr->right=temp;
temp->left=ptr;
return(head);
}
node*insert_cir_end(node*head)
{
node*ptr,*temp;
int item;
ptr=(node*)malloc(sizeof(node));
printf("\n Enter the No.");
scanf("%d",&item);
ptr->info=item;
temp=head->left;
head->right=ptr;
temp->left=temp;
ptr->right=head;
head->left=ptr;
return(head);
}
node*dele_beg(node*head)
{
node*temp;
if(head->right==head)
{
printf("\n Empty List");
return'\0';
}
else
{
temp=head->right;
printf("\n deleted element is =%d",temp->info);
head->right=temp->left;
temp->left->left=head;
free(temp);
return(head);
}
}
node*dele_end(node*head)
{
node*temp;
if(head->right==head)
{
printf("\n Empty list");
return '\0';
}
else
{
temp=head->left;
printf("deleted element is=%d \n", temp->info);
head->left=temp->left;
free(temp);
return(head);
}
}
void traverse(node*start)
{
node*p;
p=start;
if(p=='\0')
{
printf("\n List is empty");
}
else
{
while(p->right==start)
{
printf("%d",&p->info);
p=p->right;
}}}
void main()
{
int left,right,choice;
clrscr();
printf("\n Enter 1 : Insert at the begining of the link list");
printf("\n Enter 2 : Insert at the end of link list");
printf("\n Enter 3 : Delete at the begining of the link list");
printf("\n Enter 4 : Delete at the end of link list");
printf("\n Enter 5 : Traverse");
printf("\n Enter 6 : End");
do
{
printf("\n Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:insert_cir(head);
break;
case 2:insert_cir_end(head);
break;
case 3:dele_beg(head);
break;
case 4:dele_end(head);
break;
case 5:traverse(head);
break;
case 6:return;
}
}
while(choice!=6);
getch();
}

Output :
// Program to print sum of diagonal elements..

#include<stdio.h>
#include<conio.h>
void main()
{
int a[50][50],i,n,m,j,k=0;
clrscr();
printf("\n Enter the no. of rows : ");
scanf("%d",&m);
printf("\n Enter the no. of columns : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
printf("\n Enter the a[%d][%d] element\t",i,j);
scanf("%d",&a[i][j]);
}}
printf("\n\n MATRIX IS :::::\n\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
printf("\t%d",a[i][j]);
}
printf("\n");
}
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if(a[i][j]==0)
k=k+1;
}}
printf("\n SUM of diagonal elements is::::\t%d",k);
getch();
}
Output :
// Program to print tower of Hanoi.

#include<stdio.h>
#include<conio.h>
void TOH(char,char,char,int);
void TOH(char x,char y,char z,int n)
{
if(n==0)
{
printf("\n Nothing to move");
}
if(n==1)
{
printf("\n Move disk from %c to %c ",x,y);
}
else
{
TOH(x,z,y,n-1);
TOH(x,y,z,1);
TOH(y,x,z,n-1);
}
}
void main()
{
clrscr();
int n;
char x,y,z;
printf("\n Input no of disk : ");
scanf("%d",&n);
printf("\n No. of moves : ");
TOH('x','y','z',n);
getch();
}
Output :
// Program to print Heap Sort.

#include<stdio.h>
#include<conio.h>
void heapsort(int []);
void buildmaxheap(int[]);
void maxheapify(int[],int);
int n,i,j,heapsize;
void main()
{
int a[15];
clrscr();
printf("\n Enter the size of array : ");
scanf("%d",&n);
printf("\n Enter the elements in array : \n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
heapsort(a);
printf("\n Sorted list is below : \n");
for(i=1;i<=n;i++)
printf(" %d ",a[i]);
getch();
}
void heapsort(int a[])
{
int temp;
heapsize=n;
buildmaxheap(a);
for(i=n;i>=1;i--)
{
temp=a[i];
a[i]=a[1];
a[1]=temp;
heapsize=heapsize-1;
//buildmaxheap(a);
maxheapify(a,1);
}
}
void buildmaxheap(int a[])
{
int length=n/2;
for(i=length;i>=1;i--)
maxheapify(a,i);
}
void maxheapify(int a[],int j)
{
int r;
int left,right,largest;
left=2*j;
right=(2*j)+1;
largest=j;
if(left<=heapsize && a[left]>a[j])
{
largest=left;
}
if(right<=heapsize && a[right]>a[largest])
{
largest=right;
}
if(largest!=j)
{
r=a[j];
a[j]=a[largest];
a[largest]=r;
maxheapify(a,largest);
}
}

Output :
// program to delete in an array.

#include<stdio.h>
#include<conio.h>
void delete(int*arr,int pp)
{
Int j;
for(j=pp;p<6;j++)
arr[j-1]=arr[j];
}
void main()
{
int i,p,a[6],n;
clrscr();
printf(“\n Enter the values of array”);
scanf(“%d”,&n);
printf(“\n Enter the values in array”);
for(i=0;i<6;i++)
scanf(“%d”,&a[i]);
printf(“\n Enter the position”);
scanf(“%d”,&p);
delete(&a,p);
printf(“\n Array after deletion”);
for(i=0;i<5;i++)
printf(“\n \n %d”,a[i]);
getch();
}

Output :
//Program for Push Pop and Display..

#include<stdio.h>
#include<conio.h>
struct stack
{
int no;
struct stack *next;
}
*start=NULL;
typedef struct stack st;
void push();
int pop();
void display();
void main()
{
char ch;
int choice,item;
do
{
clrscr();
printf("\n 1: push");
printf("\n 2: pop");
printf("\n 3: display");
printf("\n Enter your choice");
scanf("%d",&choice);
switch (choice)
{
case 1: push();
break;
case 2: item=pop();
printf("The delete element in %d",item);
break;
case 3: display();
break;
default : printf("\n Wrong choice");
};
printf("\n do you want to continue(Y/N)");
fflush(stdin);
scanf("%c",&ch);
}
while (ch=='Y'||ch=='y');
}
void push()
{
st *node;
node=(st *)malloc(sizeof(st));

printf("\n Enter the number to be insert");


scanf("%d",&node->no);
node->next=start;
start=node;
}
int pop()
{
st *temp;
temp=start;
if(start==NULL)
{
printf("stack is already empty");
getch();
exit();
}
else
{
start=start->next;
free(temp);
}
return(temp->no);
}
void display()
{
st *temp;
temp=start;
while(temp->next!=NULL)
{
printf("\nno=%d",temp->no);
temp=temp->next;
}
printf("\nno=%d",temp->no);
}

Output :
// Program for addition, deletion and traversing…

#include<stdio.h>
#include<conio.h>
struct queue
{
int no;
struct queue *next;
}
*start=NULL;
void add();
int del();
void traverse();
void main()
{
int ch;
char choice;
do
{
clrscr();
printf("----1. add\n");
printf("----2. delete\n");
printf("----3. traverse\n");
printf("----4. exit\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: add();
break;
case 2: printf("the delete element is\n%d",del());
break;
case 3: traverse();
break;
case 4: return;
default : printf("wrong choice\n");
};
fflush(stdin);
scanf("%c",&choice);
}
while(choice!=4);
}
void add()
{
struct queue *p,*temp;
temp=start;
p=(struct queue*)malloc(sizeof(struct queue));
printf("Enter the data");
scanf("%d",&p->no);
p->next=NULL;
if(start==NULL)
{
start=p;
}
else
{
while(temp->next!=NULL)
temp=temp->next;
temp->next=p;
}
}
int del()
{
struct queue *temp;
int value;
if(start==NULL)
{
printf("queue is empty");
getch();
return(0);
}
else
{
temp=start;
value=temp->no;
start=start->next;
free(temp);
}
return(value);
}
void traverse()
{
struct queue *temp;
temp=start;
while(temp->next!=NULL)
{
printf("no=%d",temp->no);
temp=temp->next;
}
printf("no=%d",temp->no);
getch();
}
Output :
// Program of shortest path between two node in graph using Djikstra algorithm

#include<stdio.h>
#include<conio.h>
#define MAX 10
#define TEMP 0
#define PERM 1
#define infinity 9999
struct node
{
int predecessor;
int dist; /*minimum distance of node from source*/
int status;
};

int adj[MAX][MAX];
int n;
void main()
{
int i,j;
int source,dest;
int path[MAX];
int shortdist,count; clrscr();

create_graph();
printf("The adjacency matrix is :\n");
display();

while(1)
{
printf("Enter source node(0 to quit) : ");
scanf("%d",&source);
printf("Enter destination node(0 to quit) : ");
scanf("%d",&dest);

if(source==0 || dest==0)
exit(1);

count = findpath(source,dest,path,&shortdist);
if(shortdist!=0)
{
printf("Shortest distance is : %d\n", shortdist);
printf("Shortest Path is : ");
for(i=count;i>1;i--)
printf("%d->",path[i]);
printf("%d",path[i]);
printf("\n");
}
else
printf("There is no path from source to destination node\n");
}/*End of while*/
}/*End of main()*/

create_graph()
{
int i,max_edges,origin,destin,wt;

printf("Enter number of vertices : ");


scanf("%d",&n);
max_edges=n*(n-1);

for(i=1;i<=max_edges;i++)
{
printf("Enter edge %d(0 0 to quit) : ",i);
scanf("%d %d",&origin,&destin);
if((origin==0) && (destin==0))
break;
printf("Enter weight for this edge : ");
scanf("%d",&wt);
if( origin > n || destin > n || origin<=0 || destin<=0)
{
printf("Invalid edge!\n");
i--;
}
else
adj[origin][destin]=wt;
}/*End of for*/
}/*End of create_graph()*/

display()
{
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%3d",adj[i][j]);
printf("\n");
}

}/*End of display()*/
int findpath(int s,int d,int path[MAX],int *sdist)
{
struct node state[MAX];
int i,min,count=0,current,newdist,u,v;
*sdist=0;
/* Make all nodes temporary */
for(i=1;i<=n;i++)
{
state[i].predecessor=0;
state[i].dist = infinity;
state[i].status = TEMP;
}

/*Source node should be permanent*/


state[s].predecessor=0;
state[s].dist = 0;
state[s].status = PERM;

/*Starting from source node until destination is found*/


current=s;
while(current!=d)
{
for(i=1;i<=n;i++)
{
/*Checks for adjacent temporary nodes */
if ( adj[current][i] > 0 && state[i].status == TEMP )
{
newdist=state[current].dist + adj[current][i];
/*Checks for Relabeling*/
if( newdist < state[i].dist )
{
state[i].predecessor = current;
state[i].dist = newdist;
}
}
}/*End of for*/
/*Search for temporary node with minimum distand make it current
node*/
min=infinity;
current=0;
for(i=1;i<=n;i++)
{
if(state[i].status == TEMP && state[i].dist < min)
{
min = state[i].dist;
current=i;
}
}/*End of for*/
if(current==0) /*If Source or Sink node is isolated*/
return 0;
state[current].status=PERM;
}/*End of while*/

/* Getting full path in array from destination to source */


while( current!=0 )
{
count++;
path[count]=current;
current=state[current].predecessor;
}

/*Getting distance from source to destination*/


for(i=count;i>1;i--)
{
u=path[i];
v=path[i-1];
*sdist+= adj[u][v];
}
return (count);
}

Output :
//traversing of tree

#include<stdio.h>
struct rec
{
long num;
struct rec *left;
struct rec *right;
};
struct rec *tree=NULL;
struct rec *insert(struct rec *tree,long num);
void preorder(struct rec *tree);
void inorder(struct rec *tree);
void postorder(struct rec *tree);
int count=1;
main()
{
int choice;
long digit;
do
{
choice=select();
switch(choice)
{
case 1: puts("Enter integer: To quit enter 0");
scanf("%ld",&digit);
while(digit!=0)
{
tree=insert(tree,digit);
scanf("%ld",&digit);
}continue;
case 2: puts("\npreorder traversing TREE");
preorder(tree);continue;
case 3: puts("\ninorder traversing TREEE");
inorder(tree);continue;
case 4: puts("\npostorder traversing TREE");
postorder(tree);continue;
case 5: puts("END");exit(0);
}
}while(choice!=5);
}
int select()
{
int selection;
do
{
puts("Enter 1: Insert a node in the BT");
puts("Enter 2: Display(preorder)the BT");
puts("Enter 3: Display(inorder)the BT");
puts("Enter 4: Display(postorder)the BT");
puts("Enter 5: END");
puts("Enter your choice");
scanf("%d",&selection);
if((selection<1)||(selection>5))
{
puts("wrong choice:Try again");
getch(); }
}while((selection<1)||(selection>5));
return (selection);
}
struct rec *insert(struct rec *tree,long digit)
{
if(tree==NULL)
{
tree=(struct rec *)malloc(sizeof(struct rec));
tree->left=tree->right=NULL;
tree->num=digit;count++;
}
else
if(count%2==0)
tree->left=insert(tree->left,digit);
else
tree->right=insert(tree->right,digit);
return(tree);
}
void preorder(struct rec *tree)
{
if(tree!=NULL)
{
printf("%12ld\n",tree->num);
preorder(tree->left);
preorder(tree->right);
}
}
void inorder(struct rec *tree)
{
if(tree!=NULL)
{
inorder(tree->left);
printf("%12ld\n",tree->num);
inorder(tree->right);
}
}
void postorder(struct rec *tree)

{
if(tree!=NULL)
{
postorder(tree->left);
postorder(tree->right);
printf("%12ld\n",tree->num);
}}

Output :
// Program for searching an array..

#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],i,loc,mid,beg,end,n,flag=0,item;
clrscr();
printf("How many elements");
scanf("%d",&n);
printf("Enter the element of the array\n");
for(i=0;i<=n-1;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element to be searching\n");
scanf("%d",&item);
loc=0;
beg=0;
end=n-1;
while((beg<=end)&&(item!=a[mid]))
{
mid=((beg+end)/2);
if(item==a[mid])
{
printf("search is successfull\n");
loc=mid;
printf("position of the item%d\n",loc+1);
flag=flag+1;
}
if(item<a[mid])
end=mid-1;
else
beg=mid+1;
}
if(flag==0)
{
printf("search is not successfull\n");
}
getch();
Output :

You might also like