-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharrayInsertion.c
124 lines (106 loc) · 2.17 KB
/
arrayInsertion.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Insertion in Array as Abstract Data Type
#include <stdio.h>
#include <stdlib.h>
#define CAPACITY 100
int size = 6;
void insertAtIndex();
void display();
void insertAtBeg();
void insertAtEnd();
void main()
{
int arr[CAPACITY] = {1, 5, 0, 3, 5, 4};
int ch, x;
int in, y;
M:
printf("Press 1 to insert at Begning\n");
printf("Press 2 to insert at End\n");
printf("Press 3 to insert at given index\n");
printf("Press 4 to display the array\n");
printf("Enter choice here : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter the element to be inserted : ");
scanf("%d", &x);
insertAtBeg(arr, x);
break;
case 2:
printf("Enter the element to be inserted : ");
scanf("%d", &x);
insertAtEnd(arr, x);
break;
case 3:
printf("Enter the element to be inserted : ");
scanf("%d", &x);
printf("Enter valid index : \n");
scanf("%d", &in);
insertAtIndex(arr, in, x);
break;
case 4:
display(arr);
break;
default:
printf("SYNTAX ERROR\n");
break;
}
printf("\nPress 1 to ReInsert Data : ");
scanf("%d", &y);
if (y == 1)
{
goto M;
}
else
{
exit(0);
}
}
void insertAtIndex(int arr[], int index, int x)
{
if (size >= CAPACITY)
{
printf("Array Capacity is FULL !!!!!");
return;
}
if (index == 0)
{
insertAtBeg(arr, x);
return;
}
else if (index == size - 1)
{
insertAtEnd(arr, x);
return;
}
for (int i = size - 1; i >= index; i--)
{
arr[i + 1] = arr[i];
}
arr[index] = x;
size = size + 1;
printf("ELEMENT INSERTED\n");
}
void display(int arr[])
{
for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
void insertAtBeg(int arr[], int x)
{
for (int i = size - 1; i >= 0; i--)
{
arr[i + 1] = arr[i];
}
arr[0] = x;
size = size + 1;
printf("ELEMENT INSERTED\n");
}
void insertAtEnd(int arr[], int x)
{
arr[size++] = x;
printf("ELEMENT INSERTED\n");
}