Professional Documents
Culture Documents
Arrays
Arrays
#include <stdio.h>
#include <stdlib.h>
int main() {
*ptr = 20;
int *dynamicArray;
int size = 5;
if (dynamicArray == NULL) {
return 1;
dynamicArray[i] = i * 2;
}
printf("\n");
free(dynamicArray);
return 0;
2. Similarities:
Data Organization: All three data structures organize and store elements of various data types.
Access: Elements in arrays, records, and linked lists can be accessed using specific methods or
indices.
Data Types: They can store elements of different data types, including integers, floats, characters,
and user-defined types.
Differences:
Memory Allocation:
Arrays: Contiguous memory allocation. All elements in an array are stored in adjacent memory
locations.
Records: Also known as structures, records store elements of different data types under a single
name. The elements within a record may not be contiguous in memory.
Linked Lists: Non-contiguous memory allocation. Linked lists use pointers to connect individual
nodes, allowing them to be scattered throughout memory.
Size Flexibility:
Arrays: Fixed size in most programming languages. Once created, the size of an array cannot be
changed.
Records: The size of a record is determined by the combined sizes of its individual elements and is
usually fixed.
Linked Lists: Dynamic size. Linked lists can grow or shrink during runtime, as nodes are dynamically
allocated or deallocated.
Element Access:
Linked Lists: Elements are accessed by traversing the list using pointers.
Arrays: Insertion and deletion operations can be inefficient, especially if elements need to be shifted.
Records: Records are typically not used for insertion and deletion operations directly, as they are
usually used to represent fixed structures.
Linked Lists: Efficient for insertion and deletion operations, as they involve updating pointers to add
or remove nodes.
Traversal:
Linked Lists: Traversal requires following pointers from one node to another until the desired node is
reached.