Data Structures Tutorial with C Examples254


## Introduction
Data structures play a crucial role in organizing and manipulating data efficiently in computer programs. They provide a systematic way to store, retrieve, and manage data in a structured manner. In this tutorial, we will explore some of the essential data structures along with their implementation in C programming language.
## Array
An array is a linear data structure that stores a collection of elements of the same data type. Each element is identified by its index, which starts from 0. Arrays are contiguous in memory, meaning they occupy consecutive memory locations.
```c
int main() {
int arr[5] = {1, 2, 3, 4, 5};
printf("%d", arr[2]); // Output: 3
return 0;
}
```
## Linked List
A linked list is a data structure that stores data in nodes. Each node consists of two parts: the data itself and a pointer to the next node. Linked lists are not contiguous in memory, allowing flexibility in adding and removing elements.
```c
typedef struct Node {
int data;
struct Node *next;
} Node;
Node *head = NULL;
// Insert a node at the beginning
void insertAtBeginning(int data) {
Node *new_node = malloc(sizeof(Node));
new_node->data = data;
new_node->next = head;
head = new_node;
}
// Print the linked list
void printLinkedList() {
Node *current_node = head;
while (current_node != NULL) {
printf("%d ", current_node->data);
current_node = current_node->next;
}
}
```
## Stack
A stack is a linear data structure that follows the Last-In, First-Out (LIFO) principle. Elements are added and removed from the top of the stack.
```c
#define MAX_SIZE 100
int stack[MAX_SIZE];
int top = -1;
// Push an element into the stack
void push(int data) {
if (top == MAX_SIZE - 1) {
printf("Stack overflow!");
return;
}
stack[++top] = data;
}
// Pop an element from the stack
int pop() {
if (top == -1) {
printf("Stack underflow!");
return -1;
}
return stack[top--];
}
```
## Queue
A queue is a linear data structure that follows the First-In, First-Out (FIFO) principle. Elements are added at the end of the queue and removed from the beginning.
```c
#define MAX_SIZE 100
int queue[MAX_SIZE];
int front = -1, rear = -1;
// Enqueue an element into the queue
void enqueue(int data) {
if ((rear + 1) % MAX_SIZE == front) {
printf("Queue overflow!");
return;
}
else if (front == -1) { // Queue is empty
front = rear = 0;
}
else {
rear = (rear + 1) % MAX_SIZE;
}
queue[rear] = data;
}
// Dequeue an element from the queue
int dequeue() {
if (front == -1) {
printf("Queue underflow!");
return -1;
}
int data = queue[front];
if (front == rear) { // Queue has only one element
front = rear = -1;
}
else {
front = (front + 1) % MAX_SIZE;
}
return data;
}
```
## Tree
A tree is a hierarchical data structure consisting of nodes connected by edges. Each node can have multiple children, but only one parent.
```c
typedef struct Node {
int data;
struct Node *left;
struct Node *right;
} Node;
Node *root = NULL;
// Insert a node into the tree
void insert(int data) {
Node *new_node = malloc(sizeof(Node));
new_node->data = data;
new_node->left = new_node->right = NULL;
if (root == NULL) {
root = new_node;
}
else {
Node *current_node = root;
while (1) {
if (data < current_node->data) {
if (current_node->left == NULL) {
current_node->left = new_node;
break;
}
else {
current_node = current_node->left;
}
}
else {
if (current_node->right == NULL) {
current_node->right = new_node;
break;
}
else {
current_node = current_node->right;
}
}
}
}
}
// Print the tree in preorder traversal
void preorder(Node *node) {
if (node != NULL) {
printf("%d ", node->data);
preorder(node->left);
preorder(node->right);
}
}
```
## Conclusion
Data structures are fundamental concepts in computer science. Understanding how to implement and use these structures is crucial for developing efficient and reliable software applications. This tutorial provides a solid foundation for working with arrays, linked lists, stacks, queues, and trees in C programming language.

2024-11-23


Previous:Unlimited Data Mobile Plan Guide

Next:Who Coined the Term “Cloud Computing“?