Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- // Function to delete an element from the array-based list
- void deleteElement(int arr[], int *n, int pos) {
- // Check if the position is valid
- if (pos < 0 || pos >= *n) {
- printf("Invalid position!\n");
- return;
- }
- // Shift elements to the left to fill the gap created by deletion
- for (int i = pos; i < *n - 1; i++) {
- arr[i] = arr[i + 1];
- }
- // Decrease the size of the list
- (*n)--;
- printf("Element deleted successfully.\n");
- }
- // Function to display the list
- void displayList(int arr[], int n) {
- printf("List: ");
- for (int i = 0; i < n; i++) {
- printf("%d ", arr[i]);
- }
- printf("\n");
- }
- int main() {
- int arr[100]; // Array-based list of size 100
- int n = 5; // Initial size of the list
- // Initialize the list with some elements
- arr[0] = 10;
- arr[1] = 20;
- arr[2] = 30;
- arr[3] = 40;
- arr[4] = 50;
- printf("Original ");
- displayList(arr, n);
- // Delete element at position 2 (0-based index)
- deleteElement(arr, &n, 2);
- printf("After deletion ");
- displayList(arr, n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement