Professional Documents
Culture Documents
UNIT-I ( CHAPTER-2)
What are Data Structures?
► Data structure is a storage that is used to store and organize data. It is a way of arranging
data on a computer so that it can be accessed and updated efficiently.
► Depending on your requirement and project, it is important to choose the right data
structure for your project.
► For example, if you want to store data sequentially in the memory, then you can go for the
Array data structure.
Types of Data Structures
► In linear data structures, the elements are arranged in sequence one after the other. Since
elements are arranged in particular order, they are easy to implement.
► However, when the complexity of the program increases, the linear data structures might
not be the best choice because of operational complexities.
► Popular linear data structures are:
► 1. Array Data Structure
► 2. Stack Data Structure
► 3. Queue Data Structure
► 4. Linked List Data Structure
1. Array Data Structure
► In an array, elements in memory are arranged in continuous memory. All the elements of
an array are of the same type. And, the type of elements that can be stored in the form of
arrays is determined by the programming language.
2. Stack Data Structure
► In stack data structure, elements are stored in the LIFO principle. That is, the last element
stored in a stack will be removed first.
► It works just like a pile of plates where the last plate kept on the pile will be removed
first.
3. Queue Data Structure
► Unlike stack, the queue data structure works in the FIFO principle where first element
stored in the queue will be removed first.
► It works just like a queue of people in the ticket counter where first person on the queue
will get the ticket first.
4. Linked List Data Structure
► In linked list data structure, data elements are connected through a series of nodes. And,
each node contains the data items and address to the next node.
Non linear data structures
► Unlike linear data structures, elements in non-linear data structures are not in any
sequence. Instead they are arranged in a hierarchical manner where one element will be
connected to one or more elements.
► Non-linear data structures are further divided into graph and tree based data structures.
1. Graph Data Structure
► In graph data structure, each node is called vertex and each vertex is connected to other
vertices through edges.
2. Trees Data Structure
► Similar to a graph, a tree is also a collection of vertices and edges. However, in tree data
structure, there can only be one edge between two vertices.
► https://www.javatpoint.com/data-structure-tutorial
► https://www.programiz.com/dsa/data-structure-types
Introduction to Array(1-d & 2-d)
► Array is a data structure that is used to store variables that are of similar data types at
contiguous locations. The main advantage of the array is random access and cache
friendliness. There are mainly three types of the array:
► 1. 1 Dimensional Array (1-d)
► 2. 2 Dimensional Array(2-d)
1 Dimensional Array (1-d)
1. Array size is fixed: The array is static, which means its size is always fixed. The memory
which is allocated to it cannot be increased or decreased.
2. Array is homogeneous:The array is homogeneous, i.e., only one type of value can be
store in the array. For example, if an array type “int“, can only store integer elements and
cannot allow the elements of other data types such as double, float, char so on.
3. Array is Contiguous blocks of memory: The array stores data in contiguous(one by one)
memory location.
4. Insertion and deletion are not easy in Array: The operation insertion and deletion over
an array are problematic as to insert or delete anywhere in the array, it is necessary to
traverse the array and then shift the remaining elements as per the operation. This
operation cost is more.
Applications of Array
► Arrays are used to implement vectors and lists which are an important part of C++ STL.
► Arrays are also used to implement stack and queues.
► Trees also use array implementation whenever possible as arrays are easy to handle
compared to pointers. Tress, in turn, are used to implement various other
types of data structures.
► Matrices which are an important part of the mathematical library in any programming
languages is implemented using arrays.
► Adjacency list implementation of graph uses vectors which are again implemented using
arrays.
► Data structures like a heap, map, and set use binary search tree and balanced binary trees
which uses can be implemented using arrays.
► Arrays are used to maintain multiple variables with the same name.
► CPU scheduling algorithms use implemented using arrays.
► All sorting algorithms use arrays at its core.
► 2D Arrays are used to implement matrices.
► Arrays can be used to implement various data structures like a heap, stack, queue, etc.
► They allow random access.
► They are cache-friendly.
► A vector is similar to an Array. A vector holds multiple number values. In Python, you
can do operations on vectors using things like dot product and cross product, in linear
algebra.
What is Stack Data Structure
► The simplest application of a stack is to reverse a word. You push a given word to stack -
letter by letter - and then pop letters from the stack.
► There are other uses also like:
► Parsing
► Expression Conversion(Infix to Postfix, Postfix to Prefix etc)
Implementation of Stack Data Structure
► Stack can be easily implemented using an Array or a Linked List. Arrays are quick, but
are limited in size and Linked List requires overhead to allocate, link, unlink, and
deallocate, but is not limited in size. Here we will implement Stack using array.
Algorithm for PUSH operation
► Below mentioned are the time complexities for various operations that can be performed
on the Stack data structure.
► Push Operation : O(1)
► Pop Operation : O(1)
► Top Operation : O(1)
► Search Operation : O(n)
► The time complexities for pop() and push() are O(1) because we always have to insert or
remove the data from the top of the stack, which is a one step process.
Advantages of Stack
► The list can be defined as an abstract data type in which the elements are stored in an
ordered manner for easier and efficient retrieval of the elements.
► List Data Structure allows repetition that means a single piece of data can occur more
than once in a list.
► In the case of multiple entries of the same data, each entry of that repeating data is
considered as a distinct item or entry.
► It is very much similar to the array but the major difference between the array and the list
data structure is that array stores only homogenous data in them whereas the list (in some
programming languages) can store heterogeneous data items in its object. List Data
Structure is also known as a sequence.
► The list can be called Dynamic size arrays, which means their size increased as we go on
adding data in them and we need not to pre-define a static size for the list.
For example:-
► numbers = [ 1, 2, 3, 4, 5]
► In this example, 'numbers' is the name of the List Data Structure object and it has five
items stored in it. In the object named numbers, we have stored all the elements of
numeric type. In the list, the indexing starts from zero, which means if we want to access
or retrieve the first element of this list then we need to use index zero and similarly
whenever we want to access any element from this list named numbers. In other words,
we can say that element 1 is on the index 0 and element 2 is on index 1 and similarly for
further all elements.
► Mixed_data = [205, 'Nirnay', 8.56]
► In this second example, mixed_data is the name of the list object that stores the data of
different types. In the mixed_data list, we have stored data of three types, first one is the
integer type which is id '205', after the integer data we have stored a string type data
having the value 'Nirnay' stored at index 1 and at last the index value 2, we have stored a
float type data having the value '8.56'.
Various operations on the List Data
Structure:
► The various operations that are performed on a List Data Structure or Sequence are:
► 1. Add or Insert Operation: In the Add or Insert operation, a new item (of any
data type) is added in the List Data Structure or Sequence object.
► 2. Replace or reassign Operation: In the Replace or reassign operation, the
already existing value in the List object is changed or modified. In other words, a new
value is added at that particular index of the already existing value.
► 3. Delete or remove Operation: In the Delete or remove operation, the already
present element is deleted or removed from the Dictionary or associative array object.
► 4. Find or Lookup or Search Operation: In the Find or Lookup operation, the
element stored in that List Data Structure or Sequence object is fetched.
Advantages of Linked List
► Memory Usage
More memory is required to store elements in linked list as compared to array. Because in
linked list each node contains a pointer and it requires extra memory for itself.
► Traversal
Elements or nodes traversal is difficult in linked list. We can not randomly access any element
as we do in array by index. For example if we want to access a node at position n then we
have to traverse all the nodes before it. So, time required to access a node is large.
► Reverse Traversing
In linked list reverse traversing is really difficult. In case of doubly linked list its easier but
extra memory is required for back pointer hence wastage of memory.
► https://www.studytonight.com/data-structures/counting-sort
► javatpoint.com/list-data-structure
► https://www.thecrazyprogrammer.com/2016/11/advantages-disadvantages-linked-list.html