Dsu 1
Dsu 1
DSU
int a[20], n;
void create() {
int i;
printf("Enter the size of an array: ");
scanf("%d", &n);
printf("Enter the elements: ");
for(i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
return;
}
void insert() {
int i, ele, pos;
printf("Enter the element to be inserted: ");
scanf("%d", &ele);
printf("Enter the position at which you want to insert the element: ");
scanf("%d", &pos);
for(i = n-1; i >= pos-1; i--) {
a[i+1] = a[i];
}
a[pos-1] = ele;
n++;
}
void delet() {
int pos, i;
printf("Enter the position from which you want to delete: ");
scanf("%d", &pos);
for(i = pos-1; i < n-1; i++) {
a[i] = a[i+1];
}
n--;
}
void display() {
int i;
printf("Entered elements are: ");
for(i = 0; i < n; i++) {
printf("\t%d", a[i]);
}
printf("\n");
return;
}
int main() {
int choice;
while(1) {
printf("\n-----------------------MENU-----------------------");
printf("\n1. Create \n2. Insert \n3. Delete \n4. Display \n5. Exit");
printf("\n----------------------------------------------------");
printf("\n\n Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
create();
break;
case 2:
insert();
break;
case 3:
delet();
break;
case 4:
display();
break;
case 5:
exit(0);
default:
printf("Invalid Choice!!\n");
break;
}
}
return 0;
}
Output: