What Is Data Structure Types in C?

//

Heather Bennett

Data structures are an essential concept in computer programming. They allow us to organize and manipulate data efficiently.

In the C programming language, there are several types of data structures that can be implemented to solve different problems. In this tutorial, we will explore some of the most commonly used data structure types in C.

Arrays

Arrays are a fundamental type of data structure in C. They allow us to store multiple elements of the same type in contiguous memory locations. Arrays have a fixed size, which is determined at the time of declaration. Elements in an array can be accessed using their index.

Example:


int numbers[5] = {1, 2, 3, 4, 5};
printf("Third element: %d", numbers[2]); // Output: Third element: 3

Linked Lists

Linked lists are dynamic data structures that consist of nodes. Each node contains both data and a pointer to the next node in the list. Linked lists can grow or shrink during program execution as memory is allocated or deallocated.

Example:


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

// Creating nodes
struct Node* head = NULL;
struct Node* second = NULL;
struct Node* third = NULL;

// Allocate memory for nodes
head = (struct Node*)malloc(sizeof(struct Node));
second = (struct Node*)malloc(sizeof(struct Node));
third = (struct Node*)malloc(sizeof(struct Node));

// Linking nodes
head->data = 1;
head->next = second;

second->data = 2;
second->next = third;

third->data = 3;
third->next = NULL;

Stacks

Stacks are a type of data structure that follows the Last-In-First-Out (LIFO) principle. Elements can only be inserted or removed from the top of the stack. Stacks are commonly used in applications that require backtracking or undoing operations.

Example:


#define MAX_SIZE 100

struct Stack {
    int data[MAX_SIZE];
    int top;
};

// Initializing stack
void init(struct Stack* stack) {
    stack->top = -1;
}

// Push operation
void push(struct Stack* stack, int element) {
    if (stack->top >= MAX_SIZE - 1) {
        printf("Stack Overflow");
        return;
    }
    stack->data[++stack->top] = element;
}

// Pop operation
int pop(struct Stack* stack) {
    if (stack->top < 0) {
        printf("Stack Underflow");
        return -1;
    }
    return stack->data[stack->top--];
}

Queues

Queues are a type of data structure that follows the First-In-First-Out (FIFO) principle. Elements can only be inserted at the rear and removed from the front. Queues are commonly used in scheduling algorithms and simulations.

struct Queue {
int data[MAX_SIZE];
int front, rear;
};

// Initializing queue
void init(struct Queue* queue) {
queue->front = -1;
queue->rear = -1;
}

// Enqueue operation
void enqueue(struct Queue* queue, int element) {
if (queue->rear == MAX_SIZE – 1) {
printf(“Queue Overflow”);
return;
}
if (queue->front == -1) {
queue->front = 0;
}
queue->data[++queue->rear] = element;
}

// Dequeue operation
int dequeue(struct Queue* queue) {
if (queue->front == -1 || queue->front > queue->rear) {
printf(“Queue Underflow”);
return -1;
}
return queue->data[queue->front++];
}

Trees

Trees are hierarchical data structures that consist of nodes connected by edges. Each node can have zero or more child nodes. Trees are commonly used in applications such as file systems, network routing, and hierarchical databases.

Example:


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

// Creating nodes
struct TreeNode* root = NULL;
struct TreeNode* leftChild = NULL;
struct TreeNode* rightChild = NULL;

// Allocate memory for nodes
root = (struct TreeNode*)malloc(sizeof(struct TreeNode));
leftChild = (struct TreeNode*)malloc(sizeof(struct TreeNode));
rightChild = (struct TreeNode*)malloc(sizeof(struct TreeNode));

// Linking nodes
root->data = 1;
root->left = leftChild;
root->right = rightChild;

leftChild->data = 2;
leftChild->left = NULL;
leftChild->right = NULL;

rightChild->data = 3;
rightChild->left = NULL;
rightChild->right = NULL;

Conclusion

In this tutorial, we explored some of the most commonly used data structure types in C. Arrays, linked lists, stacks, queues, and trees each have their own unique characteristics and applications. By understanding these data structures and their implementations, you will be better equipped to solve complex programming problems efficiently.

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

Privacy Policy