0% found this document useful (0 votes)
1 views7 pages

Data Structure

The document provides an overview of data types, data structures, and linked lists in programming. It details primitive and derived data types, classifications of data structures, and operations on one-dimensional arrays and linked lists. Additionally, it covers insertion, deletion, and applications of linked lists.

Uploaded by

krishnasahu09999
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)
1 views7 pages

Data Structure

The document provides an overview of data types, data structures, and linked lists in programming. It details primitive and derived data types, classifications of data structures, and operations on one-dimensional arrays and linked lists. Additionally, it covers insertion, deletion, and applications of linked lists.

Uploaded by

krishnasahu09999
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/ 7

Sure! Let's go into a bit more detail for each topic.

1. Data Type

A data type is a classification that specifies what type of data a variable can
hold. It helps the compiler understand how much memory to allocate and
what operations can be performed on the data.

Types of Data Types

A. Primitive Data Types (Basic data types)

1. Integer (int) – Stores whole numbers (e.g., 5, -10, 1000).


2. Float (float, double) – Stores decimal values (e.g., 3.14, -2.5).
3. Character (char) – Stores a single character (e.g., 'A', 'B').
4. Boolean (bool) – Stores true or false values.

B. Derived Data Types (Built from primitive types)

1. Arrays – Collection of elements of the same data type.


2. Pointers – Store memory addresses of variables.
3. Structures (struct) – User-defined grouping of different data types.

C. User-Defined Data Types

1. Structures (struct) – Groups multiple variables of different types.


2. Classes (class) – Used in object-oriented programming.
3. Enumerations (enum) – Defines a set of named integer constants.

2. Data Structure and Its Classification

A data structure is a way to organize and store data efficiently in memory.

Classification of Data Structures

A. Linear Data Structures (Data elements arranged sequentially)

1. Array – Fixed-size collection of elements stored in contiguous memory.


2. Linked List – Collection of nodes connected by pointers.
3. Stack – Follows LIFO (Last In, First Out) principle.
4. Queue – Follows FIFO (First In, First Out) principle.

B. Non-Linear Data Structures (Data elements are connected in different


ways)

1. Tree – Hierarchical data structure (e.g., Binary Tree, Binary Search Tree).
2. Graph – Collection of nodes (vertices) connected by edges.

C. Other Types

• Static Data Structure – Fixed size, defined at compile-time (e.g., Arrays).


• Dynamic Data Structure – Size can grow/shrink at runtime (e.g., Linked
Lists).

3. Array: Concept & Types

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


memory locations.
Example:
int arr[5] = {10, 20, 30, 40, 50};

Here, arr is an integer array of size 5, storing five elements.

Types of Arrays

1. One-Dimensional Array (1D Array)


• Stores elements in a single row.
• Example:
int arr[4] = {1, 2, 3, 4};

2. Two-Dimensional Array (2D Array)


• Stores elements in rows and columns (like a matrix).
• Example:
int arr[2][2] = { {1, 2}, {3, 4} };
3. Multi-Dimensional Array
• More than two dimensions, e.g., a 3D array.

4. Operations on One-Dimensional Array

A. Traversal (Visiting each element one by one)

Example in C:

#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50}; // Array
declaration
int size = sizeof(arr) / sizeof(arr[0]); //
Finding size
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]); // Print each element
}
return 0;
}

Output:
10 20 30 40 50

B. Insertion (Adding a new element at a specific position)

• We need to shift elements to make space.


• Example: Insert 25 at position 2 in {10, 20, 30, 40, 50}.

C Code for Insertion:

#include <stdio.h>
void insertElement(int arr[], int *size, int
position, int value) {
for (int i = *size; i > position; i--) {
arr[i] = arr[i - 1]; // Shift elements
}
arr[position] = value; // Insert new value
(*size)++; // Increase size
}
int main() {
int arr[10] = {10, 20, 30, 40, 50}; // Initial
array
int size = 5, pos = 2, val = 25; // Position and
value to insert
insertElement(arr, &size, pos, val);
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]); // Print updated array
}
return 0;
}

Output:
10 20 25 30 40 50

C. Deletion (Removing an element from a specific position)

• Shift elements to the left after the deleted position.


• Example: Delete element at position 2 from {10, 20, 30, 40,
50}.

C Code for Deletion:

#include <stdio.h>
void deleteElement(int arr[], int *size, int
position) {
for (int i = position; i < *size - 1; i++) {
arr[i] = arr[i + 1]; // Shift elements left
}
(*size)--; // Decrease size
}
int main() {
int arr[10] = {10, 20, 30, 40, 50};
int size = 5, pos = 2; // Position to delete
deleteElement(arr, &size, pos);
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]); // Print updated array
}
return 0;
}

Output:
10 20 40 50

Summary

Operati
Explanation
on

Travers
Visit and display all elements.
al

Insertio Add a new element at a


n specific index.

Remove an element and shift


Deletion
the rest.
Would you like more examples or explanations? 😊

Great! Let's break it down step by step.

1. Concept of Linked List

A Linked List is a linear data structure where elements (called nodes) are linked
using pointers. Unlike arrays, linked lists don’t have a fixed size and can grow
or shrink dynamically.
Each node contains:
• Data (value stored in the node)
• Pointer (next) (address of the next node)
Example of a simple Singly Linked List:
[10 | * ] → [20 | * ] → [30 | NULL]

Here, each node points to the next node, and the last node points to NULL.

2. Circular & Doubly Linked List

• Circular Linked List (CLL):


• The last node points to the first node instead of NULL, forming a
circle.
• Can be singly (only next pointer) or doubly (next & prev pointers).
• Example: Used in round-robin scheduling.
• Doubly Linked List (DLL):
• Each node has two pointers:
• next → points to the next node
• prev → points to the previous node
• Example:
NULL ← [10 | * | * ] ↔ [20 | * | * ] ↔ [30 |
* | NULL]

• Used in undo/redo functionality in applications.

3. Operations on Linked List

• Traversal: Visiting each node from start to end.


• Insertion: Adding a new node at the beginning, middle, or end.
• Deletion: Removing a node from the beginning, middle, or end.
• Search: Finding a node with a given value.

4. List Manipulation with Pointers


• Using Pointers, we can dynamically allocate and deallocate memory for
nodes.
• Example in C:
struct Node {
int data;
struct Node* next;
};
struct Node* head = NULL; // Head pointer stores
the first node

5. Insertion & Deletion of Elements

Insertion

• At the beginning: Update head to the new node.


• At the end: Traverse till the last node and update its next.
• In the middle: Update next pointers of the previous and new node.

Deletion

• From the beginning: Update head to the next node.


• From the end: Traverse till second last node and set next = NULL.
• From the middle: Update next pointer of the previous node.

6. Applications of Linked List

• Dynamic Memory Allocation (heap memory management)


• Undo/Redo Operations (text editors)
• Music/Video Playlists (circular linked lists)
• Graph Implementations (adjacency list representation)
• Hashing (Separate Chaining) in hash tables

Would you like code examples for insertion, deletion, or traversal? 😊

You might also like