Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Module_2_Part1

Uploaded by

jbalaji47
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Module_2_Part1

Uploaded by

jbalaji47
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 62

Module 2

DATA STRUCTURES AND


ALGORITHMS

7/29/2024 1
Definitions
What is Data Structure?
“It is set of procedures to define, store, access and
manipulate data”

or

“The organized collection of data is called data structure”

7/29/2024 2
Types of Data Structures

7/29/2024 4
Types of Data
• Primitive
• Non-primitive
• Records and Files

• Primitive(Elementary Data Types)


– The data types which are processed or
manipulated as a single whole (i.e, alone)
– Example: integer, real, charater etc.
– They are also called built-in data types

7/29/2024 5
Types of Data
• Non-Primitive(Structure Data Types)
– The items which are collection of other data
structures are called non-primitive items.
– If more than one values are combined in the single
name is called non-primitive data structures
– Example: Arrays, Stack, Queues etc.

• Types of non-Primitive Data Structures


– Linear
– Non-Linear

7/29/2024 6
Linear Data Structures
• The DS in which there is concept of linearity b/w the
components is called linear data structure.

• The components exists in a certain sequence one after


another.

• They have some successor or predecessor


Example, Arrays, Stack, Queues etc.

7/29/2024 7
Types of Linear Data Structures
• Physical Linear Data Structures
– The linear data structures whose successive
components occupy consecutive memory locations
are called physical linear DS.
– Example: Array
• Logical Linear Data Structures
– The linear DS whose components are accessed
in certain sequence but they are not necessarily
stored in consecutive memory locations.
– Example: Linked Lists
Queues and Stacks are both logical and physical linear data
structures
7/29/2024 8
Non-Linear
• The data structure in which the order of data structure does not
matter and is not fixed
– Example Tree, Graph

• Record
The collection of fields is called record.
What is Domain?
– Set of possible values of an item is called its domain
– For example, set of possible values of Boolean type is 2

7/29/2024 9
Arrays
• A collection of consecutive locations having
same type that can be accessed randomly. They
are physical linear data structure

• Types of Arrays
– One Dimensional Array
– Two Dimensional Array
– Multidimensional Array

7/29/2024 10
One Dimensional Arrays
• 1-dimensional Arrays are also called vectors
– Example, Int A[20];
– The elements of 1-d array are stored in consecutive
memory locations
– The start of array is called its Base Address (BA)
– Let BA denotes the base address of the array and S denotes
the size for each location, then the address of ith location
can be accessed as:
A(i)= BA + (i-1)* S
Generally
A(i)= BA + (i-lb)* S

7/29/2024 11
2-d Array
• Two dimensional array are used to store matrices
• There are two methods for storing 2-d Arrays

Row Major Order


Column Major Order 1 2
For Example int A[3][2]; 3 4

Row Major Order 5 6

1 2 3 4 5 6
Column Major Order
1 3 5 2 4 6

7/29/2024 12
3-d arrays

7/29/2024 13
3 D Array

7/29/2024 14
Operations in Array

• Traversing - print all the array elements one by one.


• Insertion - Adds an element at the given index.
• Deletion - Deletes an element at the given index.
• Updation - Updates an element at the given index.
• Searching - Searches an element using the given index or
by the value.
• Sorting - Sort the elements in the array in increasing or
decreasing order.

7/29/2024 15
Traversing

Step 01: Start

Step 02: [Initialize counter variable. ] Set i = LB.

Step 03: Repeat for i = LB to UB.


Time Complexity:
Step 04: Apply process to arr[i].

Step 05: [End of loop. ]

Step 06: Stop


7/29/2024 16
7/29/2024 17
Insertion
• Step 01: Start
• Step 02: [Reset size of the array. ] set size = size + 1
• Step 03: [Initialize counter variable. ] Set i = size - 1
• Step 04: Repeat Step 05 and 06 for i = size -1 to i >= pos -1
• Step 05: [Move ith element forward. ] set arr[i+1] = arr[i]
• Step 06: [Decrease counter. ] Set i = i - 1
• Step 07: [End of step 04 loop. ]
• Step 08: [Insert element. ] Set arr[pos-1] = x
• Step 09: Stop

7/29/2024 18
Deleting an element in Array

7/29/2024 19
Deletion
Step 01: Start
Step 02: [Initialize counter variable. ] Set i = pos - 1
Step 03: Repeat Step 04 and 05 for i = pos - 1 to i < size
Step 04: [Move ith element backward (left). ] set a[i] = a[i+1]
Step 05: [Increase counter. ] Set i = i + 1
Step 06: [End of step 03 loop. ]
Step 07: [Reset size of the array. ] set size = size - 1
Step 08: Stop
7/29/2024 20
Updation

Step 01: Start


Step 02: Set a[pos-1] = x
Step 03: Stop

7/29/2024 21
Searching an element in Array
Step 01: Start
Step 02: [Initialize counter variable. ] Set i = 0; specify the
search term X
Step 03: Repeat Step 04 and 05 for i = 0 to i < n
Step 04: if a[i] = x, then jump to step 07
Step 05: [Increase counter. ] Set i = i + 1
Step 06: [End of step 03 loop. ]
Step 07: Print x found at i + 1 position and go to step 09
Step 08: Print x not found (if a[i] != x, after all the
iteration of the above for loop. )
Step 09: Stop
7/29/2024 22
7/29/2024 23
Sorting
Step 01: Start
Step 02: Repeat Step 03 to 07 for i = 0 to i < n
Step 03: Repeat Step 04 and 05 for j = i + 1 to j < n
Step 04: Check a statement using the if keyword, If arr[i] >
arr[j], Swap arr[i] and arr[j].
Step 05: [Increase counter. ] Set j = j + 1
Step 06: [End of step 03 loop. ]
Step 07: [Increase counter. ] Set i = i + 1
Step 08: [End of step 02 loop. ]
Step 09: Stop

7/29/2024 24
Sorting

7/29/2024 25
LIST
List is a Sequential data structure and consists of n no of nodes
Each nodes is divided into two parts:
◦The first part contains the information of the element.
◦ The second part contains the memory address of the next node in the
list. Also called Link part.

7/29/2024 26
Example of operations on list

The elements of a list are 34, 12, 52, 16, 12

• Find (52) -> 3


• Insert (20, 4) -> 34, 12, 52, 20, 16, 12
• Delete (52) -> 34, 12, 20, 16, 12
• FindKth (3) -> 20

7/29/2024 27
Linked List

A Linked list is a linear collection of data elements .It has two part one
is info and other is link part.info part gives information and link part is
address of next node.

7/29/2024 28
Types of List
Types of linked lists:

◦Single linked list

◦Doubly linked list

◦Single circular linked list

◦Doubly circular linked list

7/29/2024 29
Singly Linked List
Singly Linked List : A singly linked list contains two fields in each node -an
information field and the linked field.
•The information field contains the data of that node.
•The link field contains the memory address of the next node.

There is only one link field in each node, the linked list is called singly
linked list.

7/29/2024 30
Doubly linked list
Doubly linked list
It is a linked list in which each node is points both to the next node and also to the
previous node.
In doubly linked list each node contains three parts:
FORW : It is a pointer field that contains the address of the next node
BACK: It is a pointer field that contains the address of the previous node.
INFO: It contains the actual data.
In the first node, if BACK contains NULL, it indicated that it is the first node in the list.
The node in which FORW contains, NULL indicates that the node is the last node.

7/29/2024 31
single circular LL

The link field of the last node contains the memory address of
the first node, such a linked list is called circular linked list.

In a circular linked list every node is accessible from a given


node.

7/29/2024 32
Circular doubly linked list
Circular doubly linked list is a more complexed type of data structure in which a
node contain pointers to its previous node as well as the next node.
• Circular doubly linked list doesn't contain NULL in any of the node.
• The last node of the list contains the address of the first node of the list.
• The first node of the list also contain address of the last node in its previous
pointer.

7/29/2024 33
Creating a linked list

The nodes of a linked list can be created by the following structure declaration.
struct Node
{
int info;
struct Node *link;
}*node1, node2;
Here info is the information field and link is the link field.
The link field contains a pointer variable that refers the same node structure. Such a
reference is called as Self addressing pointer.

7/29/2024 34
Stack
•Stack is a linear data structure

•In which the insertion and deletion operations are performed at only one end.

•In a stack, adding and removing of elements are performed at a single position
which is known as "top".

•A Stack is a collection of objects inserted and removed according to the Last In


First Out (LIFO) principle. Think of a stack of books

7/29/2024 35
Representation of Stack

7/29/2024 36
Push()

7/29/2024 37
Pop ()

7/29/2024 38
Status of Stack

7/29/2024 39
peek()

Algorithm of peek() function:


• begin procedure peek
• return stack[top]
• end procedure

7/29/2024 40
isfull()
Algorithm of isfull() function
• begin procedure isfull
• if top equals to MAXSIZE
• return true
• else
• return false
• endif
• end procedure
• }

7/29/2024 41
7/29/2024 42
7/29/2024 43
7/29/2024 44
Advantages of Stack
• A Stack helps to manage the data in the ‘Last in First out’
method.
• It allows you to control and handle memory allocation and
deallocation.
• It helps to automatically clean up the objects.

Disadvantages of Stack
• It is difficult in Stack to create many objects as it increases the
risk of the Stack overflow.
• It has very limited memory.
• In Stack, random access is not possible.

7/29/2024 45
Application of the Stack
• A Stack can be used for evaluating expressions consisting of
operands and operators.

• Stacks can be used for Backtracking, i.e., to check


parenthesis matching in an expression.

• It can also be used to convert one form of expression to


another form.

• It can be used for systematic Memory Management.

7/29/2024 46
Expression Evaluation
• Expression consists of operands and operators
such as arithmetic, logical, relational, unary and
parenthesis.
• simple arithmetic expression

A+B*C/D-E^F*G
• The problem to evaluate this expression is the
order of evaluation.

7/29/2024 47
Precedence and Associativity of operators

7/29/2024 48
Paranthesised Infix expression

((A + B) * ((C / D) - (E ^ (F * G))))

1 2 3

6
7/29/2024 49
Problems in previous methods

• Repeatedly scanning the expression from


left to right
• Ambiguous in generating corresponding
code for given expression
• problem arises if partially paranthesised
These problems can be fixed in two steps
1. conversion of given expression to special notation
2. evaluate the object code using stack

7/29/2024 50
Notations for arithmetic expression
• Conventional way of writing an expression is called Infix
notation
<operand> <operator> <operand>
eg. A+B, 4*7, c-9

• Prefix notation always uses the operator before the operands


<operator> <operand> <operand>
eg. +AB, *47, -c9

• Postfix notation always writes the operator after the operands


<operand> <operand> <operator>
eg. AB+, 47*, c9-
7/29/2024 51
Converting Infix to postfix and prefix
• Assume fully paranthesized version of infix expression.
• Move all operators so that they replace their
corresponding right/left part of parantheses
• Remove all parantheses

7/29/2024 52
• infix :(A+((B^C)-D))*E-(A/C)))

• postfix : A B C ^ D - + E A C / - *

• prefix : * + A - ^ B C D - E / A C

7/29/2024 53
Evaluation of Infix Expression

• two steps involved in evaluation of


expression
• 1. Conversion of Infix to Postfix
• 2. Evaluating Postfix Notation

7/29/2024 54
Rules for converting Infix to Postfix
• Print the operands as they arrive Infix A+B/C
• If stack is empty or contains ‘(‘ then push the operator onto the stack
• If incoming symbol is ‘(‘ push it onto stack
• If incoming symbol is ‘)’ pop the stack until & print operators until ‘(‘ is
found
• If incoming symbol is higher precedence than top of the stack push in onto
the stack
• If incoming symbol is lower precedence than top of the stack then pop and
print the top ( test the symbol with new top)
• If incoming symbol is having equal precedence with top use associativity
rule
Associativity L->R then pop & print top of the stack & push the incoming operator
Associativity R->L then push the incoming operator
7/29/2024 55
• If end of the expression pop and print all operators.
Example Conversion of Infix to Postfix
A+B/C

Read symbol Stack postfix expression

A A
+ + A
B + AB
/ +/ AB
C +/ ABC
end ABC/+

7/29/2024 56
Example 2

A-B/C*D+E
Read symbol Stack Postfix expression

A A
- - A
B - AB
-/ AB
C -/ ABC
* -* ABC/
D -* ABC/D
+ + ABC/D*-
E + ABC/D*-E
END ABC/D*-E+ Postfix expression

7/29/2024 57
Algorithm to evaluate Postfix expression

7/29/2024 58
Example to evaluate the postfix
expression

7/29/2024 59
Infix to Prefix
• First reverse the given expression
• If the scanned character is an operand, put it into prefix expression.
• If the scanned character is an operator and operator's stack is empty, push operator
into operators' stack.
• If the operator's stack is not empty, there may be following possibilities.
• If the precedence of scanned operator is greater than the top most operator of
operator's stack, push this operator into operator 's stack.
• If the precedence of scanned operator is less than the top most operator of
operator's stack, pop the operators from operator's stack untill we find a low
precedence operator than the scanned character.
• If the precedence of scanned operator is equal then check the associativity of the
operator. If associativity left to right then simply put into stack. If associativity right to
left then pop the operators from stack until we find a low precedence operator.
• If the scanned character is opening round bracket ( '(' ), push it into operator's
stack.
• If the scanned character is closing round bracket ( ')' ), pop out operators from
operator's stack until we find an opening bracket ('(' ).
• Now pop out all the remaining operators from the operator's stack and push into
postfix expression.
7/29/2024 60
Queue

• A queue is the simple data structures and


it is very powerful in solving numerous
computer application

7/29/2024 61
• Queue is a linear data structure in which the insertion and deletion
operations are performed at two different ends.
• In a queue data structure, adding and removing elements are performed at
two different positions.
• The insertion is performed at one end and deletion is performed at another
end.
• In a queue data structure, the insertion operation is performed at a position
which is known as 'rear' and the deletion operation is performed at a
position which is known as 'front'.
• The insertion and deletion operations are performed based on FIFO (First
In First Out) principle.

• Example:
• Any waiting line is a queue:
• • The check-out line at a grocery store
• • The cars at a stop light
• • An assembly line
7/29/2024 62
Functions of Queue

The insertion operation is performed using a function called "enQueue()" and


deletion operation is performed using a function called "deQueue()".

7/29/2024 63

You might also like