For working professionals
For fresh graduates
More
5. Array in C
13. Boolean in C
18. Operators in C
33. Comments in C
38. Constants in C
41. Data Types in C
49. Double In C
58. For Loop in C
60. Functions in C
70. Identifiers in C
81. Linked list in C
83. Macros in C
86. Nested Loop in C
97. Pseudo-Code In C
100. Recursion in C
103. Square Root in C
104. Stack in C
106. Static function in C
107. Stdio.h in C
108. Storage Classes in C
109. strcat() in C
110. Strcmp in C
111. Strcpy in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
125. Type Casting in C
126. Types of Error in C
127. Unary Operator in C
128. Use of C Language
When working on large C programs, you often need to organize and manage data efficiently. Without the right approach, handling complex data becomes difficult and leads to unnecessary errors. Data Structures in C play a key role in solving this problem by giving you ways to store and process data systematically.
In real-world programming, using Data Structures in C helps you write optimized code that runs faster and uses memory wisely. They make tasks like searching, sorting, and managing data easier, even in programs with thousands of variables.
Explore Online Software Development Courses from top universities.
Data Structures in C refer to techniques that let you store, organize, and manage data in specific formats. Each structure defines how data elements relate to each other and how you can perform operations like insertion, deletion, or traversal.
The syntax for defining a data structure like a struct in C is:
struct StructName {
dataType member1;
dataType member2;
// other members
};
You use this syntax to group different types of data under a single name.
You need Data Structures in C to manage large amounts of data efficiently. Without them, storing related data would require multiple variables, making the code messy and hard to debug. Data structures help you keep data organized and easy to access.
If you’re ready to grow your skills, check out these programs:
You declare a structure in C using the struct keyword followed by its members. Here’s an example:
struct Student {
int rollNo;
char name[50];
float marks;
};
In this example, Student is a structure with three members.
You can initialize a structure at the time of declaration or later in the code. Here’s how:
struct Student s1 = {101, "Alice", 89.5};
Output:
Student Details: Roll No: 101, Name: Alice, Marks: 89.50\
Explanation: This initializes s1 with roll number 101, name "Alice", and marks 89.5. You can access members using the dot operator.
C supports many types of data structures. Each serves different purposes. Below are the main types, along with examples, output, and explanations.
Arrays are one of the most basic data structures. Learn more about arrays in C and their implementation. An array stores multiple elements of the same type in continuous memory locations.
#include <stdio.h>
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
return 0;
}
Output:
10 20 30 40 50
Explanation: The code initializes an array numbers with five integers and prints them using a loop.
A linked list connects data nodes using pointers.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* head = malloc(sizeof(struct Node));
head->data = 10;
head->next = malloc(sizeof(struct Node));
head->next->data = 20;
head->next->next = NULL;
struct Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
return 0;
}
Output:
10 20
Explanation: We created a simple linked list with two nodes containing 10 and 20. Each node points to the next one.
Stacks are closely related to other data structures like arrays and linked lists. A stack follows the Last In First Out (LIFO) principle.
#include <stdio.h>
#define SIZE 5
int main() {
int stack[SIZE], top = -1;
stack[++top] = 10;
stack[++top] = 20;
printf("Top Element: %d\n", stack[top]);
printf("Popped: %d\n", stack[top--]);
return 0;
}
Output:
Top Element: 20
Popped: 20
Explanation: We pushed 10 and 20 onto the stack, printed the top element, then popped the top.
Queues are another fundamental data structure. Explore the implementation of queues using linked lists in C. A queue follows the First In First Out (FIFO) principle.
#include <stdio.h>
#define SIZE 5
int main() {
int queue[SIZE], front = 0, rear = -1;
queue[++rear] = 10;
queue[++rear] = 20;
printf("Dequeued: %d\n", queue[front++]);
printf("Front Element: %d\n", queue[front]);
return 0;
}
Output:
Dequeued: 10
Front Element: 20
Explanation: We enqueued 10 and 20, then removed 10 (the front element) and displayed the next front.
A tree organizes data hierarchically.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* left;
struct Node* right;
};
struct Node* newNode(int data) {
struct Node* node = malloc(sizeof(struct Node));
node->data = data;
node->left = node->right = NULL;
return node;
}
void inorder(struct Node* root) {
if (root != NULL) {
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}
int main() {
struct Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
inorder(root);
return 0;
}
Output:
2 1 3
Explanation: We created a binary tree with nodes 1, 2, and 3. The inorder function prints them in left-root-right order.
A graph represents data using nodes (vertices) and edges.
#include <stdio.h>
#define V 3
int main() {
int graph[V][V] = {{0, 1, 0}, {1, 0, 1}, {0, 1, 0}};
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
printf("%d ", graph[i][j]);
}
printf("\n");
}
return 0;
}
Output:
0 1 0
1 0 1
0 1 0
Explanation: We created an adjacency matrix for a graph with 3 vertices. Each 1 shows an edge between two vertices.
Data structures in C are classified as linear (arrays, linked lists, stacks, queues) and non-linear (trees, graphs). Linear structures store data sequentially. Non-linear structures store data in hierarchical or connected ways.
Here’s a simple classification example:
printf("Linear: Array, Linked List, Stack, Queue\n");
printf("Non-Linear: Tree, Graph\n");
Output:
Linear: Array, Linked List, Stack, Queue
Non-Linear: Tree, Graph
Explanation: This output lists common structures under their respective classifications.
Abstract Data Types (ADTs) in C define operations and behavior without showing implementation details. Examples include stacks, queues, and lists when implemented using structures and functions.
Here’s a simple stack ADT using functions:
#include <stdio.h>
#define SIZE 5
int stack[SIZE], top = -1;
void push(int val) {
if (top < SIZE - 1)
stack[++top] = val;
}
int pop() {
if (top >= 0)
return stack[top--];
return -1;
}
int main() {
push(10);
push(20);
printf("Popped: %d\n", pop());
return 0;
}
Output:
Popped: 20
Explanation: We defined push and pop functions to hide internal stack operations, making it an ADT.
Here’s a program combining multiple data structures:
#include <stdio.h>
struct Student {
int rollNo;
char name[50];
float marks;
};
int main() {
struct Student s1 = {101, "Alice", 92.5};
printf("Roll No: %d\n", s1.rollNo);
printf("Name: %s\n", s1.name);
printf("Marks: %.2f\n", s1.marks);
return 0;
}
Output:
Roll No: 101
Name: Alice
Marks: 92.50
Explanation: We used a structure to store a student’s details and printed them using the dot operator.
Data Structures in C provide organized ways to store and process data efficiently. By learning arrays, linked lists, stacks, queues, trees, and graphs, you can solve complex problems with ease. Each structure fits specific use cases, and mastering them improves your coding skills. When you understand how to implement these data structures in C, you gain better control over memory management and performance. They allow you to choose the right tool for each problem, whether it’s handling dynamic data with linked lists, managing function calls with stacks, or representing relationships using graphs. By practicing these core concepts, you build a strong foundation for advanced algorithms, making you a more effective and versatile programmer.
Data Structures in C help organize, store, and manage data efficiently. They allow programmers to access, modify, and process data effectively while improving program speed and memory usage for different problem-solving tasks.
Choosing the right Data Structure in C depends on the problem’s requirements. Consider factors like data type, access frequency, insertion or deletion needs, and memory constraints to select the most suitable structure for your application.
An array stores elements in contiguous memory locations with fixed size, while a linked list uses nodes with dynamic memory allocation. Arrays allow direct access; linked lists allow easier insertion or deletion without shifting elements.
A stack can be implemented using arrays in C by defining an array and a variable to track the top index. You perform push and pop operations by updating the top index and accessing the array elements accordingly.
Key operations in a queue Data Structure in C include enqueue (inserting an element at the rear), dequeue (removing an element from the front), checking if the queue is full or empty, and accessing the front or rear elements.
A circular queue in C connects the rear end back to the front, forming a circle. It prevents wasted space by reusing vacated positions. You use modulo operations to wrap the rear and front pointers within the array’s bounds.
A binary tree in C is a hierarchical structure where each node has up to two children. It’s used in applications like searching, sorting, expression evaluation, and file systems to organize data in a logical, searchable form.
Graphs in C allow nodes (vertices) to have multiple connections (edges) in any direction, including cycles. Trees are a special type of graph with a hierarchical, acyclic structure where each node has only one parent except the root.
Abstract Data Types in C define a logical model of data and operations without specifying implementation details. Examples include stacks, queues, and lists. ADTs focus on what operations are performed, not how they’re implemented.
Dynamic memory allocation in Data Structures in C lets you allocate memory at runtime using functions like malloc() and free(). It allows flexible memory management, especially for linked structures like linked lists, trees, and graphs.
Pointers are crucial in implementing Data Structures in C because they allow dynamic memory handling, link creation between nodes, and efficient traversal. Without pointers, it’s difficult to build flexible structures like linked lists or trees.
To traverse a linked list in C, start from the head node and follow each node’s pointer to the next node. Continue visiting nodes until you reach a node whose next pointer is NULL, indicating the end of the list.
Data Structures in C improve data organization, processing efficiency, and code reusability. They allow better memory utilization and algorithm implementation. By choosing the right structure, you solve problems faster with optimized resources.
Linear Data Structures in C store elements sequentially, like arrays, stacks, and queues. Nonlinear Data Structures, such as trees and graphs, organize elements hierarchically or with complex connections, enabling different data relationship models.
Yes, Data Structures in C are used in real-world applications like databases, operating systems, compilers, network routing, and file management. They form the backbone for efficient algorithms that handle large, structured, or dynamic data.
Take a Free C Programming Quiz
Answer quick questions and assess your C programming knowledge
Author|900 articles published
Previous
Next
Start Learning For Free
Explore Our Free Software Tutorials and Elevate your Career.
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918068792934
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.