Data structures combine data elements with organizational patterns to create efficient storage systems. Data encompasses various information types incluidng numeric values, user profiles, and multimedia content. Structure refers to the methodology for organizing this data to enable efficient access and manipulation.
Arrays provide basic data organization, but more complex structures are needed for advanced operations. While arrays can store multiple elements, they lack sophisticated management capabilities for large datasets.
Linear Data Structures
Linear structures arrange elements sequentially while maintaining logical relationships. Common implementations include arrays, linked lists, stacks, queues, and strings. These structures may use contiguosu memory (arrays) or linked nodes (pointers) for physical storage.
Sequential List Implementation
Sequential lists build up on arrays by adding management functionality. Two primary variants exist:
- Static Sequential Lists: Fixed-size arrays that may waste space or become insufficient
- Dynamic Sequential Lists: Resizable arrays that allocate memory as needed
Dynamic Sequential List Structure
#define INITIAL_CAP 4
typedef int DataType;
typedef struct DynamicList {
DataType* elements;
int count;
int capacity;
} DList;
// Core operations
void initList(DList* list);
void destroyList(DList* list);
void resizeCheck(DList* list);
void appendElement(DList* list, DataType value);
void prependElement(DList* list, DataType value);
void removeLast(DList* list);
void removeFirst(DList* list);
void insertAt(DList* list, int position, DataType value);
void removeAt(DList* list, int position);
int findElement(DList* list, DataType value);
Contact Management Application
Implementing a contact system demonstrates sequential list practicality. The system stores personal information including names, genders, ages, phone numbers, and addresses.
Data Structures
// contact.h
#define MAX_NAME 100
#define MAX_GENDER 4
#define MAX_PHONE 11
#define MAX_ADDRESS 100
typedef struct ContactEntry {
char name[MAX_NAME];
char gender[MAX_GENDER];
int age;
char phone[MAX_PHONE];
char address[MAX_ADDRESS];
} Contact;
typedef struct DynamicList ContactList;
// Contact operations
void initContacts(ContactList* contacts);
void addContact(ContactList* contacts);
void deleteContact(ContactList* contacts);
void displayContacts(ContactList* contacts);
void findContact(ContactList* contacts);
void modifyContact(ContactList* contacts);
void cleanupContacts(ContactList* contacts);
Implementation Details
// contact.c
#include "contact.h"
#include "dlist.h"
void loadContacts(ContactList* contacts) {
FILE* file = fopen("contacts.dat", "rb");
if (!file) {
perror("File opening failed");
return;
}
Contact entry;
while (fread(&entry, sizeof(Contact), 1, file)) {
appendElement(contacts, entry);
}
fclose(file);
}
int locateContact(ContactList* contacts, char searchName[]) {
for (int i = 0; i < contacts->count; i++) {
if (strcmp(contacts->elements[i].name, searchName) == 0) {
return i;
}
}
return -1;
}
void saveContacts(ContactList* contacts) {
FILE* file = fopen("contacts.dat", "wb");
if (!file) {
perror("File creation failed");
return;
}
for (int i = 0; i < contacts->count; i++) {
fwrite(&contacts->elements[i], sizeof(Contact), 1, file);
}
fclose(file);
}
User Interface
// main.c
#include "contact.h"
void showMenu() {
ContactList contacts;
initContacts(&contacts);
int choice;
do {
printf("\nContact Management System\n");
printf("1. Add Contact\n");
printf("2. Remove Contact\n");
printf("3. Find Contact\n");
printf("4. Update Contact\n");
printf("5. Display All\n");
printf("0. Exit\n");
printf("Selection: ");
scanf("%d", &choice);
switch (choice) {
case 1: addContact(&contacts); break;
case 2: deleteContact(&contacts); break;
case 3: findContact(&contacts); break;
case 4: modifyContact(&contacts); break;
case 5: displayContacts(&contacts); break;
}
} while (choice != 0);
cleanupContacts(&contacts);
}
Algorithm Considerations
Common sequential list algorithms include element removal and sorted array merging. Performance considerations:
- Insertion/deletion operations have O(n) time complexity
- Memory reallocation involves copying overhead
- Capacity doubling may lead to space inefficiency
These limitations motivate alternative structures like linked lists for specific use cases.