Unit I
Unit I
A data structure is a way of organizing the data so that it can be used efficiently.
Here, we have used the word efficiently, which in terms of both the space and time. For
example, a stack is an ADT (Abstract data type) which uses either arrays or linked list data
structure for the implementation. Therefore, we conclude that we require some data structure
to implement a particular ADT.
An ADT tells what is to be done and data structure tells how it is to be done. In
other words, we can say that ADT gives us the blueprint while data structure provides the
implementation part. Now the question arises: how can one get to know which data structure
to be used for a particular ADT?
As the different data structures can be implemented in a particular ADT, but the
different implementations are compared for time and space. For example, the Stack ADT can
be implemented by both Arrays and linked list. Suppose the array is providing time efficiency
while the linked list is providing space efficiency, so the one which is the best suited for the
current user's requirements will be selected.
1. Primitive Data Structure : Primitive Data Structures are the data structures
consisting of the numbers and the characters that come in-built into programs.
A data structure that preserves a linear connection among its data elements is known
as a Linear Data Structure. The arrangement of the data is done linearly, where each
element consists of the successors and predecessors except the first and the last data
element. However, it is not necessarily true in the case of memory, as the arrangement
may not be sequential.
Non-Linear Data Structures
Non-Linear Data Structures are data structures where the data elements are not arranged in
sequential order. Here, the insertion and removal of data are not feasible in a linear manner.
There exists a hierarchical relationship between the individual data items.
In the following section, we will discuss the different types of operations that we can perform
to manipulate data in every data structure:
1. Traversal: Traversing a data structure means accessing each data element exactly
once so it can be administered. For example, traversing is required while printing the
names of all the employees in a department.
2. Search: Search is another data structure operation which means to find the location of
one or more data elements that meet certain constraints. Such a data element may or
may not be present in the given set of data elements. For example, we can use the
search operation to find the names of all the employees who have the experience of
more than 5 years.
3. Insertion: Insertion means inserting or adding new data elements to the collection.
For example, we can use the insertion operation to add the details of a new employee
the company has recently hired.
4. Deletion: Deletion means to remove or delete a specific data element from the given
list of data elements. For example, we can use the deleting operation to delete the
name of an employee who has left the job.
5. Sorting: Sorting means to arrange the data elements in either Ascending or
Descending order depending on the type of application. For example, we can use the
sorting operation to arrange the names of employees in a department in alphabetical
order or estimate the top three performers of the month by arranging the performance
of the employees in descending order and extracting the details of the top three.
6. Merge: Merge means to combine data elements of two sorted lists in order to form a
single list of sorted data elements.
7. Create: Create is an operation used to reserve memory for the data elements of the
program. We can perform this operation using a declaration statement. The creation of
data structure can take place either during the following:
a. Compile-time
b. Run Time
For example, the malloc() function is used in C Language to create data structure.
8. Selection: Selection means selecting a particular data from the available data. We can
select any particular data by specifying conditions inside the loop.
9. Update: The Update operation allows us to update or modify the data in the data
structure. We can also update any particular data by specifying some conditions inside
the loop, like the Selection operation.
10. Splitting: The Splitting operation allows us to divide data into various subparts
decreasing the overall process completion time.
From the data structure point of view, following are some important categories of algorithms
1. sum=0;
2. // Suppose we have to calculate the sum of n numbers.
3. for i=1 to n
4. sum=sum+i;
5. // when the loop ends then sum holds the sum of the n numbers
6. return sum;
In the above code, the time complexity of the loop statement will be atleast n, and if the value
of n increases, then the time complexity also increases. While the complexity of the code, i.e.,
return sum will be constant as its value is not dependent on the value of n and will provide
the result in one step only. We generally consider the worst-time complexity as it is the
maximum time taken for any given input size.
Auxiliary space: The extra space required by the algorithm, excluding the input size, is
known as an auxiliary space. The space complexity considers both the spaces, i.e., auxiliary
space, and space used by the input.
So,