-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertdelete.c
98 lines (93 loc) · 1.83 KB
/
insertdelete.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
//Program to insert and delete elements into the array | Vishruth codes
#include<stdio.h>
#include<stdlib.h>
int displayed(int a[], int n)
{
printf("\nThe array: ");
for(int i=0;i<n;i++)
{
if(i==n-1)
{
printf("%d.",a[i]);
}
else
{
printf("%d, ",a[i]);
}
}
}
void insertion(int a[], int n)
{
int pos,ele;
printf("\nEnter the position (0-%d): ",n-1);
scanf("%d", &pos);
if(pos>=n || pos<0)
{
printf("\nInvalid position.");
}
else{
printf("\nEnter the element: ");
scanf("%d", &ele);
printf("%d is kicked oy of the array.\n",a[n-1]);
for(int i=n-1;i>=pos;i--)
{
a[i]=a[i-1];
}
a[pos]=ele;
}
}
void deletion(int a[], int *n)
{
int pos;
printf("\nEnter the position (0-%d): ",*n-1);
scanf("%d",&pos);
if(pos>=*n || pos<0)
{
printf("\nInvalid position.");
}
else{
for(int i=pos;i<*n;i++)
{
a[i]=a[i+1];
}
--(*n);
}
}
int main()
{
int cc;
int n=10;
int len;
int ar[10]={11,22,33,44,55,66,77,88,99,110};
displayed(ar,n);
printf("\nThe length of array is: %d", n);
printf("\n\nEnter - \n1. for Insertion\n2. for Deletion\n3. for Exit\n:= ");
scanf("%d",&cc);
switch(cc)
{
case 1:
{
insertion(ar,n);
displayed(ar,n);
printf("\nThe length of array is: %d", n);
break;
}
case 2:
{
deletion(ar,&n);
displayed(ar,n);
printf("\nThe length of array is: %d", n);
break;
}
case 3:
{
exit(0);
break;
}
default:
{
printf("\nInvalid Input!");
}
}
return 0;
}