Introduction To DS
Introduction To DS
Why DS
• Human requirement with computer are going to complex
day by day. To solve the complex requirements in
efficient way we need this study.
• Provide fastest solution of human requirements.
• Provide efficient solution of complex problems in terms
of ...
– Space
– Time
What is the need of DS
1. It helps us to understand the relationship of one data
element with other data element.
2. It helps us to analyze the data, store it and organize in a
logical or mathematical manner.
3. To store the data in a linear manner
(eg. Month of a year)
4. To store the data in a historical manner
(eg. Historical data)
Sometimes Built-in data types are not enough to handle the complex DS. So
its programmers responsibility to create their own data types (user defined
data types) such custom data type is called as ADT(Abstract Data Types).
The data type made by user or programmer for performing any specific task
of the program called as ADT. Therefore it is represented as
ADT=Type+ function name +behavior of function
Examples of ADT: Stack, Queue, List etc.
For example, a stack is a typical abstract data type. Items stored in a stack can
only be added and removed in certain order – the last item added is the first
item removed. We call these operations, pushing and popping. In this definition,
we haven’t specified have items are stored on the stack, or how the items are
pushed and popped. We have only specified the valid operations that can be
performed.
For example, if we want to read a file, we wrote the code to read the physical
file device. That is, we may have to write the same code over and over again. So
we created what is known
today as an ADT. We wrote the code to read a file and placed it in a library for a
programmer to use.
lists: (Linear linked list) can be defined as a collection of variable number of data
items called nodes. Lists are the most commonly used non- primitive data
structures. Each nodes is divided into two parts:
◦ The first part contains the information of the element.
◦ o The second part contains the memory address of the next node in
the list. Also called Link part.
2) Non-Linear Data structures:
A Non-Linear Data structures is a data structure in which data item is connected to
several other data items. Non-Linear data structure may exhibit either a
hierarchical relationship or
parent child relationship. The data elements are not arranged in a sequential
structure.
The different non-linear data structures are trees and graphs.
tree is a data structure consisting of nodes organized as a hierarchy.
Tree is a hierarchical data structure which stores the information naturally
in the form of hierarchy style.
It is a non-linear data structure compared to arrays, linked lists, stack and
queue.
It represents the nodes connected by edges.
Example 1:
Main()
{
int j,n,a=10;
cout<<“\n Enter the value of n”;
cout<<n;
for(j=0;j<n;j++)
a++;
}
Now let us analyse the above program fragment –
Concentrate on ‘for’ loop
i) j=0 will be executed one time
ii) j<n will be executed n+1 time i.e.
iii) j++ will be executed n times
iv) a++ will be executed n times.