Data Structure Using C: Theory and Program

Author: Ahmad Talhan Siddiqui, Shoeb Ahad Siddiqui
File Type: pdf
Size: 6.0 MB
Language: English
Pages: 428

Data Structure Using C: Theory and Program: Complete Guide with Examples, Applications, and Tips

Introduction to Data Structure Using C

Data structures are the backbone of efficient programming. They define how data is stored, organized, and manipulated. If you are learning the C programming language, mastering data structures is not optional—it’s essential. From simple arrays to complex graphs, C provides low-level access to memory and powerful tools to implement data structures effectively.

In this guide, you’ll find everything you need to know about data structures using C: definitions, examples, practical applications, step-by-step code snippets, case studies, FAQs, and expert tips. By the end, you’ll not only understand data structures but also know how to use them in real-world scenarios.


Why Learn Data Structures in C?

C as the Foundation

C is often called the “mother of programming languages.” It’s widely used to build operating systems, compilers, databases, and performance-critical applications. One reason C has endured is its tight control over memory management and pointers—two features crucial for implementing efficient data structures.

Benefits of Learning in C

When you learn data structures in C:

  • ✔You gain deep knowledge of how memory works.

  • ✔You understand low-level details behind high-level languages like Java, Python, or C++.

  • You build a strong foundation for advanced topics like algorithms, system programming, and artificial intelligence.

In short, if you can implement a data structure in C, you can implement it in any language.


Major Types of Data Structures in C

We can categorize data structures into two main groups:

  • Linear Data Structures – Data elements are arranged sequentially.

  • Non-Linear Data Structures – Data elements form hierarchies or networks.


Linear Data Structures

1. Arrays

  • Definition: A collection of elements stored at contiguous memory locations.

  • Advantages: Fast access, easy indexing.

  • Disadvantages: Fixed size, costly insertions/deletions.

Example: Array Implementation in C
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
for(int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
return 0;
}

Applications: Used in databases, image processing, and sorting algorithms.


2. Linked Lists

  • Definition: A sequence of nodes, where each node contains data and a pointer to the next node.

  • Advantages: Dynamic memory allocation, efficient insertion/deletion.

  • Disadvantages: Slower access, extra memory for pointers.

Singly Linked List Example
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};void printList(struct Node* n) {
while (n != NULL) {
printf(“%d -> “, n->data);
n = n->next;
}
printf(“NULL\n”);
}int main() {
✔struct Node* head = NULL;
✔struct Node* second = NULL;
struct Node* third = NULL;

head = (struct Node*)malloc(sizeof(struct Node));
second = (struct Node*)malloc(sizeof(struct Node));
third = (struct Node*)malloc(sizeof(struct Node));

head->data = 10;
head->next = second;

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

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

printList(head);

return 0;
}

Applications: Used in music playlists, undo functionality, and dynamic memory management.


3. Stacks

  • Definition: A collection of elements that follows Last-In-First-Out (LIFO).

  • Operations: Push (insert), Pop (delete), Peek (top element).

Example: Stack Using Arrays
#include <stdio.h>
#define SIZE 5
int stack[SIZE], top = -1;
void push(int value) {
if(top == SIZE – 1) {
printf(“Stack Overflow\n”);
} else {
stack[++top] = value;
printf(“%d pushed\n”, value);
}
}void pop() {
if(top == -1) {
printf(“Stack Underflow\n”);
} else {
printf(“%d popped\n”, stack[top–]);
}
}int main() {
push(10);
push(20);
pop();
return 0;
}

Applications: Used in recursion, expression evaluation, browser history, and undo operations.


4. Queues

  • Definition: A collection of elements that follows First-In-First-Out (FIFO).

  • Types: Simple Queue, Circular Queue, Priority Queue, Double-Ended Queue (Deque).

Example: Circular Queue
#include <stdio.h>
#define SIZE 5
int items[SIZE];
int front = -1, rear = -1;void enqueue(int value) {
if ((front == 0 && rear == SIZE – 1) || (rear == (front – 1) % (SIZE – 1))) {
printf(“Queue is Full\n”);
} else {
if (front == -1)
front = rear = 0;
else if (rear == SIZE – 1 && front != 0)
rear = 0;
else
rear++;
items[rear] = value;
printf(“%d enqueued\n”, value);
}
}void dequeue() {
if (front == -1) {
printf(“Queue is Empty\n”);
} else {
printf(“%d dequeued\n”, items[front]);
if (front == rear)
front = rear = -1;
else if (front == SIZE – 1)
front = 0;
else
front++;
}
}

int main() {
enqueue(10);
enqueue(20);
dequeue();
enqueue(30);
return 0;
}

Applications: Used in CPU scheduling, printers, messaging systems, and traffic management.


Non-Linear Data Structures

1. Trees

  • Definition: Hierarchical data structure with a root and child nodes.

  • Types: Binary Tree, Binary Search Tree (BST), AVL Tree, B-Tree, Heap.

Example: Binary Search Tree
#include <stdio.h>
#include <stdlib.h>
*struct Node {
int data;
struct Node* left;
struct Node* right;
};struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->left = newNode->right = NULL;
return newNode;
}struct Node* insert(struct Node* root, int value) {
if (root == NULL) return createNode(value);
if (value < root->data)
root->left = insert(root->left, value);
else
root->right = insert(root->right, value);
return root;
}

void inorder(struct Node* root) {
if (root != NULL) {
inorder(root->left);
printf(“%d “, root->data);
inorder(root->right);
}
}

int main() {
struct Node* root = NULL;
root = insert(root, 50);
✔insert(root, 30);
✔insert(root, 70);
insert(root, 20);
insert(root, 40);

inorder(root);
return 0;
}

Applications: Used in file systems, search engines, and decision-making algorithms.


2. Graphs

  • Definition: Collection of nodes (vertices) and edges.

  • Types: Directed, Undirected, Weighted, Unweighted.

Example: Graph Using Adjacency Matrix
#include <stdio.h>
#define V 4
void printGraph(int graph[V][V]) {
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
printf(“%d “, graph[i][j]);
}
printf(“\n”);
}
}int main() {
int graph[V][V] = {
{0, 1, 0, 0},
{1, 0, 1, 1},
{0, 1, 0, 1},
{0, 1, 1, 0}
};
printGraph(graph);
return 0;
}

Applications: Used in social networks, maps, communication systems, and AI pathfinding.


Case Studies

Case Study 1: Student Record Management System

We’ll combine arrays, linked lists, and queues to manage student enrollment, course assignment, and scheduling.

Case Study 2: Hospital Management System

Using queues for patient scheduling and stacks for medical history retrieval.

Case Study 3: Airline Reservation System

Graphs for route maps, heaps for efficient seat allocation.


Tips for Learning Data Structures in C

  • Start with arrays before moving to complex structures.

  • Master pointers—they’re the key to linked lists, trees, and graphs.

  • Draw diagrams to visualize memory allocation.

  • Implement sorting and searching algorithms on each structure.

  • Debug using printf statements to track pointers and memory.


FAQs On Data Structure Using C: Theory and Program

Q1: Which is the best book for data structures in C?
A: “Data Structures Using C” by Reema Thareja and “Data Structures Through C” by Yashavant Kanetkar.

Q2: Is C better than Python for learning data structures?
A: Yes. C gives you deeper control over memory and pointers.

Q3: Do I need to master pointers before learning data structures?
A: Yes—pointers are essential for linked lists, trees, and graphs.

Q4: How do I practice data structures in C effectively?
A: Start with small programs, then build projects like calculators, management systems, or simple compilers.


Conclusion

Data structures are the heart of computer science, and learning them with C gives you unmatched control and understanding of how computers store and process information. Whether you’re preparing for coding interviews, building systems software, or just mastering programming fundamentals, C and data structures together provide the best foundation.

By practicing examples, exploring applications, and tackling projects, you’ll turn theoretical knowledge into practical skill.

Download
Scroll to Top