Write an interactive program to do the following operations on a list:
i) insert element at desired location.
ii) delete an element of users choice.
iii) find the number of occurrence of each element.
Answers
Answer:
Program to insert, delete and search an element in an array is discussed here. Given an array, the array operations like insert, delete and search an element in an array are performed and the result is displayed.
If the position where the element to be inserted is greater than the size of an array display Invalid Input.
Input format:
Input consists of 3 integers and 1 array.
Input the size of the array.
Input the array elements.
Input the position where the element should be inserted.
Input the element to be inserted in that position.
Sample Input:
5 (size of the array)
1 (array elements)
2
3
4
5
4 (Position)
10 (Element to be inserted)
Sample Output:
1
2
3
10
4
5
Algorithm to insert, delete and search an element in an array
Insertion
Input the array elements, the position of the new element to be inserted and the new element.
Insert the new element at that position and shift the rest of the elements to right by one position.
Deletion
Input the array elements, the position of the new element to be inserted and the new element.
Delete the element and shift the rest of the elements to left by one position.
Search
Input the array elements, the element to be searched.
Traverse the array and check if the element is present in the array.
Display "Yes" if it is present in the array, else display "No".