What Are the Data Structure in C?

//

Heather Bennett

Data structures in C are essential tools that allow programmers to organize and manipulate data efficiently. They provide a way to store and retrieve data in a structured manner, ensuring optimal performance and ease of use. In this article, we will explore some of the most commonly used data structures in C, including arrays, linked lists, stacks, queues, and trees.

Arrays

Arrays are one of the simplest and most widely used data structures in C. They are collections of elements of the same data type that are stored contiguously in memory. Arrays provide random access to their elements using an index.

Example:


int arr[5]; // declaration of an integer array with size 5
arr[0] = 10; // assigning a value to the first element

Linked Lists

Linked lists consist of nodes that contain both data and a pointer to the next node in the list. Unlike arrays, linked lists can dynamically grow or shrink as elements are added or removed. They provide flexibility but require more memory due to the overhead of storing pointers.

Example:


struct Node {
    int data;
    struct Node* next;
};

struct Node* head = NULL; // initializing an empty linked list

void insert(int value) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = value;
    newNode->next = head;
    head = newNode;
}

Stacks

Stacks follow the Last-In-First-Out (LIFO) principle. Elements can only be inserted or removed from one end called the top. Stacks can be implemented using arrays or linked lists.

Example:


#define MAX_SIZE 100

int stack[MAX_SIZE];
int top = -1;

void push(int value) {
    if (top == MAX_SIZE - 1) {
        printf("Stack Overflow\n");
        return;
    }
    stack[++top] = value;
}

int pop() {
    if (top == -1) {
        printf("Stack Underflow\n");
        return -1;
    }
    return stack[top--];
}

Queues

Queues follow the First-In-First-Out (FIFO) principle. Elements are inserted at one end called the rear and removed from the other end called the front. Like stacks, queues can be implemented using arrays or linked lists.

int queue[MAX_SIZE];
int front = -1, rear = -1;

void enqueue(int value) {
if (rear == MAX_SIZE – 1) {
printf(“Queue Overflow\n”);
return;
}
if (front == -1)
front = 0;
queue[++rear] = value;
}

int dequeue() {
if (front == -1 || front > rear) {
printf(“Queue Underflow\n”);
return -1;
}
return queue[front++];
}

Trees

Trees are hierarchical structures consisting of nodes that have a parent-child relationship. Each node can have multiple children but only one parent. Trees are used in various applications like file systems, database indexing, and network routing algorithms.

Example:


struct Node {
    int data;
    struct Node* left;
    struct Node* right;
};

struct Node* newNode(int value) {
    struct Node* node = (struct Node*)malloc(sizeof(struct Node));
    node->data = value;
    node->left = NULL;
    node->right = NULL;
    return node;
}

int main() {
    struct Node* root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    return 0;
}

Conclusion

In C, data structures play a crucial role in efficient and organized data manipulation. Arrays, linked lists, stacks, queues, and trees are just a few examples of the wide range of data structures available.

Understanding their characteristics and implementation details can greatly enhance your programming skills and enable you to solve complex problems efficiently. So, go ahead and explore the world of data structures in C!

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy