





















































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Btech Engineer Data structure and algorithm complete notes in 91 pages pdf
Typology: Cheat Sheet
1 / 93
This page cannot be seen from the preview
Don't miss anything!
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:
The indexing of the array can be defined in three ways.
In the following image, we have shown the memory allocation of an array arr of size
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.
To access any random element of an array we need the following information:
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 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.
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
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.
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,
Example :
isMember
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 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.
Multiple Stacks:
Following pictures are two ways to do two stacks in array: