Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Data Structure and Algorithm pdf Notes, Cheat Sheet of Data Structures and Algorithms

Btech Engineer Data structure and algorithm complete notes in 91 pages pdf

Typology: Cheat Sheet

2024/2025

Available from 01/02/2025

sanjay-saharan
sanjay-saharan 🇮🇳

1 document

1 / 93

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
DATA STRUCTURE AND ALGORITHM
Unit I
Arrays and sequential representations ordered lists Stacks and Queues Evaluation
ofExpressions Multiple Stacks and Queues Singly Linked List Linked Stacks and
queues Polynomial addition.
Data Structure Introduction:
The data structure name indicates itself that organizing the data in memory.
There are many ways of organizing the data in the memory as we have already seen
one of the data structures, i.e., array in C language. Array is a collection of memory
elements in which data is stored sequentially, i.e., one after another. In other words,
we can say that array stores the elements in a continuous manner. This organization
of data is done with the help of an array of data structures. There are also other ways
to organize the data in memory. To structure the data in memory, 'n' number of
algorithms were proposed, and all these algorithms are known as Abstract data
types. These abstract data types are the set of rules.
Types of Data Structures
There are two types of data structures:
o Primitive data structure
o 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
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d

Partial preview of the text

Download Data Structure and Algorithm pdf Notes and more Cheat Sheet Data Structures and Algorithms in PDF only on Docsity!

DATA STRUCTURE AND ALGORITHM

Unit I

Arrays and sequential representations – ordered lists – Stacks and Queues – Evaluation

ofExpressions – Multiple Stacks and Queues – Singly Linked List – Linked Stacks and

queues – Polynomial addition.

Data Structure Introduction:

The data structure name indicates itself that organizing the data in memory.

There are many ways of organizing the data in the memory as we have already seen

one of the data structures, i.e., array in C language. Array is a collection of memory

elements in which data is stored sequentially, i.e., one after another. In other words,

we can say that array stores the elements in a continuous manner. This organization

of data is done with the help of an array of data structures. There are also other ways

to organize the data in memory. To structure the data in memory, 'n' number of

algorithms were proposed, and all these algorithms are known as Abstract data

types. These abstract data types are the set of rules.

Types of Data Structures

There are two types of data structures:

o Primitive data structure o 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:

 Linear data structure  Non-linear data structure

Linear Data Structure

The arrangement of data in a sequential manner is known as a linear data structure.

The data structures used for this purpose are Arrays, Linked list, Stacks, and Queues.

In these data structures, one element is connected to only one another element in a

linear form.

Non- Linear Structure:

When one element is connected to the 'n' number of elements known as a non-

linear data structure. The best example is trees and graphs. In this case, the elements are arranged in a random manner.

Data structures can also be classified as:

Static data structure: It is a type of data structure where the size is allocated at the compile time. Therefore, the maximum size is fixed.  Dynamic data structure: It is a type of data structure where the size is allocated at the run time. Therefore, the maximum size is flexible.

Major Operations

The major or the common operations that can be performed on the data structures

are:

o Searching: We can search for any element in a data structure. o Sorting: We can sort the elements of a data structure either in an ascending or descending order. o Insertion: We can also insert the new element in a data structure. o Updation: We can also update the element, i.e., we can replace the element with another element. o Deletion: We can also perform the delete operation to remove the element from the data structure.

Advantages of Data structures

The following are the advantages of a data structure:

Memory Allocation of the array

As we have mentioned, all the data elements of an array are stored at

contiguous locations in the main memory. The name of the array represents

the base address or the address of first element in the main memory. Each

element of the array is represented by a proper indexing.

The indexing of the array can be defined in three ways.

  1. 0 (zero - based indexing) : The first element of the array will be arr[0].
  2. 1 (one - based indexing) : The first element of the array will be arr[1].
  3. n (n - based indexing) : The first element of the array can reside at any random index number.

In the following image, we have shown the memory allocation of an array arr of size

  1. The array follows 0-based indexing approach. The base address of the array is 100th byte. This will be the address of arr[0]. Here, the size of int is 4 bytes therefore each element will take 4 bytes in the memory.

In 0 based indexing, If the size of an array is n then the maximum index number, an

element can have is n- 1. However, it will be n if we use 1 based indexing.

Accessing Elements of an array

To access any random element of an array we need the following information:

  1. Base Address of the array.
  2. Size of an element in bytes.
  3. Which type of indexing, array follows.

Address of any element of a 1D array can be calculated by using the following

formula:

Byte address of element A[i] = base address + size * ( i - first index )

2D Array

2D array can be defined as an array of arrays. The 2D array is organized as matrices which can be represented as the collection of rows and columns.

However, 2D arrays are created to implement a relational database look alike data structure. It provides ease of holding bulk of data at once which can be passed to any number of functions wherever required.

How to declare 2D Array

The syntax of declaring two dimensional array is very much similar to

that of a one dimensional array, given as follows.

  1. int arr[max_rows][max_columns];

however, It produces the data structure which looks like following.

25M

Above image shows the two dimensional array, the elements are organized in the form of rows and columns. First element of the first row is represented by a[0][0] where the number shown in the first index is the number of that row while the number shown in the second index is the number of the column.

The size of a two dimensional array is equal to the multiplication of number of rows and the number of columns present in the array. We do need to map two dimensional array to the one dimensional array in order to store them in the memory.

A 3 X 3 two dimensional array is shown in the following image. However, this array

needs to be mapped to a one dimensional array in order to store it into the memory.

There are two main techniques of storing 2D array elements into memory

  1. Row Major ordering

In row major ordering, all the rows of the 2D array are stored into the memory

contiguously. Considering the array shown in the above image, its memory allocation according to row major order is shown as follows.

first, the 1st^ row of the array is stored into the memory completely, then the 2nd^ row of the array is stored into the memory completely and so on till the last row.

  1. Column Major ordering

According to the column major ordering, all the columns of the

2D array are stored into the memory contiguously. The memory

allocation of the array which is shown in in the above image is

given as follows.

first, the 1st^ column of the array is stored into the memory completely, then the 2 nd^ row of the array is stored into the memory completely and so on till the last column of the array.

Calculating the Address of the random element of a 2D array

Due to the fact that, there are two different techniques of storing the two dimensional array into the memory, there are two different formulas to calculate the address of a random element of the 2D array.

By Row Major Order

If array is declared by a[m][n] where m is the number of rows while n is the number of columns, then address of an element a[i][j] of the array stored in row major order is calculated as,

  1. Address(a[i][j]) = B. A. + (i * n + j) * size where, B. A. is the base address or the address of the first element of the array a[0][0].

Example :

  1. a[10...30, 55...75], base address of the array (BA) = 0, size of an element = 4 bytes.
  2. Find the location of a[15][68].
  3. Address(a[15][68]) = 0 +

isMember

  • used to test whether a given object instance is in the container.

stack

A real-world stack allows operations at one end only. For example, we can place or remove a card or plate from the top of the stack only. At any given time, we can only access the top element of a stack. This feature makes it LIFO data structure. LIFO stands for Last-in-first-out. Here, the element which is placed (inserted or added) last, is accessed first. In stack terminology, insertion operation is called PUSH operation and removal operation is called POP operation.

Stack Representation

The following diagram depicts a stack and its operations −

A stack can be implemented by means of Array, Structure, Pointer, and Linked List. Stack can either be a fixed size one or it may have a sense of dynamic resizing. Here, we are going to implement stack using arrays, which makes it a fixed size stack implementation.

Basic Operations

Stack operations may involve initializing the stack, using it and then de-initializing it. Apart from these basic stuffs, a stack is used for the following two primary operations −  push() − Pushing (storing) an element on the stack.  pop() − Removing (accessing) an element from the stack. When data is PUSHed onto stack. To use a stack efficiently, we need to check the status of stack as well. For the same purpose, the following functionality is added to stacks −  peek() − get the top data element of the stack, without removing it.  isFull() − check if stack is full.

isEmpty() − check if stack is empty. At all times, we maintain a pointer to the last PUSHed data on the stack. The top pointer provides top value of the stack without actually removing it. First we should learn about procedures to support stack functions −

peek()

Algorithm of peek() function −

begin procedure peek return stack[top] end procedure

isfull()

Algorithm of isfull() function −

begin procedure isfull

if top equals to MAXSIZE return true else return false endif

end procedure

isempty()

Algorithm of isempty() function −

begin procedure isempty

if top less than 1 return true else return false endif

end procedure

Implementation of isempty() function in C programming language is slightly different. We initialize top at -1, as the index in array starts from 0. So we check if the top is below zero or -1 to determine if the stack is empty. Here's the code −

Push Operation

The process of putting a new data element onto stack is known as a Push Operation. Push operation involves a series of steps −  Step 1 − Checks if the stack is full.  Step 2 − If the stack is full, produces an error and exit.

data ← stack[top] top ← top - 1 return data

end procedure

Queue

Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queue is open at both its ends. One end is always used to insert data (enqueue) and the other is used to remove data (dequeue). Queue follows First-In-First-Out methodology, i.e., the data item stored first will be accessed first.

Queue Representation

As we now understand that in queue, we access both ends for different reasons. The following diagram given below tries to explain queue representation as data structure −

Basic Operations Queue operations may involve initializing or defining the queue, utilizing it, and then completely erasing it from the memory. Here we shall try to understand the basic operations associated with queues −  enqueue() − add (store) an item to the queue.  dequeue() − remove (access) an item from the queue. Few more functions are required to make the above-mentioned queue operation efficient. These are −  peek() − Gets the element at the front of the queue without removing it.  isfull() − Checks if the queue is full.  isempty() − Checks if the queue is empty. In queue, we always dequeue (or access) data, pointed by front pointer and while enqueing (or storing) data in the queue we take help of rear pointer.

peek()

This function helps to see the data at the front of the queue. The algorithm of peek() function is as follows − Algorithm

begin procedure peek return queue[front] end procedure

isfull()

As we are using single dimension array to implement queue, we just check for the rear pointer to reach at MAXSIZE to determine that the queue is full. In case we maintain the queue in a circular linked-list, the algorithm will differ. Algorithm of isfull() function – Algorithm begin procedure isfull

if rear equals to MAXSIZE return true else return false endif

end procedure

isempty() Algorithm of isempty() function − Algorithm begin procedure isempty

if front is less than MIN OR front is greater than rear return true else return false endif

end procedure If the value of front is less than MIN or 0, it tells that the queue is not yet initialized, hence empty.

Enqueue Operation

Queues maintain two data pointers, front and rear. Therefore, its operations are comparatively difficult to implement than that of stacks. The following steps should be taken to enqueue (insert) data into a queue −  Step 1 − Check if the queue is full.  Step 2 − If the queue is full, produce overflow error and exit.  Step 3 − If the queue is not full, increment rear pointer to point the next empty space.  Step 4 − Add data element to the queue location, where the rear is pointing.  Step 5 − return success.

Algorithm for dequeue operation

procedure dequeue

if queue is empty return underflow end if

data = queue[front] front ← front + 1 return true

end procedure

Evaluation of Expression

The way to write arithmetic expression is known as a notation. An arithmetic expression can be written in three different but equivalent notations, i.e., without changing the essence or output of an expression. These notations are –  Infix Notation  Prefix (Polish) Notation  Postfix (Reverse-Polish) Notation These notations are named as how they use operator in expression. We shall learn the same here in this chapter.

Infix Notation

We write expression in infix notation, e.g. a - b + c, where operators are used in - between operands. It is easy for us humans to read, write, and speak in infix notation but the same does not go well with computing devices. An algorithm to

process infix notation could be difficult and costly in terms of time and space consumption.

Prefix Notation

In this notation, operator is prefix ed to operands, i.e. operator is written ahead of operands. For example, +ab. This is equivalent to its infix notation a + b. Prefix notation is also known as Polish Notation.

Postfix Notation

This notation style is known as Reversed Polish Notation. In this notation style, the operator is postfix ed to the operands i.e., the operator is written after the operands. For example, ab+. This is equivalent to its infix notation a + b. The following table briefly tries to show the difference in all three notations −

Sr.No. Infix Notation Prefix Notation Postfix Notation

1 a + b + a b a b +

2 (a + b) ∗ c ∗ + a b c a b + c ∗

(^3) a ∗ (b + c) ∗ a + b c a b c + ∗

4 a / b + c / d + / a b / c d a b / c d / +

(^5) (a + b) ∗ (c + d) ∗ + a b + c d a b + c d + ∗

6 ((a + b) ∗ c) - d - ∗ + a b c d a b + c ∗ d -

Parsing Expressions

As we have discussed, it is not a very efficient way to design an algorithm or program to parse infix notations. Instead, these infix notations are first converted into either postfix or prefix notations and then computed. To parse any arithmetic expression, we need to take care of operator precedence and associativity also.

Precedence

When an operand is in between two different operators, which operator will take the operand first, is decided by the precedence of an operator over others. For example −

Operand1 Operand2 Operator

Example

Postfix Expression Evaluation using Stack Data

Structure

A postfix expression can be evaluated using the Stack data structure. To evaluate a postfix expression using Stack data structure we can use the following steps... Read all the symbols one by one from left to right in the given Postfix Expression

**1. If the reading symbol is operand, then push it on to the Stack.

  1. If the reading symbol is operator (+ , - , * , / etc.,), then perform TWO pop** operations and store the two popped oparands in two different variables (operand and operand2). Then perform reading symbol operation using operand1 and **operand2 and push result back on to the Stack.
  2. Finally! perform a pop operation and display the popped value as final result.** Example Consider the following Expression..

Multiple Stacks and Queues:

Multiple Stacks:

Following pictures are two ways to do two stacks in array:

  1. None fixed size of the stacks: