You are on page 1of 42

Data Structure and Types

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

► There are two types of data structures:


1. Primitive data structure
2. Non-primitive data structure

► Primitive Data structure


► The primitive data structures are primitive data types. The int, char, float, double, and
pointer are the primitive data structures that can hold a single value.
► Non-Primitive Data structure
The non-primitive data structure is divided into two types:
1. Linear data structure
2. Non-linear data structure
Linear 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)

► It is a list of the variable of similar data types.


► It allows random access and all the elements can be accessed with the help of their index.
► The size of the array is fixed.
► Representation of 1D array:
2 Dimensional Array(2-d)

► It is a list of lists of the variable of the same data type.


► It also allows random access and all the elements can be accessed with the help of their
index.
► It can also be seen as a collection of 1D arrays. It is also known as the Matrix.
► Its dimension can be increased from 2 to 3 and 4 so on.
► They all are referred to as a multi-dimension array.
► The most common multidimensional array is a 2D array.
► Representation of 2 D array:
Ex of 2-d array
Advantages of Arrays

► Below are some advantages of the array:


1. In an array, accessing an element is very easy by using the index number.
2. The search process can be applied to an array easily.
3. 2D Array is used to represent matrices.
4. For any reason a user wishes to store multiple values of similar type then the Array can be
used and utilized efficiently.
Disadvantages of Arrays

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

► Stack is an abstract data type with a bounded(predefined) capacity. It is a simple data


structure that allows adding and removing elements in a particular order. Every time an
element is added, it goes on the top of the stack and the only element that can be removed
is the element that is at the top of the stack, just like a pile of objects.
Basic features of Stack

► Stack is an ordered list of similar data type.


► Stack is a LIFO(Last in First out) structure or we can say FILO(First in Last out).
► Push() function is used to insert new elements into the Stack and pop() function is used to
remove an element from the stack. Both insertion and removal are allowed at only one
end of Stack called Top.
► Stack is said to be in Overflow state when it is completely full and is said to be
in Underflow state if it is completely empty.
Applications of Stack

► 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

► Check if the stack is full or not.


► If the stack is full, then print error of overflow and exit the program.
► If the stack is not full, then increment the top and add the element.

► Algorithm for POP operation


► Check if the stack is empty or not.
► If the stack is empty, then print error of underflow and exit the program.
► If the stack is not empty, then print the element at the top and decrement the top.
Analysis of Stack Operations

► 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 advantages of using stack are listed below:


► Efficient data management: Stack helps you manage the data in a LIFO (last in, first
out) method, which is not possible with a Linked list and array.
► Efficient management of functions: When a function is called, the local variables are
stored in a stack, and it is automatically destroyed once returned.
► Control over memory: Stack allows you to control how memory is allocated and
deallocated.
► Smart memory management: Stack automatically cleans up the object.
► Not easily corrupted: Stack does not get corrupted easily; hence it is more secure and
reliable.
► Does not allow resizing of variables: Variables cannot be resized.
Disadvantages of Stack

► The disadvantages of using stack are listed below:


► Limited memory size: Stack memory is very limited.
► Chances of stack overflow: Creating too many objects on the stack can increase the risk
of stack overflow.
► Random access is not possible: In a stack, random accessing the data is not possible.
► Unreliable: When variable storage will get overwritten, it will sometimes leads to
undefined behaviour of the function or program.
► Undesired termination: The stack will fall outside of the memory area, which might
lead to an abnormal termination.
List Data Structure

► 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

► Dynamic Data Structure


Linked list is a dynamic data structure so it can grow and shrink at runtime by allocating and
deallocating memory. So there is no need to give initial size of linked list.
► Insertion and Deletion
Insertion and deletion of nodes are really easier. Unlike array here we don’t have to shift
elements after insertion or deletion of an element. In linked list we just have to update the
address present in next pointer of a node.
► No Memory Wastage
As size of linked list can increase or decrease at run time so there is no memory wastage. In
case of array there is lot of memory wastage, like if we declare an array of size 10 and store
only 6 elements in it then space of 4 elements are wasted. There is no such problem in linked
list as memory is allocated only when required.
► Implementation
Data structures such as stack and queues can be easily implemented using linked list.
Disadvantages 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

You might also like