-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertdelete.c
111 lines (105 loc) · 1.61 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
99
100
101
102
103
104
105
106
107
108
109
110
111
#include<stdio.h>
#include<stdlib.h>
#define max 10
int a[max],n,i;
void readarr();
void disparr();
void insert();
void delet();
void main()
{
int ch,flag=0;
while(1)
{
printf("1 for Read\n2 for Display\n3 for insert\n4 for Delete\n5 for Exit");
printf("Enter the choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:readarr();
flag=1;
break;
case 2:if(flag==0)
{
printf("Array is not declared\n");
}
else
disparr();
break;
case 3:if(flag==0)
{
printf("Array is not declared\n");
}
else
insert();
break;
case 4:if(flag==0)
{
printf("Array is not declared\n");
}
else
delet();
break;
case 5:exit (0);
break;
}
}
}
void readarr()
{
printf("Enter the total number of elements in an array:\n");
scanf("%d",&n);
if(n>=max)
{
printf("Elements are excess.\n");
exit (0);
}
if(n==0)
{
printf("It is a empty array.\n");
exit (0);
}
printf("Enter the %d array elements:\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
}
void disparr()
{
printf("The enterd %d array elements are:\n",n);
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
}
void insert()
{
int item,pos;
printf("Enter the position of the element where you want to insert;\n");
scanf("%d",&pos);
printf("Enter the key element:\n");
scanf("%d",&item);
for(i=n-1;i>=pos;i--)
{
a[i+1]=a[i];
}
a[pos]=item;
n++;
}
void delet()
{
int pos;
printf("Enter the posiont of the elements which you want to delete:\n");
scanf("%d",&pos);
if(pos>=n)
{
printf("Position is invalid.\n");
exit (0);
}
for(i=pos;i<n-1;i++)
{
a[i]=a[i+1];
}
n--;
}