Data Structures and Algorithms
ROAD MAP
What is An Algorithm ?
A finite, clearly specified sequence of instructions to be followed to solve a problem.
Example:
Properties of an Algorithm
What is a Data Structure ?
An organization and representation of data
Concept of Data Structure ?
Properties of a Data Structure ?
Classification of Data Structure
Classification of Data Structure
Analysis of Algorithm
Mathematical Background
For input size N
Running time of Algorithm A = TA(N) = 1000 N
Running time of Algorithm B = TB(N) = N2
Which one is faster ?
N | TA | TB |
10 | 10-2 sec | 10-4 sec |
100 | 10-1 sec | 10-2 sec |
1000 | 1 sec | 1 sec |
10000 | 10 sec | 100 sec |
100000 | 100 sec | 10000 sec |
If the unit of running time of algorithms A and B is µsec
So which algorithm is faster ?
If N<1000 TA(N) > TB(N)
o/w TB(N) > TA(N)
Compare their relative growth ?
Big Oh Notation (O)
Provides an “upper bound” for the function f
T(N) = O (f(N)) if there are positive constants c and n0 such that
T(N) ≤ cf(N) when N ≥ n0
Big Oh Notation (O)
1000 N ≤ cN
if c= 2000 and n0 = 1 for all N
is right
Examples
for c=8 and n0 =5
7n+5 ≤ 8n n>5 = n0
for c=7 and n0=2
7n+5 ≤ 7n2 n≥n0
Advantages of O Notation
O(7n2) = O(n2)
Omega Notation (Ω)
T(N) = Ω (f(N)) if there are positive constants c and n0 such that T(N) ≥ c f(N) when N≥ n0
Theta Notation (θ)
T(N) = θ (h(N)) if and only if
T(N) = O(h(N)) and T(N) = Ω(h(N))
Theta Notation (θ)
T(N) = 3N2
T(N) = O(N4)
T(N) = O(N3)
T(N) = θ(N2) 🡪 best
Little O Notation (o)
T(N) = o(p(N)) if
T(N) = O(p(N)) and T(N)≠θ(p(N))
Little O Notation (o)
T(N) = 3N2
T(N) = o(N4)
T(N) = o(N3)
T(N) = θ(N2)
What is Data Abstraction?
What is an Abstract Data Type?
ADT Implementation
ARRAYS
Data type - what kind of values it can store. For example, int, char, float
Name - to identify the array
Size - the maximum number of values that the array can hold
type name[size];
1st element | 2nd element | 3rd element | 4th element | 5th element | 6th element | 7th element | 8th element | 9th element | 10th element |
marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6] marks[7] marks[8] marks[9]
Introduction
Accessing Elements of an Array
int i, marks[10];
for(i=0;i<10;i++)
marks[i] = -1;
Calculating the Address of Array Elements
where
A is the array
k is the index of the element whose address we have to calculate
BA is the base address of the array A
w is the word size of one element in memory. For example, size of int is 2
99 | 67 | 78 | 56 | 88 | 90 | 34 | 85 |
marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6] marks[7]
1000 1002 1004 1006 1008 1010 1012 1014
marks[4] = 1000 + 2(4 – 0)
= 1000 + 2(4) = 1008
Storing Values in Arrays
Store values in the array
Initialize the elements
Input values for the elements
Assign values to the elements
int i, marks[10];
for(i=0;i<10;i++)
scanf(“%d”, &marks[i]);
int i, arr1[10], arr2[10];
for(i=0;i<10;i++)
arr2[i] = arr1[i];
Inputting Values from Keyboard
Assigning Values to Individual Elements
Initializing Arrays during declaration
int marks [5] = {90, 98, 78, 56, 23};
Calculating the Length of an Array
Length = upper_bound – lower_bound + 1
where
upper_bound is the index of the last element
lower_bound is the index of the first element in the array
99 | 67 | 78 | 56 | 88 | 90 | 34 | 85 |
marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6 marks[7]]
Here, lower_bound = 0, upper_bound = 7
Therefore, length = 7 – 0 + 1 = 8
WAP to Read and Display N Numbers using an Array
#include<stdio.h>
#include<conio.h>
int main()
{
int i=0, n, arr[20];
clrscr();
printf(“\n Enter the number of elements : ”);
scanf(“%d”, &n);
printf(“\n Enter the elements : ”);
WAP to Read and Display N Numbers using an Array
for(i=0;i<n;i++)
{
printf(“\n arr[%d] = ”, i);
scanf(“%d”, &num[i]);
}
printf(“\n The array elements are ”);
for(i=0;i<n;i++)
printf(“arr[%d] = %d\t”, i, arr[i]);
return 0;
}
Inserting an Element in an Array
Algorithm to insert a new element to the end of an array
Step 1: Set upper_bound = upper_bound + 1
Step 2: Set A[upper_bound] = VAL
Step 3; EXIT
Algorithm INSERT( A, N, POS, VAL) to insert an element VAL at position POS
Step 1: [INITIALIZATION] SET I = N
Step 2: Repeat Steps 3 and 4 while I >= POS
Step 3: SET A[I + 1] = A[I]
Step 4: SET I = I – 1
[End of Loop]
Step 5: SET N = N + 1
Step 6: SET A[POS] = VAL
Step 7: EXIT
Deleting an Element from an Array
Algorithm to delete an element from the end of the array
Step 1: Set upper_bound = upper_bound - 1
Step 2: EXIT
Algorithm DELETE( A, N, POS) to delete an element at POS
Step 1: [INITIALIZATION] SET I = POS
Step 2: Repeat Steps 3 and 4 while I <= N-1
Step 3: SET A[I] = A[I + 1]
Step 4: SET I = I + 1
[End of Loop]
Step 5: SET N = N - 1
Step 6: EXIT
Pointers and Arrays
int *ptr;
ptr = &arr[0];
int *ptr = &arr[0];
ptr++;
printf (“The value of the second element in the array is %d”, *ptr);
Arrays of Pointers
int *ptr[10];
int *ptr[10];
int p=1, q=2, r=3, s=4, t=5;
ptr[0]=&p;
ptr[1]=&q;
Arrays of Pointers
ptr[2]=&r;
ptr[3]=&s;
ptr[4]=&t
Can you tell what will be the output of the following statement?
printf(“\n %d”, *ptr[3]);
Arrays of Pointers
The output will be 4 because ptr[3] stores the address of integer variable s and *ptr[3] will therefore print the value of s that is 4.
Two-dimensional Arrays
A two-dimensional array is specified using two subscripts where one subscript denotes row and the other denotes column.
C looks at a two-dimensional array as an array of one-dimensional arrays.
A two-dimensional array is declared as:
data_type array_name[row_size][column_size];
| | | |
| | | |
| | | |
| | | |
Second Dimension
First Dimension
Two-dimensional Arrays
Therefore, a two dimensional m×n array is an array that contains m×n data elements and each element is accessed using two subscripts, i and j, where i<=m and j<=n
int marks[3][5];
Rows/Columns | Col 0 | Col 1 | Col2 | Col 3 | Col 4 |
Row 0 | Marks[0][0] | Marks[0][1] | Marks[0][2] | Marks[0][3] | Marks[0][4] |
Row 1 | Marks[1][0] | Marks[1][1] | Marks[1][2] | Marks[1][3] | Marks[1][4] |
Row 2 | Marks[2][0] | Marks[2][1] | Marks[2][2] | Marks[2][3] | Marks[2][4] |
Two Dimensional Array
Memory Representation of a 2D Array
| | | | | | | | | | | |
(0,0) (0, 1) (0,2) (0,3) (1,0) (1,1) (1,2) (1,3) (2,0) (2,1) (2,2) (2,3)
Memory Representation of a 2D Array
| | | | | | | | | | | |
(0,0) (1,0) (2,0) (3,0) (0,1) (1,1) (2,1) (3,1) (0,2) (1,2) (2,2) (3,2)
Initializing Two-dimensional Arrays
A two-dimensional array is initialized in the same was as a single dimensional array is initialized. For example,
int marks[2][3]={90, 87, 78, 68, 62, 71};
int marks[2][3]={{90,87,78},{68, 62, 71}};
Calculating the Address of Array Elements
A is a 2D array of M*N
Row Major Order
LOC(A[J,K])=Base(A)+w[M(K-1)+(J-1)]
Column Major Order
LOC(A[J,K])=Base(A)+w[N(J-1)+(K-1)]
Multi-dimensional Arrays
I1<=M1 I2<=M2 I3 <= M3 ……… In <= Mn
Multi-dimensional Arrays
Multi-dimensional Arrays
Suppose the array is of n-dimension having the size/length of each dimension as L1,L2,L3, . .. Ln .Base Address is BA.Index number of the element to find the address is given as i1, i2, i3, . . . .in
Then the formula for column major order will be:
Address of A[i1][ i2]. . . [ in] = BA(A) + W*[((…ENLN-1+ EN-1 )LN-2 +... E3 )L2+ E2 )L1 +E1 ]
Then the formula for row major order will be:
Address of A[i1][ i2][ i3]. . . .[ in] = BA(A) + W*[(E1L2 + E2 )L3 +E3 )L4 ….. + EN-1 )LN + EN ]
Where E(effective index)
Ei= Ki-(lower bound)i
Example: An int array A[50][40][30] is stored at the address 1000. Find the address of A[40][20][10].
Sparse Matrix
A matrix is a two-dimensional data object made of m rows and n columns, therefore having total m x n values. If most of the elements of the matrix have 0 value, then it is called a sparse matrix.
Why to use Sparse Matrix instead of simple matrix ?
Storage: There are lesser non-zero elements than zeros and thus lesser memory can be used to store only those elements.
Computing time: Computing time can be saved by logically designing a data structure traversing only non-zero elements..
Example:
0 0 3 0
4 0 0 5
7 0 0 0
0 0 0 0
2 6 0 0
Sparse Matrix
Representing a sparse matrix by a 2D array leads to wastage of lots of memory as zeroes in the matrix are of no use in most of the cases. So, instead of storing zeroes with non-zero elements, we only store non-zero elements. This means storing non-zero elements with triples- (Row, Column, value).
Sparse Matrix Representations can be done in many ways following are two common representations:
Sparse Matrix
Method 1: Using Arrays
2D array is used to represent a sparse matrix in which there are three rows named as
Row: Index of row, where non-zero element is located
Column: Index of column, where non-zero element is located
Value: Value of the non zero element located at index – (row,column)
Sparse Matrix
Method 2: Using Linked Lists
In linked list, each node has four fields. These four fields are defined as:
Row: Index of row, where non-zero element is located
Column: Index of column, where non-zero element is located
Value: Value of the non zero element located at index – (row,column)
Next node: Address of the next node
Applications of Arrays
Operations On Arrays
1.Traversing
2.Insertion
3.Deletion
4.Searching
a)linear search
b)binary search
5.Sorting
a)bubble sort[O(n2)]
b)merge sort[(O(n)]
c)quick sort etc[(nlog(n)]
Traversing Linear Arrays
55
| | | | | | | |
•••
Linear Array
Traversing Linear Arrays
56
| | | | | | | |
•••
Linear Array
Traversing Linear Arrays
57
| | | | | | | |
•••
Linear Array
Traversing Linear Arrays
58
| | | | | | | |
•••
Linear Array
Apply PROCESS to LA[K]
[End of Loop]
2. Exit
Inserting and Deleting
59
Insertion
60
1 | Brown |
2 | Davis |
3 | Johnson |
4 | Smith |
5 | Wagner |
6 | |
7 | |
8 | |
1 | Brown |
2 | Davis |
3 | Johnson |
4 | Smith |
5 | Wagner |
6 | Ford |
7 | |
8 | |
Insert Ford at the End of Array
Insertion
61
1 | Brown |
2 | Davis |
3 | Johnson |
4 | Smith |
5 | Wagner |
6 | |
7 | |
8 | |
1 | Brown |
2 | Davis |
3 | Johnson |
4 | Smith |
5 | Wagner |
6 | Wagner |
7 | |
8 | |
Insert Ford as the 3rd Element of Array
1 | Brown |
2 | Davis |
3 | Johnson |
4 | Smith |
5 | Smith |
6 | Wagner |
7 | |
8 | |
1 | Brown |
2 | Davis |
3 | |
4 | Johnson |
5 | Smith |
6 | Wagner |
7 | |
8 | |
Ford
Insertion is not Possible without loss of data if the array is FULL
Deletion
62
1 | Brown |
2 | Davis |
3 | Ford |
4 | Johnson |
5 | Smith |
6 | Taylor |
7 | Wagner |
8 | |
1 | Brown |
2 | Davis |
3 | Ford |
4 | Johnson |
5 | Smith |
6 | Taylor |
7 | |
8 | |
Deletion of Wagner at the End of Array
Deletion
63
1 | Brown |
2 | Davis |
3 | Ford |
4 | Johnson |
5 | Smith |
6 | Taylor |
7 | Wagner |
8 | |
1 | Brown |
2 | Ford |
3 | Ford |
4 | Johnson |
5 | Smith |
6 | Taylor |
7 | Wagner |
8 | |
Deletion of Davis from the Array
1 | Brown |
2 | Ford |
3 | Johnson |
4 | Johnson |
5 | Smith |
6 | Taylor |
7 | Wagner |
8 | |
1 | Brown |
2 | Ford |
3 | Johnson |
4 | Smith |
5 | Smith |
6 | Taylor |
7 | Wagner |
8 | |
Deletion
64
1 | Brown |
2 | Ford |
3 | Johnson |
4 | Smith |
5 | Taylor |
6 | Wagner |
7 | |
8 | |
No data item can be deleted from an empty array
Insertion Algorithm
1. [Initialize Counter] Set J := N
2. Repeat Steps 3 and 4 while J ≥ K
3. [Move the Jth element downward ] Set LA[J + 1] := LA[J]
4. [Decrease Counter] Set J := J -1
5 [Insert Element] Set LA[K] := ITEM
6. [Reset N] Set N := N +1;
7. Exit
65
Deletion Algorithm
1. Set ITEM := LA[K]
2. Repeat for J = K to N -1:
[Move the J + 1st element upward] Set LA[J] := LA[J + 1]
3. [Reset the number N of elements] Set N := N - 1;
4. Exit
66
5
12
35
42
77
101
1 2 3 4 5 6
5
12
35
42
77
101
1 2 3 4 5 6
Sorting
"Bubbling Up" the Largest Element
5
12
35
42
77
101
1 2 3 4 5 6
Contd…
"Bubbling Up" the Largest Element
5
12
35
42
77
101
1 2 3 4 5 6
Swap
42
77
Contd…
"Bubbling Up" the Largest Element
5
12
35
77
42
101
1 2 3 4 5 6
Swap
35
77
Contd…
"Bubbling Up" the Largest Element
5
12
77
35
42
101
1 2 3 4 5 6
Swap
12
77
Contd…
"Bubbling Up" the Largest Element
5
77
12
35
42
101
1 2 3 4 5 6
No need to swap
Contd…
"Bubbling Up" the Largest Element
5
77
12
35
42
101
1 2 3 4 5 6
Swap
5
101
Contd…
LINKED LIST
Introduction
List of advantages :
List of Disadvantages :
Types of Linked List
1.Simple/Single linked list
2.Doubly linked list
3.Circular linked list
4.Doubly circular linked list.
Simple Linked List
1
2
3
4
5
6
7
X
START
Traversing Linked Lists
struct node
{
int data;
struct node *next;
};
START Pointer
1
DATA | NEXT |
H | 4 |
| |
| |
E | 7 |
| |
| |
L | 8 |
L | 10 |
| |
O | -1 |
1 2 3 4 5 6 7 8 9 10 |
START
START pointing to the first element of the linked list in memory
9
AVAIL
Singly Linked Lists
1
2
3
4
5
6
7
X
START
ALGORITHM FOR TRAVERSING A LINKED LIST
Step 1: [INITIALIZE] SET PTR = START
Step 2: Repeat Steps 3 and 4 while PTR != NULL
Step 3: Apply Process to PTR->DATA
Step 4: SET PTR = PTR->NEXT
[END OF LOOP]
Step 5: EXIT
Searching a Linked List
ALGORITHM TO SEARCH A LINKED LIST
Step 1: [INITIALIZE] SET PTR = START
Step 2: Repeat Step 3 while PTR != NULL
Step 3: IF VAL = PTR->DATA
SET POS = PTR
Go To Step 5
ELSE
SET PTR = PTR->NEXT
[END OF IF]
[END OF LOOP]
Step 4: SET POS = NULL
Step 5: EXIT
Searching for Val 4 in Linked List
1
7
3
4
2
6
5
X
PTR
1
7
3
4
2
6
5
X
PTR
1
7
3
4
2
6
5
X
PTR
1
7
3
4
2
6
5
X
PTR
Inserting a Node at the Beginning
1
7
3
4
2
6
5
X
START
START
9
1
7
3
4
2
6
5
X
ALGORITHM TO INSERT A NEW NODE IN THE BEGINNING OF THE LINKED LIST
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 7
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET New_Node->Next = START
Step 6: SET START = New_Node
Step 7: EXIT
Inserting a Node at the End
1
7
3
4
2
6
5
X
START, PTR
1
7
3
4
2
6
5
9
X
START
ALGORITHM TO INSERT A NEW NODE AT THE END OF THE LINKED LIST
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 10
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET New_Node->Next = NULL
Step 6: SET PTR = START
Step 7: Repeat Step 8 while PTR->NEXT != NULL
Step 8: SET PTR = PTR ->NEXT
[END OF LOOP]
Step 9: SET PTR->NEXT = New_Node
Step 10: EXIT
Inserting a Node after Node that has Value NUM
ALGORITHM TO INSERT A NEW NODE AFTER A NODE THAT HAS VALUE NUM
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 12
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET PTR = START
Step 6: SET PREPTR = PTR
Step 7: Repeat Steps 8 and 9 while PREPTR->DATA != NUM
Step 8: SET PREPTR = PTR
Step 9: SET PTR = PTR->NEXT
[END OF LOOP]
Step 10: PREPTR->NEXT = New_Node
Step 11: SET New_Node->NEXT = PTR
Step 12: EXIT
Deleting the First Node
Algorithm to delete the first node from the linked list
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 5
[END OF IF]
Step 2: SET PTR = START
Step 3: SET START = START->NEXT
Step 4: FREE PTR
Step 5: EXIT
1
7
3
4
2
6
5
X
7
3
4
2
6
5
X
START
START
Deleting the Last Node
ALGORITHM TO DELETE THE LAST NODE OF THE LINKED LIST
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Steps 4 and 5 while PTR->NEXT != NULL
Step 4: SET PREPTR = PTR
Step 5: SET PTR = PTR->NEXT
[END OF LOOP]
Step 6: SET PREPTR->NEXT = NULL
Step 7: FREE PTR
Step 8: EXIT
1
7
3
4
2
6
5
X
START, PREPTR, PTR
1
7
3
4
2
6
X
5
X
PREPTR PTR
START
Deleting the Node After a Given Node
ALGORITHM TO DELETE THE NODE AFTER A GIVEN NODE FROM THE LINKED LIST
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 10
[END OF IF]
Step 2: SET PTR = START
Step 3: SET PREPTR = PTR
Step 4: Repeat Step 5 and 6 while PRETR->DATA != NUM
Step 5: SET PREPTR = PTR
Step 6: SET PTR = PTR->NEXT
[END OF LOOP]
Step7: SET TEMP = PTR->NEXT
Step 8: SET PREPTR->NEXT = TEMP->NEXT
Step 9: FREE TEMP
Step 10: EXIT
Singly Linked List
1
7
3
4
2
6
5
X
START, PREPTR, PTR
1
7
3
4
2
6
5
X
PREPTR PTR
START
1
7
3
4
2
6
5
X
START
1
7
3
4
6
5
X
Circular Linked List
1
2
3
4
5
6
7
START
Circular Linked List
Algorithm to insert a new node in the beginning of circular the linked list
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 7
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET PTR = START
Step 6: Repeat Step 7 while PTR->NEXT != START
Step 7: PTR = PTR->NEXT
Step 8: SET New_Node->Next = START
Step 8: SET PTR->NEXT = New_Node
Step 6: SET START = New_Node
Step 7: EXIT
Circular Linked List
1
7
3
4
2
6
5
START, PTR
1
7
3
4
2
6
5
START
PTR
9
1
7
3
4
2
6
5
START
Circular Linked List
Algorithm to insert a new node at the end of the circular linked list
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 7
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET New_Node->Next = START
Step 6: SET PTR = START
Step 7: Repeat Step 8 while PTR->NEXT != START
Step 8: SET PTR = PTR ->NEXT
[END OF LOOP]
Step 9: SET PTR ->NEXT = New_Node
Step 10: EXIT
Circular Linked List
1
7
3
4
2
6
5
START, PTR
1
7
3
4
2
6
5
9
START
PTR
Circular Linked List
Algorithm to insert a new node after a node that has value NUM
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 12
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET PTR = START
Step 6: SET PREPTR = PTR
Step 7: Repeat Step 8 and 9 while PTR->DATA != NUM
Step 8: SET PREPTR = PTR
Step 9: SET PTR = PTR->NEXT
[END OF LOOP]
Step 10: PREPTR->NEXT = New_Node
Step 11: SET New_Node->NEXT = PTR
Step 12: EXIT
Circular Linked List
Algorithm to delete the first node from the circular linked list
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Step 4 while PTR->NEXT != START
Step 4: SET PTR = PTR->NEXT
[END OF IF]
Step 5: SET PTR->NEXT = START->NEXT
Step 6: FREE START
Step 7: SET START = PTR->NEXT
Step 8: EXIT
Circular Linked List
1
7
3
4
2
6
5
START, PTR
1
7
3
4
2
6
5
START
PTR
7
3
4
2
6
5
START
Circular Linked List
Algorithm to delete the last node of the circular linked list
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Step 4 while PTR->NEXT != START
Step 4: SET PREPTR = PTR
Step 5: SET PTR = PTR->NEXT
[END OF LOOP]
Step 6: SET PREPTR->NEXT = START
Step 7: FREE PTR
Step 8: EXIT
Circular Linked List
1
7
3
4
2
6
5
START, PREPTR, PTR
1
7
3
4
2
6
5
START
PTR
PREPTR
1
7
3
4
2
6
START
Circular Linked List
Algorithm to delete the node after a given node from the circular linked list
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 9
[END OF IF]
Step 2: SET PTR = START
Step 3: SET PREPTR = PTR
Step 4: Repeat Step 5 and 6 while PREPTR->DATA != NUM
Step 5: SET PREPTR = PTR
Step 6: SET PTR = PTR->NEXT
[END OF LOOP]
Step 7: SET PREPTR->NEXT = PTR->NEXT
Step 8: FREE PTR
Step 9: EXIT
Circular Linked List
1
7
3
4
2
6
5
START, PREPTR, PTR
1
7
3
4
2
6
5
START
PREPTR PTR
1
7
3
4
6
5
START
Doubly Linked List
1
X
1
2
3
4
X
START
Doubly Linked List
struct node
{ struct node *prev;
int data;
struct node *next;
};
Doubly Linked List
Algorithm to insert a new node in the beginning of the doubly linked list
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 8
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET New_Node->PREV = NULL
Step 6: SET New_Node->Next = START
Step 7: SET START = New_Node
Step 8: EXIT
1
7
3
4
2
X
X
9
1
7
3
4
X
2
X
START
START
Doubly Linked List
Algorithm to insert a new node at the end of the doubly linked list
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 11
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET New_Node->Next = NULL
Step 6: SET PTR = START
Step 7: Repeat Step 8 while PTR->NEXT != NULL
Step 8: SET PTR = PTR->NEXT
[END OF LOOP]
Step 9: SET PTR->NEXT = New_Node
Step 10: New_Node->PREV = PTR
Step 11: EXIT
1
7
3
4
2
X
X
START, PTR
1
7
3
4
2
X
9
X
PTR
Doubly Linked List
Algorithm to insert a new node after a node that has value NUM
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 11
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET PTR = START
Step 6: Repeat Step 8 while PTR->DATA != NUM
Step 7: SET PTR = PTR->NEXT
[END OF LOOP]
Step 8: New_Node->NEXT = PTR->NEXT
Step 9: SET New_Node->PREV = PTR
Step 10: SET PTR->NEXT = New_Node
Step 11: EXIT
Doubly Linked List
1
7
3
4
2
X
X
START, PTR
1
7
3
4
2
X
X
9
1
7
3
9
4
X
2
X
START
START
PTR
Doubly Linked List
Algorithm to delete the first node from the doubly linked list
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 6
[END OF IF]
Step 2: SET PTR = START
Step 3: SET START = START->NEXT
Step 4: SET START->PREV = NULL
Step 5: FREE PTR
Step 6: EXIT
1
7
3
4
2
X
X
START, PTR
7
3
4
2
X
Doubly Linked List
Algorithm to delete the last node of the doubly linked list
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 7
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Step 4 and 5 while PTR->NEXT != NULL
Step 4: SET PTR = PTR->NEXT
[END OF LOOP]
Step 5: SET PTR->PREV->NEXT = NULL
Step 6: FREE PTR
Step 7: EXIT
1
3
5
7
8
X
91
X
START, PTR
1
3
5
7
8
X
91
X
START
PTR
1
3
5
7
8
X
X
START
Doubly Linked List
Algorithm to delete the node after a given node from the doubly linked list
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 9
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Step 4 while PTR->DATA != NUM
Step 4: SET PTR = PTR->NEXT
[END OF LOOP]
Step 5: SET TEMP = PTR->NEXT
Step 6: SET PTR->NEXT = TEMP->NEXT
Step 7: SET TEMP->NEXT->PREV = PTR
Step 8: FREE TEMP
Step 9: EXIT
Doubly Linked List
1
3
4
7
8
X
91
X
1
3
4
7
8
X
91
X
1
3
4
8
9
X
X
START, PTR
START
PTR
START
Circular Doubly Linked List
Circular Doubly Linked List
1
1
2
3
4
START
Circular Doubly Linked List
Algorithm to insert a new node in the beginning of the circular doubly linked list
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 12
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 6: SET START->PREV->NEXT = new_node;
Step 7: SET New_Node->PREV = START->PREV;
Step 8: SET START->PREV= new_Node;
Step 9: SET new_node->next = START;
Step 10: SET START = New_Node
Step 11: EXIT
Circular Doubly Linked List
1
7
3
4
2
START
9
1
7
3
4
2
START
Circular Doubly Linked List
Algorithm to insert a new node at the end of the circular doubly linked list
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 11
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET New_Node->Next = START
Step 6: SET New_Node->PREV = START->PREV
Step 7: EXIT
1
7
3
4
2
1
7
3
4
2
9
START
START
Circular Doubly Linked List
Algorithm to insert a new node after a node that has value NUM
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 11
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET PTR = START
Step 6: Repeat Step 8 while PTR->DATA != NUM
Step 7: SET PTR = PTR->NEXT
[END OF LOOP]
Step 8: New_Node->NEXT = PTR->NEXT
Step 9: SET PTR->NEXT->PREV = New_Node
Step 9: SET New_Node->PREV = PTR
Step 10: SET PTR->NEXT = New_Node
Step 11: EXIT
Circular Doubly Linked List
1
7
3
4
2
START, PTR
1
7
3
4
2
9
START
PTR
1
7
3
9
4
2
START
Circular Doubly Linked List
Algorithm to delete the first node from the circular doubly linked list
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = START
Step 3: SET PTR->PREV=>NEXT= PTR->NEXT
Step 4: SET PTR->NEXT->PREV = PTR->PREV
Step 5: SET START = START->NEXT
Step 6: FREE PTR
Step 7: EXIT
1
3
5
7
8
91
START, PTR
3
5
7
8
91
START
Circular Doubly Linked List
Algorithm to delete the last node of the circular doubly linked list
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = START->PREV
Step 5: SET PTR->PREV->NEXT = START
Step 6: SET START->PREV = PTR->PREV
Step 7: FREE PTR
Step 8: EXIT
1
3
5
7
8
91
START
PTR
1
3
5
7
8
X
START
Circular Doubly Linked List
Algorithm to delete the node after a given node from the circular doubly linked list
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 9
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Step 4 while PTR->DATA != NUM
Step 4: SET PTR = PTR->NEXT
[END OF LOOP]
Step 5: SET TEMP = PTR->NEXT
Step 6: SET PTR->NEXT = TEMP->NEXT
Step 7: SET TEMP->NEXT->PREV = PTR
Step 8: FREE TEMP
Step 9: EXIT
Circular Doubly Linked List
1
3
5
7
8
91
START
1
3
5
7
8
91
START
PTR
1
3
4
8
9
START
STACKS
Introduction
The topmost plate will be removed first
Another plate will be added on top of this plate
Stacks
Array Representation of Stacks
Push Operation
A | B | C | D | E | | | | | |
0 1 2 3 TOP = 4 5 6 7 8 9
A | B | C | D | E | F | | | | |
0 1 2 3 4 TOP =5 6 7 8 9
Pop Operation
A | B | C | D | E | | | | | |
0 1 2 3 TOP = 4 5 6 7 8 9
A | B | C | D | | | | | | |
0 1 2 TOP = 3 4 5 6 7 8 9
Peek Operation
A | B | C | D | E | | | | | |
0 1 2 3 TOP = 4 5 6 7 8 9
Here Peep operation will return E, as it is the value of the topmost element of the stack.
Algorithms for Push and Pop Operations
Algorithm to PUSH an element in a stack
Step 1: IF TOP = MAX-1, then
PRINT “OVERFLOW”
Goto Step 4
[END OF IF]
Step 2: SET TOP = TOP + 1
Step 3: SET STACK[TOP] = VALUE
Step 4: END
Algorithm to POP an element from a stack
Step 1: IF TOP = NULL, then
PRINT “UNDERFLOW”
Goto Step 4
[END OF IF]
Step 2: SET VAL = STACK[TOP]
Step 3: SET TOP = TOP - 1
Step 4: END
Algorithm for Peep Operation
Algorithm for Peep Operation
Step 1: IF TOP =NULL, then
PRINT “STACK IS EMPTY”
Go TO Step 3
[END OF IF]
Step 2: RETURN STACK[TOP]
Step 3: END
Linear Representation of Stacks
1
7
3
4
2
6
5
X
TOP
Push Operation on a Linked Stack
Algorithm to PUSH an element in a linked stack
Step 1: Allocate memory for the new node and name it as New_Node
Step 2: SET New_Node->DATA = VAL
Step 3: IF TOP = NULL, then
SET New_Node->NEXT = NULL
SET TOP = New_Node
ELSE
SET New_node->NEXT = TOP
SET TOP = New_Node
[END OF IF]
Step 4: END
1
7
3
4
2
6
5
X
TOP
9
1
7
3
4
2
6
5
X
TOP
Pop Operation on a Linked Stack
Algorithm to POP an element from a stack
Step 1: IF TOP = NULL, then
PRINT “UNDERFLOW”
Goto Step 5
[END OF IF]
Step 2: SET PTR = TOP
Step 3: SET TOP = TOP ->NEXT
Step 4: FREE PTR
Step 5: END
9
1
7
3
4
2
6
5
X
TOP
1
7
3
4
2
6
5
X
TOP
Applications of Stacks
Infix Notation
Postfix Notation
Postfix Notation
AB+C* in the postfix notation.
Prefix Notation
*+ABC in the prefix notation
Evaluation of an Infix Expression
Algorithm to convert an Infix notation into postfix notation
Step 1: Add ‘)” to the end of the infix expression
Step 2: Push “(“ on to the stack
Step 3: Repeat until each character in the infix notation is scanned
IF a “(“ is encountered, push it on the stack
IF an operand (whether a digit or an alphabet) is encountered,
add it to the postfix expression.
IF a “)” is encountered, then;
b. Discard the “(“. That is, remove the “(“ from stack and do not
add it to the postfix expression
IF an operator X is encountered, then;
b. Push the operator X to the stack
Step 4: Repeatedly pop from the stack and add it to the postfix expression
until the stack is empty
Step 5: EXIT
STEP 1: Convert the infix expression into its equivalent postfix expression
Evaluation of an Infix Expression
STEP 2: Evaluate the postfix expression
Algorithm to evaluate a postfix expression
Step 1: Add a “)” at the end of the postfix expression
Step 2: Scan every character of the postfix expression and repeat
steps 3 and 4 until “)”is encountered
Step 3: IF an operand is encountered, push it on the stack
IF an operator X is encountered, then
a. pop the top two elements from the stack as A and B
b. Evaluate B X A, where A was the topmost element and B was
the element below A.
c. Push the result of evaluation on the stack
[END OF IF]
Step 4: SET RESULT equal to the topmost element of the stack
Step 5: EXIT
Evaluation of an Infix Expression
Character scanned | Stack |
9 | 9 |
3 | 9, 3 |
4 | 9, 3, 4 |
* | 9, 12 |
8 | 9, 12, 8 |
+ | 9, 20 |
4 | 9, 20, 4 |
/ | 9, 5 |
- | 4 |
Tower of Hanoi
Tower of Hanoi is one of the main applications of a recursion. It says, "if you can solve n-1 cases, then you can easily solve the nth case"
A
B
C
A
B
C
If there is only one ring, then simply move the ring from source to the destination
B
A
B
C
A
C
A
B
C
If there are two rings, then first move ring 1 to the spare pole and then move ring 2 from source to the destination. Finally move ring 1 from the source to the destination
A
B
C
Tower of Hanoi
Consider the working with three rings.
A
B
C
A
B
C
A
B
C
A
B
C
A
B
C
A
B
C
A
B
C
A
B
C
QUEUES
Introduction
People moving on an escalator. The people who got on the escalator first will be the first one to step out of it.
Array Representation of Queues
0 1 2 3 4 5 6 7 8 9
12 | 9 | 7 | 18 | 14 | 36 | | | | |
12 | 9 | 7 | 18 | 14 | 36 | 45 | | | |
0 1 2 3 4 5 6 7 8 9
Array Representation of Queues
| 9 | 7 | 18 | 14 | 36 | 45 | | | |
0 1 2 3 4 5 6 7 8 9
Algorithm for Insertion Operation
Algorithm to insert an element in a queue
Step 1: IF REAR=MAX-1, then;
Write OVERFLOW
Goto Step 4
[END OF IF]
Step 2: IF FRONT == -1 and REAR = -1, then
SET FRONT = REAR = 0
ELSE
SET REAR = REAR + 1
[END OF IF]
Step 3: SET QUEUE[REAR] = NUM
Step 4: Exit
Array Representation of Queues
Algorithm for Deletion Operation
Algorithm to delete an element from a queue
Step 1: IF FRONT = -1 OR FRONT > REAR, then
Write UNDERFLOW
Goto Step 2
ELSE
SET VAL = QUEUE[FRONT]
SET FRONT = FRONT + 1
[END OF IF]
Step 2: Exit
Linked Representation of Queues
1
7
3
4
2
6
5
X
FRONT
REAR
Inserting an Element in a Linked Queue
Algorithm to insert an element in a linked queue
Step 1: Allocate memory for the new node and name it as PTR
Step 2: SET PTR->DATA = VAL
Step 3: IF FRONT = NULL, then
SET FRONT = REAR = PTR
SET FRONT->NEXT = REAR->NEXT = NULL
ELSE
SET REAR->NEXT = PTR
SET PTR->NEXT = NULL
[END OF IF]
Step 4: END
Deleting an Element from a Linked Queue
Algorithm to delete an element from a linked queue
Step 1: IF FRONT = NULL, then
Write “Underflow”
Go to Step 5
[END OF IF]
Step 2: SET PTR = FRONT
Step 3: FRONT = FRONT->NEXT
Step 4: FREE PTR
Step 5: END
Circular Queues
| | 7 | 18 | 14 | 36 | 45 | 21 | 99 | 72 |
0 1 2 3 4 5 6 7 8 9
Inserting an Element in a Circular Queue
90 | 49 | 7 | 18 | 14 | 36 | 45 | 21 | 99 | 72 |
front=0 1 2 3 4 5 6 7 8 rear = 9
90 | 49 | 7 | 18 | 14 | 36 | 45 | 21 | 99 | |
front=0 1 2 3 4 5 6 7 rear= 8 9
| 49 | 7 | 18 | 14 | 36 | 45 | 21 | 99 | 72 |
front=1 2 3 4 5 6 7 8 rear= 9
Algorithm to Insert an Element in a Circular Queue
Step 1: IF FRONT = 0 and Rear = MAX – 1, then
Write “OVERFLOW”
Goto Step 4
[END OF IF]
Step 2: IF FRONT = -1 and REAR = -1, then;
SET FRONT = REAR = 0
ELSE IF REAR = MAX – 1 and FRONT != 0
SET REAR = 0
ELSE
SET REAR = REAR + 1
[END OF IF]
Step 3: SET QUEUE[REAR] = VAL
Step 4: Exit
Deleting an Element from a Circular Queue
| | | | | | | | | |
0 1 2 3 4 5 6 7 8 9
Delete this element and set rear = front = -1
| | | | | | | | | 81 |
0 1 2 3 4 5 6 7 8 front=rear= 9
72 | 63 | 9 | 18 | 27 | 39 | | | | 81 |
0 1 2 3 4 rear= 5 6 7 8 front= 9
Algorithm to Delete an Element from a Circular Queue
Step 1: IF FRONT = -1, then
Write “Underflow”
Goto Step 4
[END OF IF]
Step 2: SET VAL = QUEUE[FRONT]
Step 3: IF FRONT = REAR
SET FRONT = REAR = -1
ELSE
IF FRONT = MAX -1
SET FRONT = 0
ELSE
SET FRONT = FRONT + 1
[END OF IF]
[END OF IF]
Step 4: EXIT
Deques
Deques
| | | 29 | 37 | 45 | 54 | 63 | | |
0 1 2 LEFT = 3 4 5 6 RIGHT = 7 8 9
42 | | | | | | | 63 | 27 | 18 |
RIGHT = 0 1 2 3 4 5 6 LEFT = 7 8 9
Priority Queues
Linked Representation of Priority Queues
A
1
B
2
C
3
D
3
E
4
X
A
1
B
2
C
3
F
4
D
5
E
6
X
Priority queue after insertion of a new node
Array Representation of Priority Queues
Multiple Queues
| | | | | | | | | |
Queue A Queue B
0 1 2 3 4 ………………………………. n-4 n-3 n-2 n-1
Applications of Queues