0% found this document useful (0 votes)
16 views6 pages

Assignment DSA

The document outlines a practical assignment for a Data Structure course, where students are required to implement a menu-driven program for operations on a linear array. The program includes functions for inserting and deleting elements, as well as displaying the array contents. It provides detailed algorithms and code snippets for each operation, along with a complete main function to integrate all functionalities.

Uploaded by

info.vipul2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views6 pages

Assignment DSA

The document outlines a practical assignment for a Data Structure course, where students are required to implement a menu-driven program for operations on a linear array. The program includes functions for inserting and deleting elements, as well as displaying the array contents. It provides detailed algorithms and code snippets for each operation, along with a complete main function to integrate all functionalities.

Uploaded by

info.vipul2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

EXPERIMENT - 1.

Student Name: Bipul Kumar S.no: 65


Branch: Computer Science and Engineering Section: A6
Semester: 3rd Date of Submission:
Subject Name: Data Structure Subject Code:

Aim of Practical: Write a menu driven program that implement following operations
(using separate functions) on a linear array:
a) Insert a new element at end as well as at a given position
b) Delete an element from a given whose value is given or whose position is given
c) To display the elements of the linear array

Algorithm: Insert a New Element at End and at a Given Position

Insert at End:
1. Check if the array is full.
2. If not, add the new element at the next available index.
3. Increment the array size.

Insert at a Given Position:


1. Check if the array is full.
2. Validate the position (should be within valid bounds).
3. Shift all elements to the right starting from the given position.
4. Insert the new element at the given position.
5. Increment the array size.
Code:
#include <stdio.h>
void insertAtEnd(int arr[], int *n, int element) {
if (*n >= MAX_SIZE) {
printf("Array is full! Cannot insert.\n");
return;
}
arr[*n] = element;
(*n)++;
printf("Inserted %d at the end.\n", element);
}
void insertAtPosition(int arr[], int *n, int element, int position) {
if (*n >= MAX_SIZE) {
printf("Array is full! Cannot insert.\n");
return;
}
if (position < 0 || position > *n) {
printf("Invalid position!\n");
return;
}
for (int i = *n; i > position; i--) {
arr[i] = arr[i - 1];
}
arr[position] = element;
(*n)++;
printf("Inserted %d at position %d.\n", element, position);
}
Algorithm: Delete an Element by Value or Position
Delete by Value:
1. Find the first occurrence of the value in the array.
2. If the value exists, shift all elements to the left starting from the found position.
3. Decrement the array size.
Delete by Position:
1. Validate the position.
2. If valid, shift all elements to the left starting from the given position.
3. Decrement the array size.

Code:
void deleteByValue(int arr[], int *n, int value) {
int index = -1;
for (int i = 0; i < *n; i++) {
if (arr[i] == value) {
index = i;
break;
}
}
if (index == -1) {
printf("Element not found!\n");
return;
}
for (int i = index; i < *n - 1; i++) {
arr[i] = arr[i + 1];
}
(*n)--;
printf("Deleted element %d.\n", value);
}
void deleteByPosition(int arr[], int *n, int position) {
if (position < 0 || position >= *n) {
printf("Invalid position!\n");
return;
}
for (int i = position; i < *n - 1; i++) {
arr[i] = arr[i + 1];
}
(*n)--;
printf("Deleted element at position %d.\n", position);
}

Algorithm: Display the Elements of the Linear Array


1. Check if the array is empty.
2. If not, iterate through the array and print each element.

Code:
void displayArray(int arr[], int n) {
if (n == 0) {
printf("Array is empty!\n");
return;
}
printf("Current Array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
Complete Program:
To integrate all functionalities, you can use a menu-driven structure:
int main() {
int arr[MAX_SIZE];
int n = 0, choice, element, position, value;

while (1) {
printf("\nMenu:\n");
printf("1. Insert at End\n");
printf("2. Insert at Position\n");
printf("3. Delete by Value\n");
printf("4. Delete by Position\n");
printf("5. Display Array\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the element to insert at the end: ");
scanf("%d", &element);
insertAtEnd(arr, &n, element);
break;
case 2:
printf("Enter the element to insert: ");
scanf("%d", &element);
printf("Enter the position (0-based index): ");
scanf("%d", &position);
insertAtPosition(arr, &n, element, position);
break;
case 3:
printf("Enter the value to delete: ");
scanf("%d", &value);
deleteByValue(arr, &n, value);
break;
case 4:
printf("Enter the position to delete (0-based index): ");
scanf("%d", &position);
deleteByPosition(arr, &n, position);
break;
case 5:
displayArray(arr, n);
break;
case 6:
printf("Exiting program. Goodbye!\n");
return 0;
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}

Output:

You might also like