2. DATA STRUCTURES
✔ A data structure is a scheme for organizing data in the memory of a computer
✔ The way in which the data is organized affects the performance of a program for different
tasks
✔Every data structure has its own strengths, and weaknesses.
✔Also, every data structure specially suits to specific problem types depending upon the
operations performed and the data organization.
2
3. DATA TYPES
3
Data type is a storage format in which a variable can store data to perform
specific operations.
A type of data that can be used in different computer programs such as
integer, char, float, etc.
To use a variable in a program, it must be defined as a data type.
The size of all variables and constants are determined by data types.
4. DATA OBJECT
• A list of elements is called as data object.
• Data object is a storage region that contains a value or group of values.
• Each value can be accessed by its identifier.
• Each object has a unique data type. It is a unique location in memory to store value.
• Data object: a set of elements (D) of a specific data type which may be finite or
infinite
4
6. ABSTRACT DATA TYPE (ADT)
• Data structure defines physical form whereas ADT defines logical form of data type.
• The abstract data type is a special kind of data type, whose behaviour is defined by:
• a set of values D
• a set of operations O
• set of axioms A that describes rules of governing the interactions of operation
Why abstract?
How those operations are working that is totally hidden from the user (abstract)
6
7. ADT EXAMPLES
List operations −
• insert(x): to insert one element into the list
• remove(x): to remove given element from the list
• size(): to get number of elements present into the list
• get(i): to get element at position i
• replace(x, y):to replace x with y value
7
8. Primitive operations performed on data Structure
1. Traversal: To process, each data element accessed exactly once
2. Insertion: To add new data element in given lists of data elements
3. Deletion: To delete existing data elements from given lists of data elements.
4. Searching: If a data element exists in the list of elements then return the location of that data
element.
5. Sorting: To arrange the data elements in ascending or descending order in the case of
numerical values and in the dictionary order in case of alphanumeric values.
6. Merging: To combine two sorted data structures into a single sorted form of data structure.
8
9. TYPES OF DATA STRUCTURES
Primitive Data Structures
Primitive data structures are operated directly according to machine instruction and
are basic structures used in programming languages.
Different computer uses different representations of primitive data structures.
Examples: int, float, char, constants, pointers, etc.
Non-primitive Data Structures
Non-primitive data structures are more sophisticated data structures used in computer
science. These data structures are derived from primitive data structures.
They structure homogeneous and heterogeneous data elements.
Examples: array, structure, list, files, etc.
9
10. TYPES OF DATA STRUCTURES
10
Linear Data Structures
Data is arranged in sequence, and every element is related to its previous and next element.
All elements are stored sequentially, so traversing of elements is possible one after another.
It uses a one-to-one relationship so that at a time, one element is allowed to access.
Even though memory utilization is inefficient, implementation of these data structures is easy.
Examples: array, stack, queue, linked list etc
Non-Linear Data Structures
Data is not arranged in sequence and every element is related to many elements.
Elements are non-sequential, so traversing one after the another element is not possible.
It uses one-to-many relationships.
Even though memory utilization is efficient, implementation of these data structures is difficult.
Examples: tree, graph etc.
11. LINEAR AND NON-LINEAR DATA STRUCTURES
11
Source: https://techvidvan.com/tutorials/data-structure-in-java/
12. LINEAR AND NON-LINEAR DATA STRUCTURES
12
Source: https://techvidvan.com/tutorials/data-structure-in-java/
15. TYPES OF DATA STRUCTURES
16
Physical data Structures
Physical data structures decide how memory is allocated and define how memory is
organized
The basic purpose is to store data in memory.
Examples: array, linked list, etc
Logical data structures
Logical data structures define disciplines.
They are used to express the relationship between data elements and define how to
utilize those values, and how operations are applied on them.
Examples: stack, queue, tree, graph, hash table etc
16. TYPES OF DATA STRUCTURES
17
Static Data Structures
A data structure in which the size of the structure remains fixed.
Without changing the allocated memory space, the contents of the data structure can be
modified.
Its declaration is required at the start of the program as memory is allocated at compile time.
In these data structures, accessing elements is easier.
Examples: array
Dynamic data structures
A structure in which the size of the structure is not fixed and is allocated dynamically
As the contents of the data structure can be modified during the operations performed on it,
the size of memory can be changed.
Its declaration is required when we want to perform operations on it, so memory is allocated
at run time.
These data structures are flexible.
Examples: stack, queue, linked list, etc
17. TYPES OF DATA STRUCTURES
18
Linked data structures
A data structure in which elements may or may not be stored in consecutive
memory locations
To go to a particular element, all elements preceding the element are required
to access.
Its size is not fixed, so a different amount of time is required to access each
element.
In insertion and deletion operations, movement of all elements is not required.
It uses a pointer, so according to programmer requirement, it can be extended
or reduced.
Examples: linked list etc.
18. TYPES OF DATA STRUCTURES
Sequential data structures
A data structure in which elements are stored consecutive memory locations.
To go to a particular element, direct access is possible.
Its size is fixed, so the same amount of time is required to access each
element.
In insertion and deletion operations, movement of elements is required.
According to the programmer’s requirement, it can not be extended or
reduced.
Examples: array
19
20. MEMORY ALLOCATION
✔Memory allocation in programming is very important for storing values when you
assign them to variables
✔allocates memory for the variables declared by a programmer via the compiler
✔allocation is done either before or at the time of program execution
21
21. WAYS FOR MEMORY ALLOCATION
• Compile time allocation or static allocation of memory:
✔ The memory for named variables is allocated by the compiler.
✔ The memory allocated by the compiler is allocated on the stack.
✔ Exact size and storage must be known at compile time.
✔ for array declaration, the size has to be constant.
• Runtime allocation or dynamic allocation of memory:
✔ The memory is allocated at runtime.
✔ The memory space is allocated dynamically within the program run.
✔ Dynamically allocated memory is allocated on the heap.
✔ The exact space or number of the data items does not have to be known by the compiler in advance.
✔ Pointers play a major role for dynamic memory allocation.
22
22. DYNAMIC MEMORY ALLOCATION
• Memory de-allocation is also an important part of this concept
• The "clean-up" of storage space is done for variables or for other data storage.
• It is the job of the programmer to de-allocate dynamically created space.
• For de-allocating dynamic memory, delete operator is used.
• In other words, dynamic memory Allocation refers to performing memory
management for dynamic memory allocation manually.
23
23. MEMORY PARTS IN C++ PROGRAM
• stack:
✔ All variables declared inside any function takes up memory from the stack.
• heap:
✔ It is the unused memory of the program and can be used to dynamically allocate the
memory at runtime.
24
24. THE “NEW” OPERATOR
• To allocate space dynamically, unary operator new is used , followed by the
type being allocated for memory.
✔new int; //dynamically allocates memory for an integer type
✔new double; // dynamically allocates memory an double type
✔new int[30]; // dynamically allocates memory for integer array
25
25. ALLOCATING SPACE FOR NEW
• int * ptr=NULL; // declares a pointer ptr
• ptr = new int; // dynamically allocate an int for loading the address in ptr
• double * i; // declares a pointer i
• i = new double; // dynamically allocate a double and loading the address in i
26
In the above example,
✔ we have declared a pointer variable ‘ptr’ to integer and initialized it to null.
✔ Using the “new” operator memory will be allocate to the “ptr” variable.
26. DYNAMIC MEMORY ALLOCATION FOR ARRAYS
• To allocate memory for an array of characters, i.e., a string of 50 characters. Using that
same syntax, memory can be allocated dynamically
• char* str = NULL; // Pointer initialized with NULL
• str= new char[50]; // Dynamic Allocation will be done
• Here, new operator allocates 50 continuous elements of type characters to the pointer
variable str and returns the pointer to the first element of str.
27
27. DYNAMIC MEMORY ALLOCATION FOR
LINKED LIST
Let us take a structure of a linked list node:
struct node
{
int data;
node *next;
};
node *temp=new node; // dynamic memory allocation
✔ Memory is allocated required for a node by the new operator.
✔ ‘temp’ points to a node (or space allocated for the node).
28
28. EXAMPLE: LINKED LIST USING NEW OPERATOR
#include <iostream>
using namespace std;
struct node
{
int data;
node *next;
};
class linked_list
{
private:
node *head,*tail;
public:
linked_list()
29
{
head = NULL;
tail = NULL;
}
void add_node(int n)
{
node *tmp = new node;
tmp->data = n;
tmp->next = NULL;
if(head == NULL)
{
head = tmp;
tail = tmp;
}
else
{
tail->next = tmp;
tail = tail->next;
}
}
};
int main()
{
linked_list a;
a.add_node(1);
a.add_node(2);
return 0;
}
Source: https://www.codesdope.com/blog/article/c-linked-lists-in-c-singly-linked-list/
29. THE DELETE OPERATOR
• The memory allocated dynamically using the new operator has to be freed explicitly by the
programmer. For this purpose, the “delete” operator is used
• The general syntax of the delete operator is:
delete pointer_variable;
• So memory allocated to the ptr variable can be freed as:
delete ptr;
• This statement frees the memory allocated to the variable “ptr” back to the memory pool.
• delete operator can also be used to free the memory allocated to arrays.
30
30. THE DELETE OPERATOR
• the memory allocated to the array str above can be freed as follows:
delete[] str;
• Note the subscript operator used with the delete operator.
• This is because, as we have allocated the array of elements, we need to free all the locations.
• Instead, if the following statement had executed:
delete str;
• the above statement will only delete the first element of the array.
• Using subscript “[]” all the memory allocated is to be freed.
31
31. MALLOC()
• The malloc() function from C, also exists in C++,
• But it is recommended to avoid using malloc() function.
• malloc() allocates requested size of bytes and returns a pointer to the first
byte of allocated space.
• The main benefit of new over malloc() is that new doesn't just allocate memory,
it constructs objects which is a prime concept of C++.
32
32. DYNAMIC MEMORY ALLOCATION PROGRAMMING EXAMPLE:1
#include <iostream>
using namespace std;
int main()
{
int* i= NULL;
i= new int;
*i= 5;
cout << "Value is : " << *i<< endl; delete val;
}
33
33. EXAMPLE 2 : NEW AND DELETE OPERATORS IN C++.
34
#include <iostream>
#include <string>
using namespace std;
int main()
{
int *p= NULL;
i= new int();
int *a= new int(10);
if(!p)
{
cout<<"bad memory allocation"<<endl;
}
else
{
cout<<"memory allocated successfully"<<endl;
*p= 5;
cout<<"*p= "<<*p<<endl;
cout<<"*a= "<<*a<<endl;
}
double *arr= NULL;
arr= new double[10];
if(!arr)
{cout<<"memory not allocated"<<endl;}
else
{
for(int i=0;i<5;i++)
arr] = i+1;
cout<<“Array values : ";
for(int i=0;i<5;i++)
cout<<arr[i]<<"t";
}
delete p;
delete a;
delete[] arr;
return 0;
35. THE MAJOR DIFFERENCES BETWEEN STATIC AND DYNAMIC MEMORY
ALLOCATIONS
Static Memory Allocation Dynamic Memory Allocation
Memory Allocates variables permanently Memory Allocates variables only if program
unit gets active
Memory Allocation is done before program
execution
Memory Allocation is done during program
execution
Use stack data structure for implementation Use heap data structure for implementation
Less efficient More efficient
no Memory reusable memory reusable and can be freed when not
required
https://www.w3schools.in/difference-in-static-and-dynamic-memory-allocation/