-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProg1.c
159 lines (154 loc) · 4.5 KB
/
Prog1.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
/* ---------------PROBLEM STATEMENT ---------------
Implement operations (traverse, insert, delete, linear search ) on an array.
*/
#include <stdio.h>
#include <stdlib.h>
void display(int arr[], int count){
printf("\nArray till now: ");
if(count == 0){
printf("Array is empty!");
return;
}
for(int i=0; i< count; i++){
printf("%d ", arr[i]);
}
printf("\n");
}
int main(){
int arr[1000];
int count = 0;
int choice,data,position;
char c;
while (1)
{
printf("\n============================ Main Menu =============================\n");
printf("\nSelect What type of operation you want to do in Array :\n");
printf("1. Insert at begin\n");
printf("2. Insert at end\n");
printf("3. Insert at specific Location\n");
printf("4. Delete at begin\n");
printf("5. Delete at end\n");
printf("6. Delete a specific element\n");
printf("7. Search an element in the array\n");
printf("8. Display the Array\n");
printf("9. Exit\n");
printf("Enter your choice: ");
if (scanf("%d", &choice) != 1)
{
printf("\n***** Please Enter a integer value ****** \n");
while (getchar() != '\n'); // Clear the input buffer
continue;
}
switch (choice)
{
case 1:
printf("Enter the data : ");
scanf("%d", &data);
for(int i = count; i > 0 ; i--){
arr[i] = arr[i-1];
}
arr[0] = data;
count++;
display(arr,count);
break;
case 2:
printf("Enter the data : ");
scanf("%d", &data);
arr[count++] = data;
display(arr,count);
break;
case 3:
ret:
printf("Enter the data : ");
scanf("%d", &data);
printf("Enter the index(0-based) : ");
scanf("%d", &position);
if(position > count){
printf("\nInvalid index!!\n");
printf("Renter the data and index (0-based)\n");
goto ret;
}
for(int i = count; i >= position; i--){
arr[i] = arr[i-1];
}
arr[position] = data;
count++;
display(arr,count);
break;
case 4:
if(count == 0){
display(arr,count);
break;
}
for(int i=0; i < count;i++){
arr[i] = arr[i+1];
}
count--;
display(arr,count);
break;
case 5:
if(count == 0){
display(arr,count);
break;
}
count--;
display(arr,count);
break;
case 6:
printf("\n****REMEMBER : it will delete only the first occurence of the number.****\n");
printf("Enter the data you wish to delete : ");
scanf("%d",&data);
int index = -1 ;
for(int i = 0 ; i < count; i++){
if(arr[i] == data){
index = i;
break;
}
}
if(index == -1){
printf("Element not found\n");
break;
}
for(int i = index; i < count;i++){
arr[i] = arr[i+1];
}
count--;
display(arr,count);
break;
case 7:
printf("Enter the Element you wish to search : ");
scanf("%d",&data);
index = -1;
for(int i=0; i < count;i++){
if(arr[i] == data){
index = i;
break;
}
}
if(index == -1){
printf("Element not found\n");
break;
}
else{
printf("The element is at index %d ", index);
}
break;
case 8:
display(arr,count);
break;
case 9:
printf("Are you sure you want to exit program? (y/n) : ");
scanf(" %c", &c);
if (c == 'y'){
printf("\nExiting the program...\n");
exit(0);
}
else if (c != 'n')
printf("\nInvalid Input!\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}