Data Structure
Data Structure
Topics of course:
Arrays
Matrices Physical data structure defines how the data is arranged in
memory.
Linked List
Stack
Queue
Trees Logical data structure it defines how the data is utilized.
Graph
Hashing
Recursion
Sorting
What is a program?
How the program will organize the data in main memory when dealing with it during
execution time.
Data
Memory
Program
Code
Level 1: Know all data structures above and how they work.
Level 2: Know how it works in detail, you can do analysis (time and space
complexity), and you know what operations that performed by them and how its
operations are performed.
Level 3: You can develop your own data structure.
No, because most programming languages have built in code for these data
structures.
Example of C++ code:
#include <iostream>
class a {
public:
a(int a1);
int getA();
};
int main() {
a* a1 = new a(10);
int a2 = a1->getA();
delete a1;
return 0;
a::a(int a1) {
this->a1 = a1;
int a:getA() {
return a1;
Int A [5];
Heap
A[5];
1
4
2
5
3 Stack
Void main() {
Structure: is a collection of data members that are related to data members under
one name and those data members may be of similar type, maybe of unsimilar
type.
Struct Rectangle {
Breadth = Area /
double length; length
S double breadth;
Rectangle struct
struct complexNumber{
double real /*8 bytes*/;
double img;/*8 bytes*/
/*total 16 bytes*/
};
struct students {
double age; /*4 bytes*/
char name[100] /*100 bytes*/;
char address[100] /*100 bytes*/;
char department[100] /*100 bytes*/;
/*total 304 bytes*/
};
struct card {
int color ; /*4 bytes*/
int face; /*4 bytes*/
int shape; /*4 bytes*/
/*total 12 bytes*/
};
Color (0,red )or(1,black)
Face (1,ace) ---- (12,king)
Shape (0,hearts) ----(3,clubs);
struct card {
int color;
int face;
int shape;
};
int main()
{
card myCards[100];
myCards[0] = {0,1,1};
return 0;
}
myCards take memory of (12 * 100) == 1200.
definition of structure does not consume any memory until you create a
struct meaning: struct rectangle r = {1,0};
struct Rectangle {
int length;
int breadth;
char x;
};
The key reason for the discrepancy—why x (a char) appears to take 4 bytes rather
than just 1 byte—relates to (padding and alignment rules in memory).
int length;
int breadth;
char x;
};
int length;
int breadth;
Same a length.
Char x;
the reason
is suppose b in address 0x00
the c will be address 0x08
the length will be in address 0x16 which multiple of 4 which alignment an
integer
breadth will be in address 0x20 which also multiple of 4 (20 / 4 = 5 );
Padding : means it will take extra memory, so it’s easy to our machine to read the
entire structure at the time later it will discard extra three bytes and just used 1
byte.
Pointer
Is an address variable which mean it stores
address of data.
Also, it’s a way to access data indirectly.
int x = 10; // take 4 bytes in memory
int *a = &x; /*On a 32-bit system, a pointer typically takes 4 bytes (32 bits).
cout<<x<<endl;
Heap
Stack
Code section (our program resides)
Program can directly access just (Stack and Code section).
So, program use pointer to access heap memory indirectly And using pointer
to access file that in hard disk or I/O resource or network.
Using of pointer :
Accessing heap
Accessing resources (like file or I/O resource)
Passing parameters
int *p = &x;
When the program ends it will automatically be deleting the heap memory.
All types of pointers take 8 byte of memory.
Reference: nickname given to a variable
Reference does not consume memory; it uses the same memory of variable.
Example:
Int a = 10;
int &r = a;
“r” here does not take any memory it just uses memory of variable “a” not
create new memory
StringBuilder b = c;
#include <stdio.h>
struct Rectangle {
int length;
int breadth;
};
a = a + 10;
int main() {
(*rec).breadth = 20;
cout<<(*rec).length<<endl; ------------- 10
cout<<(*rec).breadth<<endl; ------------ 20
int c =140;
return 0;
function
}
Y:5
Main int main () {
int x = 10;
Main { int y = 5;
Value passing
Reference passing
Pointer passing
Calling by values
Calling by pointers:
One function can access other functions using pointer.
Array as parameters:
For passing array for parameter must type “[]” or like this *a
{
Cout<<a[i]<<endl;
1. Array Declaration:
o When you declare an array, e.g., int arr[5];, arr is the name of the array, and it
refers to the first element of the array.
2. Pointer Behavior:
o In expressions, the name of the array (arr) can be used as a pointer to the first
element of the array. This means that arr can be used in a similar way as a
pointer.
o For example, arr can be used to access the first element (*arr), and the array
can be indexed with arr[i].
3. Pointer to an Array:
o An array name, like arr, decays into a pointer to the first element when passed
to functions, meaning that when you pass arr to a function, it's essentially
passing a pointer to the first element, not a copy of the entire array.
o Memory Location: An array has a fixed size determined at compile time, and
its name refers to the memory location where the entire array starts. In
contrast, a pointer is a variable that can be changed to point to different
memory locations.
o Size: The size of an array is fixed and known at compile time, whereas a
pointer can dynamically point to different memory locations, and you might
not know the size of the data it points to.
Example in C/C++:
Copy code
int *ptr = arr; // Pointer points to the first element of the array
// arr++; // This would result in a compile-time error since arrays cannot be incremented
Key Points:
Array Name as Pointer: In many contexts, the name of the array behaves like a
pointer to its first element.
Array vs. Pointer: Arrays are not pointers, but they can be used like pointers in
many scenarios, especially when passing them to functions.
Fixed vs. Variable Address: While the array's name points to the first element, a
pointer is a variable that can point to any memory location.