C Tutorial

# Array of Structures in C

In C programming, data structures implemented with arrays are fundamental for organizing and managing collections of data efficiently.

## Lists and Arrays

A simple list or array is the most basic data structure, where elements are stored in contiguous memory locations, allowing for efficient random access.

int numbers[] = {1, 2, 3, 4, 5};

## Stacks

Stacks are linear data structures that follow the Last-In-First-Out (LIFO) principle. They can be implemented using arrays, with elements pushed onto the end of the array and popped from the same end.

#define MAX_SIZE 100 int stack[MAX_SIZE]; int top = -1; // Push operation void push(int item) { if (top >= MAX_SIZE - 1) { printf("Stack Overflow\n"); return; } stack[++top] = item; } // Pop operation int pop() { if (top < 0) { printf("Stack Underflow\n"); return -1; // Indicates an error } return stack[top--]; }

## Queues

Queues are linear data structures that follow the First-In-First-Out (FIFO) principle. Arrays can be used to implement queues, with elements enqueued at one end and dequeued from the other end.

#define MAX_SIZE 100 int queue[MAX_SIZE]; int front = -1, rear = -1; // Enqueue operation void enqueue(int item) { if (rear == MAX_SIZE - 1) { printf("Queue is full\n"); return; } if (front == -1) { front = 0; } queue[++rear] = item; } // Dequeue operation int dequeue() { if (front == -1) { printf("Queue is empty\n"); return -1; // Indicates an error } int item = queue[front]; if (front == rear) { front = rear = -1; } else { front++; } return item; }

## Arrays of Structures

Arrays can store structures as elements, allowing you to create collections of complex data types.

struct Student { char name[50]; int rollNumber; }; struct Student studentArray[100];

## Matrices

Arrays can represent matrices, which are 2D data structures used in mathematics and computer science for various purposes, including graphics, image processing, and simulations.

int matrix[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };

Arrays are a versatile data structure that can be used to implement a variety of data structures in C. Here are some other examples:

1. Linked list: A linked list is a data structure that stores a collection of elements in a linear sequence. Each element in the linked list contains a pointer to the next element in the sequence. Arrays can be used to implement a linked list by storing the elements of the linked list in an array and using the array indices to simulate the pointers.
2. Binary tree: A binary tree is a tree data structure in which each node has at most two children. Arrays can be used to implement a binary tree by storing the elements of the tree in an array and using the array indices to represent the parent-child relationships.
3. Hash table: A hash table is a data structure that maps keys to values. Arrays can be used to implement a hash table by storing the key-value pairs in an array and using a hash function to determine the index of each key-value pair in the array.

### Conclusion

Data structures implemented with C arrays are the building blocks of many algorithms and applications. Understanding their usage and the specific array-based implementation details for each data structure is essential for efficient and effective programming in C.