0% found this document useful (0 votes)
10 views58 pages

Understanding Data Structures and Types

The document discusses the distinction between data and information, emphasizing the importance of data structures in organizing and accessing data efficiently. It covers various types of data structures, including primitive and non-primitive types, and explains concepts like pointers, structures, and unions in programming. Additionally, it highlights the significance of understanding these concepts for effective problem-solving in software development.

Uploaded by

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

Understanding Data Structures and Types

The document discusses the distinction between data and information, emphasizing the importance of data structures in organizing and accessing data efficiently. It covers various types of data structures, including primitive and non-primitive types, and explains concepts like pointers, structures, and unions in programming. Additionally, it highlights the significance of understanding these concepts for effective problem-solving in software development.

Uploaded by

juhuku3
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

Prof.

Megha Chandel
Department of Computer Science and
Engineering
(Data Science)
Data: Raw Facts Information: Processed &
Data refers to raw, unprocessed facts, figures, or symbols. By
Meaningful
themselves, these individual pieces of data often lack context Information is data that has been processed, organized,
or direct meaning. structured, or presented in a given context to make it useful
and meaningful.
• Example: The numbers 85, 76, 90. On their own, you
don't know what these numbers represent. Are they • Example: "Marks in Math: 85, Science: 76, English: 90".
temperatures? Scores? Ages? Now, the numbers have context; they are academic scores
for specific subjects.
1 2 3

Organizing Data Efficient Access Easy Modification


The primary goal is to make
it quick and easy to find a
specific piece of data when
you need it, rather than
It's not a simple data type such as
A data structure is not a It's not a language like C, C++,
an integer (like 5), a float (like
standalone software application Java, or Python. Instead, data
or program that you install and structures are abstract concepts 3.14), or a character (like 'A').
run. It's a fundamental concept that can be implemented using Data structures are ways to
used within software any programming language. organize collections of these basic
development. Eg : Chicken Curry recipe remains data types.
ꢀ same irrespective of the language Eg : apple and basket of apple
eg: oven vs jars and boxes
Foundation for Programming Logic
Understanding how to structure data,
like stacks, queues, or arrays, provides
you with fundamental building blocks
that simplify complex programming
challenges. It teaches you to think
about problem-solving in an organized
way.ꢀ
Organization of Data Access Methods
Direct Access: Jump straight to the item using its position/index.
• Iint’ mes thmore way y. data elements are arranged and related to each other • Real life: Knowing your friend’s exact house address →
go directly. Eg : arr[1]
• This arrangement affects how fast you can find, insert, or remove
data. Sequential Access: Go through elements one by one until you find it.
Types : Sequential -Seats In cinema hall (ARRAY) • Real life: Searching for a name in a printed list without knowing page
Hierarchical - CEO , Manager ,Employees (TREE) number. Eg : Linked list

Degree of Associativity
Processing Alternatives
What it means:
• How strongly the data elements are linked to each other. What it means: The operations you can perform on the data and how efficient they
Examples: are.
• Strong association: Common operations: Insertion: Adding new data
◦ Linked List: Each element stores a link to the next one. ◦ Array → slow if you insert in the middle (need to shift elements)
◦ Real life: Train coaches — each one linked to the next. ◦ Linked list → fast insertion anywhere
Weak association: • Deletion: Removing data
•  Traversing
• Searching
• Insertion
• Deletion
• Sorting
• Merging
[Link] List: Boxes connected with arrows: [data|next] → [data|next]

[Link]: Boxes stacked vertically (label: “Last In First Out”)


CLASSIFICATION OF DATA
STRUCTURE
Classification of Data
Structures
• Two types of classification:
• Based on Programming → depends on how data structures
are built inside a programming language.
• Primitive
• Non-Primitive
• (We will focus more on this)
• Based on Memory Allocation → depends on how memory
is given to the data structure when the program runs.
• Static – (Static)
• Dynamic -- (Dynamic)
Primitive Data Types
• Built-in/system-defined data types: int, float, etc.
Pointers also included.
• Store the same type of data.
• Directly manipulated by the machine (e.g., x = x + 5, x = x - 5).
• Machine already knows operators like =, +, -.
Characteristics:
• Fixed size.(An int is usually 4 bytes , cant be changed )
• Efficient in terms of memory and processing. (They take less space and
computer can handle
them quickly.)
• Operations are simple and fast.( Machine already understands operations like
+,-,etc)
Non-Primitive Data Types
• Derived from primitive data types.
• Examples: Array, Stack, Queue, Linked List, Structure, etc.
• User-defined: Programmer designs them as per need.
• Can hold multiple/different data types.
• Not directly understood by machine → programmer defines operations
separately.
• To perform operations → we define ADT (Abstract Data Type).
• Characteristics:
• Can hold multiple data types.
• Dynamic in size.
• May involve complex algorithms.
• Enable efficient organization & retrieval.
• May require more memory and processing than primitive.
Relationship Between Primitive and Non-Primitive
•Non-primitive data structures are built on top of primitive
ones.
•Example:
• int x[10]; → Array (user-defined)
• You cannot directly perform x + 4 ( where x is an array).
•Example (Structure):
•struct student { int roll; string name; };
• Holds different data types.
• Not directly understood by machine → understood by
programmer.
• Linear vs Non-Linear Data Structures
• Linear:
• Data arranged one after another in sequence.
• Even if stored in different memory (like linked list), traversal is sequential → one logical
next element at a time.
• Examples:
• Array (marks of students: 10, 20, 30, 40, 50)
• Stack (plates kept one on top of another)
• Queue (students in a line)
• Linked List (chain of connected nodes)
• Non-Linear:
• Data is not arranged sequentially.
• One element can connect to multiple elements.
• Traversal may require multiple paths.
• Examples:
• Tree (family tree → one parent, many children)
• Graph (map of cities connected by roads)
Key Recap (Self-Revision
Points)

• Data types
• Variables
• Functions (parameter passing types)
• Recursive functions
• Control strings
Structure (Why We Need
It)

• For multiple values of the same type → Arrays.


• Example: Storing 60 roll numbers → int roll[60].
• Problem: Real-life data has different types grouped
together.
• Example: A student → roll number (int), name (string), marks
(float).
• Without structures → too many separate variables
needed for each student.
• Solution → Structure groups different data types
together.
structure – Syntax
Example

•struct student {
int roll_number; // integer
char name[50]; //
string float marks; // float
};
•Within the same structure, member names must be unique.
•Memory is not allocated when defining structure → allocated
only when a variable is declared.
•Total memory allocated = sum of all member sizes.
Union

• A union allows storing different data types in the same


memory location.
• Memory allocated = size of largest member only.
• Only one member can be active at a time.
• Useful when the same memory must represent different data
depending on context.
Where to Use Unions (Use
Cases)
• Unions are particularly useful in scenarios where:
• You need to store different types of data at different times in the same
memory space, but only one piece of data is relevant at any given
moment.
• Memory efficiency is critical, especially when dealing with data that can
take on vastly different sizes or precision levels, but only one is active at a
time
• Distance between Two Objects: Imagine you need to store the distance
between two objects (e.g., planets, cities, particles).
• ◦ Sometimes the distance might be very small (e.g., between two atoms,
requiring an int or float).
• ◦ Other times, it might be extremely large (e.g., between two galaxies,
requiring a long double for high precision and range).
• ◦ Since you only need to store one distance at a time, using a union (e.g.,
union Distance { int small_dist; float medium_dist; long double large_dist; };)
would allocate memory only for the long double (the largest type), saving
significant memory compared to a struct that would sum up all their sizes.
Structure vs Union

struct Point union Value


In this example, the struct Point contains three members, i, f and c, of type
{ {
int, float and char. When you create a variable of type struct Point, it
int i; int i;
allocates memory for i, f and c, and all members are always available to
float f; float f;
use.
char c; char c;
}; };
union Value v;
v.i = 42; // Set the integer member
v.f = 3.14; // Overwrite the integer member with a float
v.c = 'A'; // Overwrite the float member with a character

In this example, the union Value contains three members: i (an


integer), f (a float), and c (a character). However, a variable of type
union Value can only store one of these members at a time.

36
Structure vs Union
• Memory Allocation :
• Structures allocate memory for all of their members
independently, and all members are available at the same
time.
• Unions allocate memory for the largest member, and only one
member can be active at any given time. Changing the active
member can overwrite the values of other members.
Size :
• The size of a structure is the sum of the sizes of all its
members.
• The size of a union is equal to the size of its largest member.
union
• Access:
• In structures, you can access all members
simultaneously.
• In unions, you can access only one active member at
a time.
POINTERS
Understanding Pointers in Programming
This presentation will recap variables and then dive into the fundamental concepts of pointers, their declaration,
initialization, and how they are used to access values in memory.
Variables Recap
Declaration Memory Allocation Core Properties
Each variable has three core
properties: a name, a value,
and an address.
What is a Pointer?

Pointers are special variables.

Pointers are considered derived data types because they are derived using fundamental data types.
Declaring a Pointer
Pointer Data Type Specification
Initializing a Pointer
Pointers must be initialized to point to a valid memory location before use, as uninitialized pointers are
risky and might point to unknown or invalid locations.

Important Rule for Initialization Incorrect Example


The data type of the variable whose address is being
stored must match the data type specified in the
pointer's declaration.
Pointer Memory and "Pointing To"
Just like regular variables, pointers themselves occupy memory and have their own unique memory addresses.

When a pointer holds the address of another variable, it is said to "point to" that variable.
Declaration and Initialization in One Step
01 02

Single Line Declaration & Initialization Variable and Pointer Together


Crucial Order of
Operations
You cannot initialize a pointer with the address of a variable
that has not yet been declared or come into scope.
Accessing Value Using Pointers (Dereferen
The size of a pointer variable depends
on the machine or compiler:
• On a 16-bit machine/compiler, the size of a pointer is 2 bytes

• On a 32-bit machine/compiler, the size of a pointer is 4 bytes

.
int x = 10;int *p = &x;int y = *p; // reads the
value at address p -> y becomes 10

int y=p; // stores the address*p = 20; //


writes 20 into x (because p points to x)
When variables and pointers are
declared, memory is allocated for them:

The Address-Of Operator (&)

• Name: This operator is called the address-of operator or referencing operator

.• Purpose: Its primary function is to return the memory address of a variable

.• Usage in Initialization: It is used to initialize a pointer by assigning it the address of another variable

.◦ Example: p = &a; means p now stores the address of a (e.g., 1000), and p is said to be pointing to a

◦ Similarly, q = &b; means q stores the address of b (e.g., 2000), and q points to b

.
Printing Addresses and Values

◦ Both &a and p (when p points to a) will yield the same address
#include <stdio.h>

int main() {
int x = 42; // Declare an integer variable 'x' and assign value 42
int *p = &x; // Declare a pointer 'p' that stores the address of 'x'

// Printing the value of x directly


printf("Value of x (direct): %d\n", x);

// Printing the value of x using the pointer (dereferencing)


printf("Value of x (using pointer): %d\n", *p);

// Printing the address of x directly using &


printf("Address of x (using &): %p\n", (void*)&x);

// Printing the address stored in pointer p (which is the address of x)


printf("Address stored in p: %p\n", (void*)p);

// Printing the address of the pointer variable itself


printf("Address of pointer p: %p\n", (void*)&p);

return 0;
}
Pointer to a Structure with Dynamic Memory
Allocation
Structure as a Parameter: Pass by
Value
Pointer to a Structure

You might also like