UNIT-1-Data Structures
UNIT-1-Data Structures
pg. 1
UNIT-1: INTRODUCTION TO DATA STRUCTURE
Record
A record is a collection of data items.
For example, the name, address, course, and marks obtained are individual data items. But
all these data items can be grouped together to form a record.
File
A file is a collection of related records.
For example, if there are 60 students in a class, then there are 60 records of the students.
All these related records are stored in a file.
pg. 2
UNIT-1: INTRODUCTION TO DATA STRUCTURE
Arrays
An array is a collection of similar data elements. These data elements have the same data
type.
The elements of the array are stored in consecutive memory locations and are referenced
by an index (also known as the subscript).
In C, arrays are declared using the following syntax:
type name[size];
For example,
int marks[10];
pg. 3
UNIT-1: INTRODUCTION TO DATA STRUCTURE
Linked Lists
A linked list is a very flexible, dynamic data structure in which elements (called nodes)
form a sequential list.
In a linked list, each node is allocated space as it is added to the list.
Every node in the list points to the next node in the list.
Therefore, in a linked list, every node contains the following two types of data:
The value of the node (data)
A pointer or link to the next node in the list (address)
The last node in the list contains a NULL pointer to indicate that it is the end or tail of the
list.
Since the memory for a node is dynamically allocated when it is added to the list, the total
number of nodes that may be added to a list is limited only by the amount of memory
available.
Figure 1.3 shows a linked list of seven nodes.
pg. 4
UNIT-1: INTRODUCTION TO DATA STRUCTURE
Stacks
A stack is a linear data structure in which insertion and deletion of elements are done at
only one end, which is known as the top of the stack.
Stack is called a last-in, first-out (LIFO) structure because the last element which is added
to the stack is the first element which is deleted from the stack.
In the computer’s memory, stacks can be implemented using arrays or linked lists. Figure
1.4 shows the array implementation of a stack.
Every stack has a variable top associated with it.
Top is used to store the address of the topmost element of the stack.
It is this position from where the element will be added or deleted.
There is another variable MAX, which is used to store the maximum number of elements
that the stack can store.
pg. 5
UNIT-1: INTRODUCTION TO DATA STRUCTURE
The push operation adds an element to the top of the stack. The pop operation removes the
element from the top of the stack. And the peep operation returns the value of the topmost
element of the stack (without deleting it).
However, before inserting an element in the stack, we must check for overflow conditions.
An overflow occurs when we try to insert an element into a stack that is already full.
Similarly, before deleting an element from the stack, we must check for underflow
conditions.
An underflow condition occurs when we try to delete an element from a stack that is already
empty.
Queues
A queue is a first-in, first-out (FIFO) data structure in which the element that is inserted
first is the first one to be taken out.
The elements in a queue are added at one end called the rear and removed from the other
end called the front.
Like stacks, queues can be implemented by using either arrays or linked lists.
Every queue has front and rear variables that point to the position from where deletions and
insertions can be done, respectively.
Consider the queue shown in Fig. 1.5
pg. 6
UNIT-1: INTRODUCTION TO DATA STRUCTURE
Every time a new element is to be added, we will repeat the same procedure.
Now, if we want to delete an element from the queue, then the value of front will be
incremented.
Deletions are done only from this end of the queue. The queue after the deletion will be as
shown in Fig.1.7.
However, before inserting an element in the queue, we must check for overflow conditions.
An overflow occurs when we try to insert an element into a queue that is already full.
Trees
A tree is a non-linear data structure which consists of a collection of nodes arranged in a
hierarchical order. One of the nodes is designated as the root node, and the remaining nodes
can be partitioned into disjoint sets such that each set is a sub-tree of the root.
The simplest form of a tree is a binary tree.
A binary tree consists of a root node and left and right sub-trees, where both sub-trees are
also binary trees.
Each node contains a data element, a left pointer which points to the left sub-tree, and a
right pointer which points to the right sub-tree.
The root element is the topmost node which is pointed by a ‘root’ pointer.
pg. 7
UNIT-1: INTRODUCTION TO DATA STRUCTURE
Graphs
A graph is a non-linear data structure which is a collection of vertices (also called nodes)
and edges that connect these vertices.
In a tree structure, nodes can have any number of children but only one parent, a graph on
the other hand relaxes all such kinds of restrictions.
Figure 1.9 shows a graph with five nodes.
pg. 8
UNIT-1: INTRODUCTION TO DATA STRUCTURE
A node in the graph may represent a city and the edges connecting the nodes can represent
roads.
A graph can also be used to represent a computer network where the nodes are workstations
and the edges are the network connections.
Note that unlike trees, graphs do not have any root node. Rather, every node in the graph
can be connected with every another node in the graph.
When two nodes are connected via an edge, the two nodes are known as neighbours.
For example, in Fig. 1.9, node A has two neighbours: B and D.
What is Data Type? Differentiate between Data Type and Data Structure.
A data type is a classification of data which tells the compiler or interpreter how the
programmer intends to use the data. Most programming languages support various types of
data, including integer, real, character or string, and Boolean etc.
Data Type Data Structure
Data Type is the kind or form of a variable Data Structure is the collection of different
which is being used throughout the program. It kinds of data. That entire data can be
defines that the particular variable will assign represented using an object and can be used
the values of the given data type only throughout the entire program.
Implementation through Data Types is a form of Implementation through Data Structures is
abstract implementation called concrete implementation
Can hold values and not data, so it is data less Can hold different kind and types of data
within one single object
Values can directly be assigned to the data type The data is assigned to the data structure
variables object using some set of algorithms and
operations like push, pop and so on.
No problem of time complexity Time complexity comes into play when
working with data structures
Examples: int, float, double Examples: stacks, queues, tree
pg. 9
UNIT-1: INTRODUCTION TO DATA STRUCTURE
Data type
Data type of a variable is the set of values that the variable can take.
Basic data types in C include int, char, float, and double.
When we talk about a primitive type (built-in data type), we actually consider two things:
1. A data item with certain characteristics and
2. The permissible operations on that data.
For example, an int variable can contain any whole-number value from –32768 to 32767
and can be operated with the operators +, –, *, and /.
Therefore, when we declare a variable of an abstract data type (e.g., stack or a queue),
we also need to specify the operations that can be performed on it.
Abstract
The word ‘abstract’ means considered apart from the detailed specifications or
implementation.
The end-user is not concerned about the details of how the methods carry out their tasks.
They are only aware of the methods that are available to them and are only concerned about
calling those methods and getting the results. They are not concerned about how they work.
For example, when we use a stack or a queue, the user is concerned only with the type of
data and the operations that can be performed on it. They should just know that to work
with stacks, they have push() and pop() functions available to them.
pg. 10
UNIT-1: INTRODUCTION TO DATA STRUCTURE
Example of ADT
Example-1:
AbstractDataType Stack
{
Instances: Stack is a collection of elements in which insertion and deletion of element
is done by one end called top.
Operations
1. Push(): By this operation one can push elements onto the stack. Before performing
push we should check whether stack is full or not.
2. Pop(): By this operation one can remove elements from the stack. Before
performing pop we should check whether stack is empty or not.
}
Example-2:
AbstractDataType queue
{
Instances: The queue is collection of elements in which element can be inserted by one
end called rear and elements get deleted from one end called front.
Operations
1. queue_full(): Checks whether queue is full or not.
2. queue_empty(): Checks whether queue is empty or not.
3. queue_insert(): Inserts the element in queue from rear end.
4. queue_delete(): Deletes the element from the queue by front end.
}
Advantages of ADT:
Abstract data type in data structure makes it very easy for us to use the complex data
structures along with their complex functions.
By using abstract data types, we can also customize any data structure depending on how
we plan to use that particular data structure.
Abstract data type in data structure follows the concept of reusability of a code. This means
that we don't have to write a particular piece of code again and again. We can just create
an abstract data type and we can use it by simply calling the functions present in it.
pg. 11
UNIT-1: INTRODUCTION TO DATA STRUCTURE
Characteristics:
1. Each Algorithm is supplied with zero or more external quantities.
The expected inputs of an algorithm must be well-defined to ensure its correctness,
predictability, and repeatability.
Well-defined inputs ensure that the algorithm's behaviour is deterministic, which means,
that the same input will always produce the same output.
Unambiguous inputs help prevent incorrect implementations and misunderstanding of the
algorithm's requirements.
2. Each Algorithm must produce at least one quantity.
The outputs of an algorithm should be well-defined to ensure that the algorithm produces
the intended and accurate result for a given set of inputs.
It avoids ambiguity and guarantees that the algorithm solves the problem correctly.
It is also easy to verify the correctness of the algorithm's implementation.
3. Each algorithm should have Definiteness.
Ambiguity in the algorithm's description can lead to incorrect implementations and
unreliable results. That is why it is important for an algorithm to be unambiguous.
4. Each algorithm should have Finiteness.
The algorithm should end after a finite amount of time, and it should have a limited number
of instructions.
A finite algorithm ensures that it will eventually stop executing and produce a result.
An infinite algorithm would never reach a conclusion, which is impractical in real-world
scenarios where computation cannot be performed infinitely.
pg. 12
UNIT-1: INTRODUCTION TO DATA STRUCTURE
pg. 13
UNIT-1: INTRODUCTION TO DATA STRUCTURE
Example 2 :
int sum(int a[], int n)
{
int r=0;
for (int i=0;i<n;i++)
{
r+=a[i];
}
return r;
}
Variable Size (if int = 2 bytes)
n 2
a[] 2n
r 2
i 2
Temp 2
Total 8+2n
Frequency Count: The frequency count is a count that denotes how many times particular
statement is executed (for calculating time complexity).
pg. 14
UNIT-1: INTRODUCTION TO DATA STRUCTURE
i = 10;
printf (“ %d” , i);
i = 10 + 5;
Here each instruction executes only once. So frequency count = 1+1+1 = 3 which is
CONSTANT.
Hence, T(n)=O(1).
What is worst case, Best case and Average case with respect to Algorithm Efficiency?
(Measurement of Complexity of an Algorithm)
Worst Case :
In the worst-case analysis, we calculate the upper bound (Maximum Running Time) on the
running time of an algorithm. We must know the case that causes a maximum number of
operations to be executed.
Best Case:
In the best-case analysis, we calculate the lower bound(Minimum Running Time) on the
running time of an algorithm. We must know the case that causes a minimum number of
operations to be executed.
pg. 15
UNIT-1: INTRODUCTION TO DATA STRUCTURE
Average Case:
In average case analysis, we take all possible inputs and calculate the computing time for all of
the inputs. Sum all the calculated values and divide the sum by the total number of inputs.
Linear search is a method for finding a particular value from the given list.
• The algorithm checks each element, one at a time and in sequence, until the desired element
is found.
• Linear search is the simplest search algorithm.
Given an Array a[5] :
2 9 3 1 8
pg. 16
UNIT-1: INTRODUCTION TO DATA STRUCTURE
pg. 17
UNIT-1: INTRODUCTION TO DATA STRUCTURE
pg. 18