What Is Data Structure in C With Example?

//

Angela Bailey

Data structures are an essential component of any programming language, including C. They allow us to organize and store data in a structured manner, making it easier to manipulate and access. In this tutorial, we will explore the concept of data structures in C and provide examples to illustrate their usage.

What is a Data Structure?
A data structure is a way of organizing and storing data in memory so that it can be efficiently accessed and modified. It defines the relationship between the data elements, as well as the operations that can be performed on them. In C, data structures are implemented using structs.

Structs in C
A struct is a user-defined data type that groups together related variables under one name. It allows you to create your own complex data types by combining different types of variables. The variables within a struct are called members.

To define a struct in C, you use the struct keyword followed by the name of the struct. For example:

    
        struct Person {
            char name[50];
            int age;
            float salary;
        };
    

Here, we have defined a struct called “Person” with three members: name (a character array), age (an integer), and salary (a float).

Data Structures Examples in C:

1. Array:

An array is a simple yet powerful data structure that stores a fixed-size sequence of elements of the same type. Elements in an array are accessed using their index.

For example:

    
        #include <stdio.h>

        #define SIZE 5

        // Function to print an array
        void printArray(int arr[], int size) {
            for(int i = 0; i < size; i++) {
                printf("%d ", arr[i]);
            }
            printf("\n");
        }

        // Main function
        int main() {
            // Declare and initialize an array
            int numbers[SIZE] = {2, 4, 6, 8, 10};

            // Print the array
            printArray(numbers, SIZE);

            return 0;
        }
    

In this example, we have defined an array called “numbers” with a size of 5. We then pass this array to the “printArray” function which prints each element of the array.

2. Linked List:

A linked list is a dynamic data structure that consists of a sequence of nodes, where each node contains data and a reference (or link) to the next node in the sequence. It allows for efficient insertion and deletion of elements at any position.

    
        #include <stdio.h>
        #include <stdlib.h>

        // Node structure definition
        struct Node {
            int data;
            struct Node* next;
        };

        
  • Create a linked list:

            
                // Function to create a new node
                struct Node* createNode(int data) {
                    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
                    newNode->data = data;
                    newNode->next = NULL;
                    return newNode;
                }

                // Main function
                int main() {
                    // Create nodes
                    struct Node* head = createNode(1);
                    struct Node* second = createNode(2);
                    struct Node* third = createNode(3);

                    // Link nodes
                    head->next = second;
                    second->next = third;

                    return 0;
                }
            
        

In this example, we have defined a struct called "Node" that represents a node in the linked list. Each node contains an integer data and a pointer to the next node. We then create three nodes and link them together to form a linked list.

  • Traverse a linked list:

            
// Function to traverse and print the linked list
void traverseLinkedList(struct Node* head) {
while(head != NULL) {
printf("%d ", head->data);
head = head->next;
}
}

// Main function
int main() {
// Create and link nodes

// Traverse and print the linked list

In this example, we have added a function called "traverseLinkedList" that takes the head of the linked list as an argument and traverses through each node, printing its data.

Conclusion

Data structures are fundamental in programming as they allow us to efficiently organize and manipulate data. In C, structs are commonly used for implementing data structures like arrays and linked lists. By understanding and utilizing these data structures, you can write more efficient and organized code.

In this tutorial, we covered the basics of data structures in C, including the concept of structs and examples of arrays and linked lists. Experiment with these examples and try implementing other data structures to further enhance your understanding. Keep practicing, and soon you'll be proficient in working with various data structures in C.

Remember to always consider the requirements of your program when choosing a data structure, as each has its own strengths and weaknesses.