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

week_1_DS

The document describes a C program that implements basic operations on an array, including insertion, deletion, and searching based on user input. It allows users to create an array, insert elements at specified positions, delete elements, display the current array, and search for elements. These operations are foundational for understanding more complex data structures and algorithms in software development.

Uploaded by

lavanya.d
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

week_1_DS

The document describes a C program that implements basic operations on an array, including insertion, deletion, and searching based on user input. It allows users to create an array, insert elements at specified positions, delete elements, display the current array, and search for elements. These operations are foundational for understanding more complex data structures and algorithms in software development.

Uploaded by

lavanya.d
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

AIM: Implement basic operations on array: insertion, deletion, searching

PROBLEM DESCRIPTION:

 The program should handle searching, insertion, and deletion of elements in an array based
on user input.
 An integer array of fixed size initialized by the user or hardcoded.
 The size of the array should be maintained to track the current number of elements.
 User choices to perform:
1. Searching for an element.
2. Inserting an element at a specified position.
3. Deleting an element from a specified position.

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define maxsize 10
int arr[maxsize],n;
void Create();
void Insert();
void Delete();
void Display();
void Search();
void main()
{
int choice;
do
{
printf("\n Array Implementation of List\n");
printf("\t1.create\n");
printf("\t2.Insert\n");
printf("\t3.Delete\n");
printf("\t4.Display\n");
printf("\t5.Search\n");
printf("\t6.Exit\n");
printf("\nEnter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1: Create();
break;
case 2: Insert();
break;
case 3: Delete();
break;
case 4: Display();
break;
case 5: Search();
break;
case 6: exit(1);
default: printf("\nEnter option between 1 - 6\n");
break;
}
}while(choice<7);
}
void Create()
{
int i;
printf("\nEnter the number of elements to be added in the list:\t");
scanf("%d",&n);
printf("\nEnter the array elements:\t");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
Display();
}
void Insert()
{
int i,data,pos;
printf("\nEnter the data to be inserted:\t");
scanf("%d",&data);
printf("\nEnter the position at which element to be inserted:\t");
scanf("%d",&pos);
for(i = n-1 ; i >= pos-1 ; i--)
arr[i+1] = arr[i];
arr[pos-1] = data;
n+=1;
Display();
}
void Delete( )
{
int i,pos;
printf("\nEnter the position of the data to be deleted:\t");
scanf("%d",&pos);
printf("\nThe data deleted is:\t %d", arr[pos-1]);
for(i=pos-1;i<n-1;i++)
arr[i]=arr[i+1];
n=n-1;
Display();
}
void Display()
{
int i;
printf("\n**********Elements in the array**********\n");
for(i=0;i<n;i++)
printf("%d\t",arr[i]);
}
void Search()
{
int search,i,count = 0;
printf("\nEnter the element to be searched:\t");
scanf("%d",&search);
for(i=0;i<n;i++)
{
if(search == arr[i])
{
count++;
printf("\nElement present in the list at location %d",i);
}
}
if(count==0)
printf("\nElement not present in the list");
}
Input and Output Processing:
CONCLUSION:
These operations form the basis for more advanced data structures and algorithms, such as linked lists,
dynamic arrays, and searching/sorting techniques. Mastering them is an essential step toward solving
real-world problems efficiently in software development.

You might also like