Data Structure

 

Data Structures through C Language 

1. Introduction to Data Structures

A data structure is a systematic way of organizing, storing, and managing data so that it can be accessed and modified efficiently. In computer science, data structures play a very important role because they help in solving complex problems in an organized and optimized way.

When writing programs in the C programming language, data structures allow programmers to handle large amounts of data efficiently. Instead of storing data randomly, data structures provide logical methods to store and manipulate information.

For example, if we want to store marks of 100 students, storing them in separate variables is inefficient. Instead, we use structures such as arrays, linked lists, stacks, and queues to manage the data effectively.

 


Data structures are widely used in:

  • Operating systems
  • Database management systems
  • Artificial intelligence
  • Networking
  • Compiler design
  • Software development

Thus, understanding data structures is essential for writing efficient programs.


2. Basic Concepts of Data Structures

Before studying different types of data structures, it is important to understand some fundamental concepts.

2.1 Data

Data refers to raw facts and figures that can be processed to produce meaningful information.

Example:

  • Numbers
  • Characters
  • Strings
  • Images
  • Audio

2.2 Information

When data is processed and organized meaningfully, it becomes information.

Example: Marks stored in a list → can produce result reports.

2.3 Data Structure Definition

A data structure is a specialized format for organizing and storing data in a computer so that it can be accessed and modified efficiently.


3. Classification of Data Structures

Data structures are mainly classified into two categories:

3.1 Primitive Data Structures

Primitive data structures are the basic data types provided by programming languages.

Examples in C:

  • int
  • float
  • char
  • double
  • pointer

Example in C:

int a = 10;
float b = 5.5;
char c = 'A';

These data types store single values.


3.2 Non-Primitive Data Structures

Non-primitive data structures are derived from primitive data types and can store multiple values.

They are further divided into two types:

1. Linear Data Structures

In linear structures, elements are arranged sequentially.

Examples:

  • Array
  • Linked List
  • Stack
  • Queue

2. Non-Linear Data Structures

In non-linear structures, elements are arranged hierarchically.

Examples:

  • Trees
  • Graphs

4. Arrays in C

Definition

An array is a collection of elements of the same data type stored in contiguous memory locations.

It is one of the simplest and most commonly used data structures.

Syntax

data_type array_name[size];

Example:

int marks[5];

Initialization

int marks[5] = {70, 80, 90, 85, 60};

Accessing Elements

printf("%d", marks[0]);

Advantages of Arrays

  • Easy access using index
  • Efficient for storing multiple values
  • Simple implementation

Disadvantages

  • Fixed size
  • Insertion and deletion are difficult
  • Wastes memory if size is larger than required

5. Structures in C

A structure is a user-defined data type that allows grouping different types of data under one name.

Example: storing information of a student.

Syntax

struct student
{
int roll;
char name[20];
float marks;
};

Example Program

#include <stdio.h>
struct student
{
int roll;
float marks;
};
int main()
{
struct student s1;
s1.roll = 101;
s1.marks = 85.5;
printf("Roll: %d\n", s1.roll);
printf("Marks: %.2f", s1.marks);
return 0;
}

Advantages

  • Combines different data types
  • Useful for records and databases
  • Makes programs organized

6. Linked List

A linked list is a dynamic data structure where elements are connected using pointers.

Each element is called a node.

Each node contains:

  1. Data
  2. Pointer to next node

Structure of Node

struct node
{
int data;
struct node *next;
};

Types of Linked Lists

  1. Singly Linked List
  2. Doubly Linked List
  3. Circular Linked List

Advantages

  • Dynamic size
  • Efficient insertion and deletion

Disadvantages

  • Extra memory for pointers
  • Sequential access only

7. Stack

A stack is a linear data structure that follows the LIFO principle (Last In First Out).

Example: Stack of plates.

Operations in stack:

  • Push
  • Pop
  • Peek

Stack Operations

Push

Insert an element into stack.

Pop

Remove an element from stack.

Peek

View top element.

Example Stack Program

#include <stdio.h>
#define MAX 5
int stack[MAX];
int top = -1;
void push(int x)
{
if(top == MAX-1)
printf("Stack Overflow");
else
stack[++top] = x;
}
void pop()
{
if(top == -1)
printf("Stack Underflow");
else
top--;
}

Applications

  • Function calls
  • Expression evaluation
  • Undo operations

8. Queue

A queue is a linear data structure that follows the FIFO principle (First In First Out).

Example: People standing in a queue.

Queue Operations

  • Enqueue
  • Dequeue
  • Peek

Enqueue

Insert element at rear.

Dequeue

Remove element from front.

Queue Example

#include <stdio.h>
#define MAX 5
int queue[MAX];
int front = -1, rear = -1;

Applications

  • CPU scheduling
  • Printer queue
  • Network buffering

9. Trees

A tree is a hierarchical data structure consisting of nodes connected by edges.

Example: Family tree.

Basic Terminology

  • Root
  • Parent
  • Child
  • Leaf
  • Edge

Binary Tree

In a binary tree, each node has at most two children.

Example representation:

A
/ \
B C
/ \
D E

Applications

  • File systems
  • Database indexing
  • Expression parsing

10. Graphs

A graph is a collection of nodes connected by edges.

Example: Social networks.

Types of Graphs

  1. Directed graph
  2. Undirected graph
  3. Weighted graph

Applications

  • Google Maps
  • Network routing
  • Social media connections

11. Dynamic Memory Allocation in C

Dynamic memory allocation allows memory to be allocated at runtime.

Functions used:

  • malloc()
  • calloc()
  • realloc()
  • free()

Example:

int *ptr;
ptr = (int*)malloc(5 * sizeof(int));

Advantages

  • Efficient memory usage
  • Flexible data structures

12. Importance of Data Structures

Data structures are extremely important in programming because they:

  1. Improve program efficiency
  2. Organize large data sets
  3. Optimize memory usage
  4. Reduce execution time
  5. Make algorithms easier to implement

Most modern software systems depend heavily on efficient data structures.


13. Applications of Data Structures

Data structures are used in many real-world systems such as:

1. Operating Systems

Process scheduling uses queues.

2. Databases

Indexing uses trees and hashing.

3. Artificial Intelligence

Graphs are used for problem solving.

4. Computer Networks

Routing algorithms use graphs.

5. Compiler Design

Stacks are used for parsing expressions.


14. Advantages of Using Data Structures

  • Efficient data management
  • Faster data access
  • Improved program performance
  • Better memory utilization
  • Easier algorithm implementation

15. Conclusion

Data structures are a fundamental concept in computer science and programming. When working with the C programming language, understanding data structures helps programmers store, manage, and process data efficiently.

Basic structures such as arrays, structures, linked lists, stacks, queues, trees, and graphs provide different ways to organize data depending on the problem requirements.

Efficient use of data structures leads to optimized programs, reduced memory consumption, and faster execution times. Therefore, mastering data structures is essential for programmers, software developers, and computer science sstuden

Comments

Popular Posts