0% found this document useful (0 votes)
4 views

Data Strctures Unit 1

This document provides an introduction to data structures, defining their types, operations, and the importance of efficient data organization in computer science. It discusses memory allocation methods, including static and dynamic allocation, and details various functions for dynamic memory management in C. Additionally, it highlights the advantages of data structures, such as efficiency and reusability, and classifies them into primitive and non-primitive types, along with their linear and non-linear categories.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Data Strctures Unit 1

This document provides an introduction to data structures, defining their types, operations, and the importance of efficient data organization in computer science. It discusses memory allocation methods, including static and dynamic allocation, and details various functions for dynamic memory management in C. Additionally, it highlights the advantages of data structures, such as efficiency and reusability, and classifies them into primitive and non-primitive types, along with their linear and non-linear categories.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Unit 1

Introduction to Data Structures: Definition, Classification of data structures: primitive


and non-primitive, linear and non-linear, operations on data structures.

Dynamic memory allocation: Meaning of static & dynamic memory allocation; memory
allocation and de-allocation functions: malloc, calloc, realloc and free.
Introduction to Data Structures
•Data Structure can be defined as the group of data elements which provides
an efficient way of storing and organizing data in the computer so that it can
be used efficiently.

• Some examples of Data Structures are arrays, Linked List, Stack, Queue,
etc.

• Data Structures are widely used in almost every aspect of Computer


Science i.e. Operating System, Compiler Design, Artificial intelligence,
Graphics and many more.
Introduction to Data Structures

•Data Structures are the main part of many computer science


algorithms as they enable the programmers to handle the data in an
efficient way.

•It plays a important role in enhancing the performance of a software


or a program as the main function of the software is to store and
retrieve the user's data as fast as possible
Need of Data Structures
As applications are getting complex and amount of data is increasing day by
day, there may arise the following problems:

•Processor speed: To handle very large amount of data, high speed processing
is required, but as the data is growing day by day to the billions of files per
entity, processor may fail to deal with that much amount of data.

•Data Search: Consider an inventory size of 1000 items in a store, If our


application needs to search for a particular item, it needs to traverse 1000 items
every time, results in slowing down the search process.
Need of Data Structures
•Multiple requests: If thousands of users are searching the data
simultaneously on a web server, then there are the chances that a very
large server can be failed during that process.

In order to solve the above problems, data structures are used.


Data is organized to form a data structure in such a way that all items
are not required to be searched and required data can be searched
instantly.
Advantages of Data Structures
•Efficiency:

• Efficiency of a program depends upon the choice of data structures.

•For example: suppose, we have some data and we need to perform


the search for a particular record. In that case, if we organize our data
in an array, we will have to search sequentially element by element.
hence, using array may not be very efficient here.

•There are better data structures which can make the search process
efficient like ordered array, binary search tree or hash tables.
Advantages of Data Structures
•Reusability: Data structures are reusable, i.e. once we have
implemented a particular data structure, we can use it at any other
place. Implementation of data structures can be compiled into libraries
which can be used by different clients.

•Abstraction: Data structure is specified by the ADT(Abstract data


type) which provides a level of abstraction.

•The client program uses the data structure through interface only,
without getting into the implementation details.
Classification of Data Structures
Classification of Data Structures

Data Structures are generally classified into two classes:


•Primitive Data Structures

•Non-primitive Data Structures

Primitive Data Structures


Primitive Data Structures are the fundamental data types which are
supported by programming language. They are created without the
support of other data structure as a support or tool. Examples:
•Integer
•Real(float)
•Characters
•Booleans
Classification of Data Structures

Non-Primitive Data Structures


Non-primitive Data Structures are created using primitive data
structures.

Non-primitive data structures are more complicated data structures


and are derived from primitive data structures. These Data Structures
can be designed by users. Examples:
•Lists
•Graphs
•Stacks
•Queues
•Trees
Classification of Data Structures

Non-Primitive Data Structures can further be classified into two


categories:
•Linear Data Structures
•Non-Linear Data Structures

Linear Data Structures

In a Linear Data Structure, the elements of Data Structure are stored in


a linear or sequential order. Examples:
•Arrays
•Linked Lists
•Stacks
•Queues
Classification of Data Structures

Non-Linear Data Structures

If the elements of a Data Structure are not stored in a sequential order,


then it is a Non-Linear Data Structure.
Examples:
•Trees
•Graphs

•Note:
•Due to its flexible nature, between the two major classification of
data structure, Non-primitive data structure is chosen more.
Operations on Data Structures

1) Traversing: Every data structure contains the set of data elements.


Traversing the data structure means visiting each element of the data
structure in order to perform some specific operation like searching or
sorting.

2) Insertion: Insertion can be defined as the process of adding the


elements to the data structure at any location.

3) Deletion: The process of removing an element from the data


structure is called Deletion. We can delete an element from the data
structure at any random location.
Operations on Data Structures

4) Searching: The process of finding the location of an element


within the data structure is called Searching. There are two algorithms
to perform searching, Linear Search and Binary Search. We will
discuss each one of them later in this tutorial.

5) Sorting: The process of arranging the data structure in a specific


order is known as Sorting. There are many algorithms that can be used
to perform sorting, for example, insertion sort, selection sort, bubble
sort, etc.

6) Merging: When two lists List A and List B of size M and N


respectively, of similar type of elements, clubbed or joined to produce
the third list, List C of size (M+N), then this process is called merging.
Memory allocation

Memory allocation

•Memory can be allocated in the following two ways


Static Memory Allocation

Static variable defines in one block of allocated space, of a fixed


size. Once it is allocated, it can never be freed.
Static Memory allocation

Memory is allocated for the declared variable in the program.


•The address can be obtained by using ‘&’ operator and can be
assigned to a pointer.
•The memory is allocated during compile time.
•It uses stack for maintaining the static allocation of memory.
•In this allocation, once the memory is allocated, the memory size
cannot change.
•It is less efficient.
•The final size of a variable is decided before running the program, it
will be called as static memory allocation. It is also called compile-
time memory allocation.
•We can't change the size of a variable which is allocated at compile-
Dynamic Memory allocation

The concept of dynamic memory allocation enables the


programmers to allocate memory at runtime.

Dynamic memory allocation in c language is possible by


4 functions of stdlib.h header file.

•malloc()

•calloc()

•realloc()


Difference between Static and Dynamic Memory allocation
Dynamic Memory allocation

malloc() function

•The malloc() function allocates single block of requested


memory.
•Allocates a block of memory in bytes at runtime.
•It returns NULL if memory is not sufficient.

•Syntax
ptr=(cast-type*)malloc(byte-size)

•Example
Dynamic Memory allocation

calloc() function

•The calloc() function allocates multiple block of


requested memory.
•It initially initializes all bytes to zero.
•It returns NULL if memory is not sufficient.

•Syntax
ptr=(cast-type*)calloc(number, byte-size)

•Example
ptr=(int*)calloc(n,sizeof(int));
Dynamic Memory allocation

realloc() function
•If memory is not sufficient for malloc() or calloc(),
you can reallocate the memory by realloc() function.

•In short, it changes the memory size.

•Syntax of realloc() function

ptr=realloc(ptr, new-size)
Dynamic Memory allocation

free() function
•The memory occupied by malloc() or calloc()
functions must be released by calling free() function.

•Otherwise, it will consume memory until program


exit.

• Syntax of free() function


free(ptr) ;

You might also like