-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArray.c
218 lines (207 loc) · 4.74 KB
/
Array.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*In exams, remember: Insertion methods - insertion by giving position or insertion by order are usually asked separately. For insertion by order, no need for sorting, just insert each element in order specified. Streamline your strategy for efficient preparation. Good luck!*/
#include<stdio.h>
#include<stdlib.h>
int ne=0;
int *x;
int size;
void disp()
{
int i;
if(ne==0)
{
printf("Array empty\n");
return;
}
printf("The Array elements you Entered are : \n");
for(i=0;i<ne;i++)
printf("%d\t",*(x+i));
printf("\n");
}
void insert_by_pos()
{
int i,pos,ele;
if(ne==size)
{
printf("Array is full.\n");
printf("Reallocating Array\n");
size++;
x=(int*)realloc(x,size*sizeof(int));
}
printf("Enter The element to be inserted : \n");
scanf("%d",&ele);
printf("Enter The position where The elements should be inserted\n");
scanf("%d",&pos);
if(pos>=1&&pos<=ne+1)
{
for(i=ne-1;i>=pos-1;i--)
{
*(x+i+1)=*(x+i);
}
*(x+i+1)=ele;
ne++;
}
else
printf("invalid position\n");
}
void insert_by_order()
{
int i,ele;
if(ne==size)
{
printf("Array is full.\n");
printf("Reallocating Array \n");
size++;
x=(int*)realloc(x,size*sizeof(int));
}
printf("Enter The element to be inserted : ");
scanf("%d",&ele);
i=ne-1;
while(i>=0&&*(x+i)>ele)
{
*(x+i+1)=*(x+i);
i--;
}
*(x+i+1)=ele;
ne++;
}
void delete_by_pos()
{
int i,pos;
if(ne==0)
{
printf("Array is empty.\n");
return;
}
printf("Enter The position from where elements should be deleted\n");
scanf("%d",&pos);
if(pos>=1&&pos<=ne)
{
for(i=pos-1;i<ne-1;i++)
{
*(x+i)=*(x+i+1);
}
ne--;
}
else
printf("invalid position.\n");
}
void delete_by_ele()
{
int i,ele;
if(ne==0)
{
printf("Array is empty.\n");
return;
}
printf("Enter The elements that should be deleted\n");
scanf("%d",&ele);
for(i=0;i<ne&&*(x+i)!=ele;i++);
if(i==ne)
{
printf("element not found in The Array list\n");
return;
}
for(;i<ne-1;i++)
{
*(x+i)=*(x+i+1);
}
ne--;
}
void search_by_key()
{
int i,ele,f=0;
if(ne==0)
{
printf("Array is empty\n");
return;
}
printf("Enter The element that should be searched\n");
scanf("%d",&ele);
for(i=0;i<ne;i++)
{
if(*(x+i)==ele)
{
printf("element found at position %d\n",i+1);
f=1;
break;
}
}
if(f==0)
printf("element not found\n");
}
void search_by_pos()
{
int pos,i;
if(ne==0)
{
printf("Array is empty\n");
return;
}
printf("Enter The position from where element should be searched\n");
scanf("%d",&pos);
if(pos>=1&&pos<=ne)
{
i=pos-1;
printf("element at %d is %d\n",pos,*(x+i));
}
else
printf("invalid position\n");
}
void reverse()
{
int t,i;
if(ne==0)
{
printf("Array is empty\n");
return;
}
for(i=0;i<ne/2;i++)
{
t=*(x+i);
*(x+i)=*(x+ne-i-1);
*(x+ne-i-1)=t;
}
}
void main()
{
int choice;
printf("Enter The size\n");
scanf("%d",&size);
x=(int *)malloc(size*sizeof(int));
printf("Menu\n1.Read\n2.Display\n3.Insert by position\n4.Insert by order\n5.Delete by position\n6.Delete by element\n7.Search by key\n8.Search by position\n9.Reverse\n10.Exit\n");
for(;;)
{
printf("Enter Your Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: read();
disp();
break;
case 2: disp();
break;
case 3: insert_by_pos();
disp();
break;
case 4: insert_by_order();
disp();
break;
case 5: delete_by_pos();
disp();
break;
case 6: delete_by_ele();
disp();
break;
case 7: search_by_key();
break;
case 8: search_by_pos();
break;
case 9: reverse();
disp();
break;
case 10 :
exit(0);
default: printf("Invalid Choice\n");
}
}
}