0% found this document useful (0 votes)
10 views

Data Structures in C

Data structures
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Data Structures in C

Data structures
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Data Structures in C

Data structures are the building blocks for programming logic, used to organize, manage, and
store data efficiently. In C programming, data structures are categorized into Primitive and
Non-Primitive types.

1. Primitive Data Structures

Primitive data structures are basic data types directly supported by the system and act as the
foundation for more complex structures.

1.1 Integer

• Definition: Integer (int) represents whole numbers, both positive and negative, with
no fractional part.
• Size: Usually occupies 4 bytes in memory, allowing it to store values in the range -
2^31 to 2^31-1.
• Example:

c
Copy code
int age = 25;
printf("Age: %d", age); // Output: Age: 25

1.2 Float

• Definition: Float represents numbers with a fractional part, allowing storage of real
numbers with decimal points.
• Size: Typically occupies 4 bytes, offering up to 7 digits of precision.
• Example:

c
Copy code
float price = 10.99;
printf("Price: %.2f", price); // Output: Price: 10.99

1.3 Character

• Definition: Character (char) stores a single character, such as a letter, digit, or


symbol.
• Size: Usually occupies 1 byte and is capable of storing a single ASCII character.
• Example:

c
Copy code
char initial = 'A';
printf("Initial: %c", initial); // Output: Initial: A

1.4 Pointer
• Definition: A pointer is a variable that stores the memory address of another variable.
• Example:

c
Copy code
int num = 42;
int *ptr = #
printf("Address of num: %p", ptr); // Output: Address of num:
0x7ffee10c091c (example)
printf("Value at address: %d", *ptr); // Output: Value at address:
42

2. Non-Primitive Data Structures

Non-primitive data structures are derived from primitive types and provide ways to store
multiple values. They are categorized as Static and Dynamic data structures.

2.1 Static Data Structures

These data structures have a fixed size, defined at compile-time. Common examples include
arrays and structures.

2.1.1 Arrays

• Definition: An array is a collection of elements of the same data type stored in


contiguous memory locations.
• Example:

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

Multi-dimensional Arrays

• Definition: Arrays can have more than one dimension, such as a 2D array,
representing a matrix or grid.
• Example:

c
Copy code
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
printf("Element at [1][2]: %d", matrix[1][2]); // Output: Element at
[1][2]: 6

2.1.2 Structures

• Definition: A structure (struct) allows grouping of variables of different data types


under one name.
• Example:
c
Copy code
struct Student {
char name[50];
int age;
float grade;
};

struct Student student1 = {"Alice", 20, 85.5};


printf("Name: %s, Age: %d, Grade: %.2f", student1.name, student1.age,
student1.grade);
// Output: Name: Alice, Age: 20, Grade: 85.50

2.2 Dynamic Data Structures

Dynamic data structures can grow or shrink at runtime, depending on the needs of the
program. They are often managed using pointers and dynamic memory allocation functions
like malloc() and free().

2.2.1 Linear Data Structures

Linear data structures have a sequential organization, where each element has a unique
predecessor and successor (except the first and last).
2.2.1.1 Linked Lists

• Definition: A linked list is a collection of nodes where each node contains data and a
pointer to the next node.
• Types:
o Singly Linked List: Each node points to the next node, with the last node
pointing to NULL.
• Example:

c
Copy code
struct Node {
int data;
struct Node* next;
};

struct Node* head = NULL;

// Dynamically allocate memory for new node


head = (struct Node*)malloc(sizeof(struct Node));
head->data = 10;
head->next = NULL;

2.2.1.2 Stacks

• Definition: A stack is a linear structure that follows the Last In First Out (LIFO)
principle, meaning the last element added is the first to be removed.
• Example (Array-based Stack):

c
Copy code
int stack[5];
int top = -1;

void push(int value) {


if(top == 4) {
printf("Stack Overflow\n");
} else {
stack[++top] = value;
}
}

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

push(10); // Add value 10 to the stack


printf("Popped: %d", pop()); // Output: Popped: 10

2.2.1.3 Queues

• Definition: A queue is a linear structure that follows the First In First Out (FIFO)
principle, where the first element added is the first to be removed.
• Example (Simple Queue):

c
Copy code
int queue[5];
int front = 0, rear = -1;

void enqueue(int value) {


if(rear == 4) {
printf("Queue Overflow\n");
} else {
queue[++rear] = value;
}
}

int dequeue() {
if(front > rear) {
printf("Queue Underflow\n");
return -1;
} else {
return queue[front++];
}
}

enqueue(15);
printf("Dequeued: %d", dequeue()); // Output: Dequeued: 15

2.2.2 Non-Linear Data Structures

Non-linear data structures allow hierarchical relationships among elements, making them
suitable for representing real-world scenarios like trees and networks.
2.2.2.1 Trees

• Definition: A tree is a hierarchical structure where each node contains data and
references to child nodes. The top node is called the root, and nodes without children
are called leaves.
• Example (Binary Tree Node):

c
Copy code
struct TreeNode {
int data;
struct TreeNode* left;
struct TreeNode* right;
};

struct TreeNode* createNode(int data) {


struct TreeNode* node = (struct TreeNode*)malloc(sizeof(struct
TreeNode));
node->data = data;
node->left = node->right = NULL;
return node;
}

struct TreeNode* root = createNode(10); // Root node


root->left = createNode(5); // Left child
root->right = createNode(15); // Right child

2.2.2.2 Graphs

• Definition: A graph is a set of vertices (nodes) connected by edges. It is used to


represent relationships between elements.
• Example (Adjacency Matrix for an Undirected Graph):

c
Copy code
int graph[3][3] = {
{0, 1, 0},
{1, 0, 1},
{0, 1, 0}
};

2.2.2.3 Heaps

• Definition: A heap is a specialized tree-based data structure used for efficient


priority-based operations. The two types of heaps are:
o Max Heap: The root node has the maximum value, and each parent node is
greater than or equal to its children.
o Min Heap: The root node has the minimum value, and each parent node is
smaller than or equal to its children.
• Example (Min Heap using Array):

c
Copy code
int heap[10], size = 0;

void insert(int value) {


int i = size++;
heap[i] = value;

while(i > 0 && heap[(i - 1) / 2] > heap[i]) {


int temp = heap[i];
heap[i] = heap[(i - 1) / 2];
heap[(i - 1) / 2] = temp;
i = (i - 1) / 2;
}
}

insert(10);
insert(5);
insert(20);
// Heap now stores values with the smallest at root

Conclusion

In C, data structures serve as the foundation for organizing and managing data efficiently.
Mastering both static and dynamic structures, such as arrays, linked lists, and trees, enables
optimal performance in terms of time and space complexity. By understanding how each
structure works and when to use them, you can write more efficient and effective C
programs.

You might also like