Various Operations On An Array:
- Insert an element to an array
- Delete an element from an array
- Traverse the given array
- Search an element in the given array
- Reverse the elements in an array
The below program illustrates the above operations on an array.
See Also:
See Also:
C Program To Merge Two Arrays
C Program For Array Representation Of Sparse Matrix
C Program To Perform Insertion, Deletion, Searching & Traversal In Singly Linked List
C Program To Perform Insertion, Deletion, Sorting In Doubly Linked List - (Simple)
C Program To Sort A Doubly Linked List (Descending Order)
C Program To Reverse Linked List
C Program To Implement Circular Singly Linked List
C Program To Implement Circular Doubly Linked List
C Program For Polynomial Multiplication Using Linked List
C Program For Polynomial Addition Using Linked List
C Program For Linked List Representation Of Sparse Matrix
C Program To Concatenate Two Linked Lists
C Program To Perform Recursion On Linked List
C Program For Array Representation Of Sparse Matrix
C Program To Perform Insertion, Deletion, Searching & Traversal In Singly Linked List
C Program To Perform Insertion, Deletion, Sorting In Doubly Linked List - (Simple)
C Program To Sort A Doubly Linked List (Descending Order)
C Program To Reverse Linked List
C Program To Implement Circular Singly Linked List
C Program To Implement Circular Doubly Linked List
C Program For Polynomial Multiplication Using Linked List
C Program For Polynomial Addition Using Linked List
C Program For Linked List Representation Of Sparse Matrix
C Program To Concatenate Two Linked Lists
C Program To Perform Recursion On Linked List
C Program - Insert, delete, search, reverse & traversal operation on Arrays:
#include <stdlib.h>
#define MAXVAL 100
int count = 0;
/* insert value 'val' at the given position 'pos' */
void insertData(int *array, int val, int pos) {
int i, temp1, temp2;
if (count < pos) {
array[count++] = val;
} else {
temp1 = array[pos - 1];
array[pos - 1] = val;
for (i = pos; i < count; i++) {
temp2 = array[i];
array[i] = temp1;
temp1 = temp2;
}
array[count++] = temp1;
}
return;
}
/* delete val from the given array */
void deleteData(int *array, int val) {
int i, flag = 0, pos;
for (i = 0; i < count; i++) {
if (val == array[i]) {
pos = i + 1;
flag = 1;
break;
}
}
if (!flag) {
printf("Given data is not present in Array!!\n");
} else {
for (i = pos; i < count; i++)
array[i - 1] = array[i];
count--;
}
return;
}
/* search val in the given array */
void searchData(int *array, int val) {
int i;
for (i = 0; i < count; i++) {
if (array[i] == val) {
printf("%d is present in the input array!!\n", val);
return;
}
}
printf("%d is not present in given inputs array!!\n", val);
return;
}
/* reverse the data in given array */
void reverseData(int *array) {
int i, temp;
for (i = 0; i < count / 2; i++) {
temp = array[i];
array[i] = array[count - 1 - i];
array[count - 1 -i] = temp;
}
}
/* print the contents of the array */
void display(int *array) {
int i;
for (i = 0; i < count; i++) {
printf("%d ", array[i]);
}
printf("\n\n");
return;
}
int main(int argc, char **argv) {
int i, data[MAXVAL];
if (argc >= MAXVAL) {
printf("Input count exceeded!!\n");
return 0;
}
for (i = 1; i < argc; i++) {
insertData(data, atoi(argv[i]), i);
}
printf("Inserting 100 in position 3\n");
insertData(data, 100, 3);
display(data);
printf("Deleting 10 & 20 form the array\n");
deleteData(data, 10);
deleteData(data, 20);
display(data);
searchData(data, 100);
printf("Insert 10 & 20 at the position 1 & 2 correspondingly\n");
insertData(data, 10, 1);
insertData(data, 20, 2);
printf("After inserting 10 and 20:\n");
display(data);
reverseData(data);
printf("After revers operation:\n");
display(data);
return 0;
}
Output: (Operations on Arrays - Example C Program)
jp@jp-VirtualBox:~/$ ./a.out 300 270 290 199 324 10 20
Inserting 100 in position 3
300 270 100 290 199 324 10 20
Deleting 10 & 20 form the array
300 270 100 290 199 324
100 is present in the input array!!
Insert 10 & 20 at the position 1 & 2 correspondingly
After inserting 10 and 20:
10 20 300 270 100 290 199 324
After revers operation:
324 199 290 100 270 300 20 10
Inserting 100 in position 3
300 270 100 290 199 324 10 20
Deleting 10 & 20 form the array
300 270 100 290 199 324
100 is present in the input array!!
Insert 10 & 20 at the position 1 & 2 correspondingly
After inserting 10 and 20:
10 20 300 270 100 290 199 324
After revers operation:
324 199 290 100 270 300 20 10
No comments:
Post a Comment