1-Write A Program To Implement The Following Operations On One Dimensional Array A) Insertion
1-Write A Program To Implement The Following Operations On One Dimensional Array A) Insertion
1-Write A Program To Implement The Following Operations On One Dimensional Array A) Insertion
dimensional array
a) INSERTION
#include<conio.h>
void main()
{
int a[10],i,p,x,n=5;
clrscr();
printf("Enter the elements of array\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
i = n-1;
while(i>=p){
a[i] = a[i-1];
i--;
}
a[p-1] = x;
void main ()
{
int arr[7],pos,i;
clrscr();
printf("Enter the elements of array\n");
for(i=0;i<7;i++)
scanf("%d",&arr[i]);
void main()
{
int a[100],i,n;
clrscr();
getch();
}
d) REVERSE
#include<conio.h>
void main()
{
int a[5],i;
clrscr();
printf("Enter elements of array:\n");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
printf("\nReversed Array:\n");
for(i=4;i>=0;i--)
printf("%d\n",a[i]);
getch();
}
e) MERGING
#include <conio.h>
void main()
{
int a[5], a1[5], i, j, c[10];
clrscr();
printf("Enter element of array1\n");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
void main()
{
int a[7];
int i;
clrscr();
printf("Enter the elements of array\n");
for(i=0;i<7;i++)
scanf("%d",&a[i]);
for(i=0;i<7;i++)
printf("%d ",a[i]);
printf("\nSorted Array\n");
mergeSort(a, 0, 6);
for(i=0;i<7;i++)
printf("%d ",a[i]);
getch();
}
c) INSERTION SORT
#include<conio.h>
void main()
{
int a[5],i,j,temp;
clrscr();
printf("Enter the elements of array\n");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
for(i=0;i<5;i++)
printf("%d ",a[i]);
getch();
}
2- Write a program to implement a Singly Linked List.
#include<conio.h>
#include<stdio.h>
struct node
{
int data;
struct node *link;
};
struct node *head, *temp, *ptr;
//main fn
void main()
{
int c = 1;
clrscr();
head = NULL;
while(c)
{
ptr = (struct node *)malloc(sizeof(struct node));
printf("Enter Data\n");
scanf("%d",&ptr->data);
ptr->link = NULL;
if(head == NULL)
head = temp = ptr;
else
{
temp->link = ptr;
temp = ptr;
}
printf("Do you want to continue(0/1)\n");
scanf("%d",&c);
}
printf("Elements\n");
while(temp!=NULL)
{
temp = head;
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp = temp->link;
}
}
getch();
}
3- Write a program to implement a Circular Linked List.
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head,*temp,*p;
void print()
{
temp=head;
while(temp->next!=head)
{
printf("%d\n",temp->data);
temp=temp->next;
}
printf("%d",temp->data);
}
void main()
{
int c=1;
clrscr();
while(c){
p=(struct node*)malloc(sizeof(struct node));
printf("Enter data\n");
scanf("%d",&p->data);
if(head==NULL)
head=temp=p;
else{
temp->next=p;
temp=p;}
temp->next=head;
printf("Continue(0/1)\n");
scanf("%d",&c);
}
printf("Elements\n");
print();
getch();
}
5- Write a program to implement a Doubly Linked List.
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *pre;
struct node *next;
};
struct node *head,*temp,*p;
void main()
{
int c=1; head=NULL;
while(c){
p=(struct node*)malloc(sizeof(struct node));
printf("\n Enter data");
scanf("%d",&p->data);
p->next=NULL; p->pre=NULL;
if(head==NULL)
{
head=temp=p;
}
else
{
temp->pre=temp; temp->next=p; temp=p;
}
printf("\n Continue (0/1)");
scanf("%d",&c);
}
printf("Elements\n");
while(temp!=NULL)
{
temp = head;
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp = temp->next;
}
}
getch();
}
6- Write a program to display the location of an element specified by
the user in an array using
LINEAR SEARCH
#include<conio.h>
void main()
{
int a[5],i,n,f=0;
clrscr();
printf("Enter Values: ");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
printf("Enter the number you want to find: ");
scanf("%d",&n);
for(i=0;i<5;i++)
{if(n==a[i])
{f=i+1;
printf("Number = %d at position = %d",n,f);
break;}
}
if(i==5)
printf("\nNumber not found");
getch();
}
BINARY SEARCH
#include<conio.h>
void main()
{
int a[5],i,s,f,l,m; //f,l,m : first, last, middle
clrscr();
printf("Enter the elements of an Array");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
f=0;
l=4;
m=(f+l)/2;
while(f<=l)
{
if(a[m]<s)
f = m+1;
else if(a[m] == s)
{
printf("Element found at postion %d",m);
break;
}
else
f = m-1;
m = (f+l)/2;
}
if(f>l)
printf("Element not found");
getch();
}
7- Write a program to accept a matrix from user, find out matrix is
sparse or not and convert it into triplet matrix.