0% found this document useful (0 votes)
537 views19 pages

C Program for Array Operations

The document provides C code snippets to implement various operations on arrays and linked lists, including: 1. Array operations - insertion, deletion, traversal, reversal, merging. 2. Sorting arrays - bubble sort, merge sort, insertion sort, selection sort. 3. Linked list implementations - singly linked list, circular linked list, doubly linked list. 4. Searching in arrays - linear search and binary search to find an element and return its index. 5. Converting a matrix to triplet format if it is sparse.

Uploaded by

Ansh sharma
Copyright
© All Rights Reserved
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)
537 views19 pages

C Program for Array Operations

The document provides C code snippets to implement various operations on arrays and linked lists, including: 1. Array operations - insertion, deletion, traversal, reversal, merging. 2. Sorting arrays - bubble sort, merge sort, insertion sort, selection sort. 3. Linked list implementations - singly linked list, circular linked list, doubly linked list. 4. Searching in arrays - linear search and binary search to find an element and return its index. 5. Converting a matrix to triplet format if it is sparse.

Uploaded by

Ansh sharma
Copyright
© All Rights Reserved
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
  • Array Operations
  • Array Sorting Techniques
  • Linked List Implementations
  • Searching Techniques
  • Sparse Matrix Conversion

1- Write a program to implement the following operations on one

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]);

//taking user input(number to be entered)


printf("Enter the number you want to enter in the array\n");
scanf("%d",&x);

printf("Enter the position at which you want the number to be inserted\n");


scanf("%d",&p);
n++;

i = n-1;
while(i>=p){
a[i] = a[i-1];
i--;
}

a[p-1] = x;

printf("Array after insertion of new element\n");


for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}
b) DELETION
#include <conio.h>

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]);

// enter the position of the element to be deleted


printf("Position of the element you want to delete\n ");
scanf (" %d", &pos);

// check whether the deletion is possible or not


if (pos >= 8)
printf ("\nDeletion is not possible in the array.");
else
{
for(i = pos - 1; i < 6; i++)
arr[i] = arr[i+1];
}

printf ("\nThe resultant array is: \n");

// display the final array


for (i = 0; i< 6; i++)
printf (" %d \n", arr[i]);
getch();
}
c) TRAVERSAL
#include<conio.h>

void main()
{
int a[100],i,n;
clrscr();

printf("Enter the size of array ");


scanf("%d",&n);

printf("\nEnter the elements of array\n");


for(i=0;i<n-1;i++)
scanf("%d",&a[i]);

printf("\nTraversing every element of array\n");


for(i=0;i<n-1;i++)
printf("%d\n",a[i]);

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]);

printf("Enter element of array2\n");


for(i=0;i<5;i++)
scanf("%d",&a1[i]);

//copying array 1 elements into an array


for (i = 0; i<5; i++)
c[i] = a[i];

//copying array 2 elements into an array


for (i=0,j=5;j<10 && i<5;i++,j++)
c[j] = a1[i];

//Array Elements After Merging


printf(“Elements after merging\n”)
for (i = 0; i<10; i++)
printf("%d\n", c[i]);
getch();
}
1- Write a program to sort an array using menu driven:
a) BUBBLE SORT
#include<conio.h>
void main()
{
int a[5],i,j,temp,flag=1;
clrscr();
printf("Enter the elements of array\n");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
printf("\nElements before sorting\n");
for(i=0;i<5;i++)
printf("%d ",a[i]);
printf("\nElements after sorting\n");
//for(i=0;i<5;i++)
while(flag!=0)
{
flag = 0;
for(j=0;j<4;j++)
{
if(a[j]>a[j+1]){
temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
flag = 1;
}
}
}
for(i=0;i<5;i++)
printf("%d ",a[i]);
getch();
}
b) MERGE SORT
#include<conio.h>
void merge(int a[], int mid, int low, int high)
{
int i,j,k,temp[7];
i = low,j = mid+1,k = low;
while (i <= mid && j <= high)
{
if (a[i] < a[j])
{
temp[k++] = a[i++];
/*i++;
k++;*/
}
else
{
temp[k++] = a[j++];
/*j++;
k++;*/
}
}
while (i <= mid)
{
temp[k++] = a[i++];
/*k++;
i++;*/
}
while (j <= high)
{
temp[k++] = a[j++];
/*k++;
j++;*/
}
for(i=low;i<=high;i++)
a[i] = temp[i];
}

void mergeSort(int a[], int low, int high)


{
int mid;
if(low<high)
{
mid = (low + high) /2;
mergeSort(a, low, mid);
mergeSort(a, mid+1, high);
merge(a, mid, low, high);
}
}

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]);

printf("\nElements before sorting\n");


for(i=0;i<5;i++)
printf("%d ",a[i]);

printf("\nElements after sorting\n");


for(i=0;i<5;i++)
{
temp = a[i];
j = i-1;
while(j>=0 && a[j]>temp)
{
a[j+1] = a[j];
j--;
}
a[j+1] = temp;
}
for(i=0;i<5;i++)
printf("%d ",a[i]);
getch();
}
d) SELECTION SORT
#include<conio.h>
void main()
{
int i,j,temp,min,a[5];
clrscr();
printf("Enter the elements of an array\n");
for(i=0;i<5;i++)
scanf("%d",&a[i]);

printf("\nElements before sorting\n");


for(i=0;i<5;i++)
printf("%d ",a[i]);

printf("\nElements after selection sort\n");


for(i=0;i<5;i++)
{
min = i;
for(j=i+1;j<5;j++)
{
if(a[j]<a[min])
min = j;
}

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]);

printf("Enter the element you want to search ");


scanf("%d",&s);

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.

1- Write a program to implement the following operations on one 
dimensional array
a) INSERTION
 
 
#include<conio.h>
void ma
b) DELETION
 
 
#include <conio.h>  
  
void main ()
{
int arr[7],pos,i;
clrscr();
printf("Enter the elements of array
");
f
c) TRAVERSAL
 
 
#include<conio.h>
void main()
{
int a[100],i,n;
clrscr();
printf("Enter the size of array ");
scanf("%d",&n)
d) REVERSE
 
 
#include<conio.h>
void main()
{
int a[5],i;
clrscr();
printf("Enter elements of array:
");
for(i=0;i<5;i++)
s
e) MERGING
 
 
#include <conio.h>
void main()
{
int a[5], a1[5], i, j, c[10];
clrscr();
printf("Enter element of array1
");
1- Write a program to sort an array using menu driven:
a) BUBBLE SORT
 
 
#include<conio.h>
void main()
{
int a[5],i,j,temp,f
b) MERGE SORT
 
 
#include<conio.h>
void merge(int a[], int mid, int low, int high)
{
int i,j,k,temp[7];
i = low,j = mid+1,k
{
int a[7];
int i;
clrscr();
printf("Enter the elements of array
");
for(i=0;i<7;i++)
scanf("%d",&a[i]);
for(i=0;i<7;i++)
pr
c) INSERTION SORT
 
 
#include<conio.h>
void main()
{
int a[5],i,j,temp;
clrscr();
printf("Enter the elements of array
");
f
d) SELECTION SORT
 
 
#include<conio.h>
void main()
{
int i,j,temp,min,a[5];
clrscr();
printf("Enter the elements of an array

You might also like