Data Structures�CS 19.203
By,
Dr. Mrinal Paliwal
Unit 2
Definition, single and multidimensional arrays, representation of arrays: row major order, and column major order, application of arrays, sparse matrices and
their representations. Singly linked lists, doubly linked list, circular linked list, insertion, deletion, traversal, polynomial representation and addition, generalized linked list.
Array & Linked list
ARRAYS
Array & Linked list
ARRAYS
Array & Linked list
ARRAYS TYPES
Array & Linked list
ONE DIMENSIONAL ARRAY
Array & Linked list
TWO DIMENSIONAL ARRAY
Array & Linked list
An array with only one row or column is called one-dimensional array.
It is finite collection of n number of elements of same type such that:
Syntax: Datatype Array_Name [Size];
Where,
ONE DIMENSIONAL ARRAY
Array & Linked list
ONE DIMENSIONAL ARRAY
To find the address of an element in an array the following formula is used-
Address of A[I] = B + W * (I – LB)
I = Subset of element whose address to be found, �B = Base address, �W = Storage size of one element store in any array(in byte), �LB = Lower Limit/Lower Bound of subscript
(If not specified assume zero).
Array & Linked list
Example: Given the base address of an array
A[1300…………1900] as 1020 and the size of each element
is 2 bytes in the memory, find the address of A[1700]?
Solution:
Given:�Base address B = 1020�Lower Limit/Lower Bound of subscript LB = 1300�Storage size of one element store in any array W = 2 Byte�Subset of element whose address to be found I = 1700
Formula:�Address of A[I] = B + W * (I – LB)
Solution:�Address of A[1700] = 1020 + 2 * (1700 – 1300)� = 1020 + 2 * (400)� = 1020 + 800�Address of A[1700] = 1820
Array & Linked list
Example: Given the base address of an array
A[1300…………1900] as 1020 and the size of each element
is 2 bytes in the memory, find the address of A[1700]?
Solution:
Given:�Base address B = 1020�Lower Limit/Lower Bound of subscript LB = 1300�Storage size of one element store in any array W = 2 Byte�Subset of element whose address to be found I = 1700
Formula:�Address of A[I] = B + W * (I – LB)
Solution:�Address of A[1700] = 1020 + 2 * (1700 – 1300)� = 1020 + 2 * (400)� = 1020 + 800�Address of A[1700] = 1820
Array & Linked list
BASIC OPERATIONS OF ARRAYS
TRAVERSING IN ARRAYS
Traversing: It is used to access each data item exactly once so that it can be processed.
E.g. We have linear array A as below:
Here we will start from beginning and will go till last element and during this process we will access value of each element exactly once as below:
A [1] = 10 A [2] = 20 A [3] = 30
A [4] = 40 A [5] = 50
INSERTION IN ARRAYS
Algorithm:
A[UB]=DATA
4. STOP
INSERTION IN ARRAYS
II. Insertion at the beginning of array
Algorithm:
K=K-1
INSERTION IN ARRAYS
III. Insertion at a given location of array
Algorithm:
K=K-1
INSERTION IN ARRAYS
IV. Insertion in the sorted list of array
Algorithm:
K=UB
7. Repeat Step(8) while K>=LOC
8. A[K+1]=A[K]
K=K-1
9. A[LOC]=DATA
10. STOP
INSERTION IN ARRAYS
Algorithm:
INSERTION IN ARRAYS
II. Deletion from the beginning
Algorithm:
K=K+1
INSERTION IN ARRAYS
III. Deletion of a given element
Algorithm:
K=K+1
LIMITATIONS IN ARRAYS
2D ARRAYS
How to declare 2D Array
Initializing 2D Arrays
The syntax to declare and initialize the 2D array is given as follows.
int arr[2][2] = {0,1,2,3};
The number of elements that can be present in a 2D array will always be equal to (number of rows * number of columns).
Initializing 2D Arrays
Calculating the Address of the random element of a 2D array
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,
Address(a[i][j]) = B. A. + ([i-lower bound of row index] * n + [j-lower bound of column index]) * size
where, B. A. is the base address or the address of the first element of the array a[0][0] .
Example :
a[10...30, 55...75], base address of the array (BA) = 0,
size of an element = 4 bytes .
Find the location of a[15][68].
Address(a[15][68]) = 0 + ((15 - 10) x (75 - 55 + 1) + (68 - 55)) x 4
= (5 x 21 + 13) x 4
= 105 x 4
= 420 answer
Calculating the Address of the random element of a 2D array
Column 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,
Address(a[i][j]) = (([j-lower bound of column index]*m)+[i-lower bound of row index])*Size + BA
where BA is the base address of the array.
m=20+5-1=24
Example:
A [-5 ... +20][20 ... 70], BA = 1020, Size of element = 8 bytes.
Find the location of a[0][30].
Address
[A[0][30]) = ((30-20) x 24 + 5) x 8 + 1020 = 245 x 8 + 1020 = 2980 bytes
Calculating the Address of the random element of a 2D array
Applications of Array Data Structure:
Below are some applications of arrays.
Applications of Array Data Structure:
Real-Time Applications of Array:
Applications of Array Data Structure:
Applications of Array in C:
Sparse Matrices in Data Structure
Sparse Matrices in Data Structure
Sparse Matrices in Data Structure
Representation of sparse matrix
Now, let's see the representation of the sparse matrix. The non-zero elements in the sparse matrix can be stored using triplets that are rows, columns, and values. There are two ways to represent the sparse matrix that are listed as follows -
Sparse Matrices in Data Structure
Array representation of the sparse matrix
Representing a sparse matrix by a 2D array leads to the wastage of lots of memory. This is because zeroes in the matrix are of no use, so storing zeroes with non-zero elements is wastage of memory. To avoid such wastage, we can store only non-zero elements. If we store only non-zero elements, it reduces the traversal time and the storage space.
In 2D array representation of sparse matrix, there are three fields used that are named as -
Sparse Matrices in Data Structure
Example -
Let's understand the array representation of sparse matrix with the help of the example given below -
Consider the sparse matrix -
In the figure, we can observe a 5x4 sparse matrix containing 7 non-zero elements and 13 zero elements. The above matrix occupies 5x4 = 20 memory space. Increasing the size of matrix will increase the wastage space.
Sparse Matrices in Data Structure
The tabular representation of the above matrix is given below -
Sparse Matrices in Data Structure
Linked List representation of the sparse matrix
In a linked list representation, the linked list data structure is used to represent the sparse matrix. The advantage of using a linked list to represent the sparse matrix is that the complexity of inserting or deleting a node in a linked list is lesser than the array.
Unlike the array representation, a node in the linked list representation consists of four fields. The four fields of the linked list are given as follows -
Sparse Matrices in Data Structure
The node structure of the linked list representation of the sparse matrix is shown in the below image -
Sparse Matrices in Data Structure
Example -
Let's understand the linked list representation of sparse matrix with the help of the example given below -
Consider the sparse matrix -
In the figure, we can observe a 4x4 sparse matrix containing 5 non-zero elements and 11 zero elements. Above matrix occupies 4x4 = 16 memory space. Increasing the size of matrix will increase the wastage space.
Multidimensional Array in Data Structure
Multidimensional Arrays
A multidimensional array in MATLAB is an array with more than two dimensions. In a matrix, the two dimensions are represented by rows and columns.
Each element is defined by two subscripts, the row index and the column index. Multidimensional arrays are an extension of 2-D matrices and use additional subscripts for indexing. A 3-D array, for example, uses three subscripts. The first two are just like a matrix, but the third dimension represents pages or sheets of elements.
LINKED LIST
A linked list is a sequence of data structures, which are connected together via links.
Linked List is a sequence of links which contains items. Each link contains a connection to another link. Linked list is the second most-used data structure after array. Following are the important terms to understand the concept of Linked List.
LINKED LIST
Types of Linked List
Following are the various types of linked list.
Basic Operations
Following are the basic operations supported by a list.
SINGLY LINKED LIST
A singly linked list is a type of linked list that is unidirectional, that is, it can be traversed in only one direction from head to the last node (tail).
Each element in a linked list is called a node. A single node contains data and a pointer to the next node which helps in maintaining the structure of the list.
SINGLY LINKED LIST
1. Creation of a Singly Linked List
Let us assume that PTR, CPT, TPT are user defined working pointer variable which contain the address of next node.
Step 1: PTR=AVAIL
AVAIL=LINK(AVAIL)
Read INFO(PTR)
First=PTR
Step 2: CH=‘Y’
Step 3: Repeat Step 4 to 5 while CH==‘Y’
Step 4: CPT=AVAIL
AVAIL=LINK(AVAIL)
Read INFO(CPT)
LINK(PTR)=CPT
PTR=CPT
Step 5: Read Choice <Y/N> into CH for more nodes
Step 6: LINK(PTR)=Null
Step 7: Stop
SINGLY LINKED LIST
Creation of a Singly Linked List
Let us assume that PTR, CPT, TPT are user defined working pointer variable which contain the address of next node.
Step 1: PTR=AVAIL
AVAIL=LINK(AVAIL)
Read INFO(PTR)
First=PTR
Step 2: CH=‘Y’
Step 3: Repeat Step 4 to 5 while CH==‘Y’
Step 4: CPT=AVAIL
AVAIL=LINK(AVAIL)
Read INFO(CPT)
LINK(PTR)=CPT
PTR=CPT
Step 5: Read Choice <Y/N> into CH for more nodes
Step 6: LINK(PTR)=Null
Step 7: Stop
SINGLY LINKED LIST
Node Creation
struct node
{
int data;
struct node *next;
};
struct node *head, *ptr;
ptr = (struct node *)malloc(sizeof(struct node *));
SINGLY LINKED LIST
Traversing of a singly Linked List
Algorithm:
INSERTION IN SINGLY LINKED LIST
I. Insertion at the beginning
Algorithm:
Step 1: If AVAIL==NULL then write link list is Overflow & Stop
Step 2: PTR=AVAIL
AVAIL=LINK(AVAIL)
Read INFO(PTR)
Step 3: LINK(PTR)=FIRST
FIRST=PTR
Step 4: STOP
INSERTION IN SINGLY LINKED LIST
II. Insertion at the End
Algorithm:
Step 1: If AVAIL==NULL then write link list is Overflow & Stop
Step 2: PTR=AVAIL
AVAIL=LINK(AVAIL)
Read INFO(PTR)
Step 3: CPT=FIRST
Step 4: Repeat Step 5 while LINK(CPT)!=NULL
Step 5: CPT=LINK(CPT)
Step 6: LINK(CPT)=PTR
LINK(PTR)=NULL
Step 7: Stop
INSERTION IN SINGLY LINKED LIST
III. Insertion at a given location
Algorithm:
Step 1: If AVAIL==NULL then write link list is Overflow & Stop
Step 2: Read DATA as information of node after which insertion will be made
Step 3: PTR=AVAIL
AVAIL=LINK(AVAIL)
Read INFO(PTR)
Step 4: CPT=FIRST
Step 5: Repeat Step 6 while INFO(CPT)!=DATA
Step 6: CPT=LINK(CPT)
Step 7: LINK(PTR)=LINK(CPT)
LINK(CPT)=PTR
Step 8: Stop
DELETION IN SINGLY LINKED LIST
I. Deletion from the beginning
Algorithm:
Step 1: If FIRST==NULL then write Underflow & Stop
Step 2: PTR=FIRST
FIRST=LINK(PTR)
Step 3: LINK(PTR)=AVAIL
AVAIL=PTR
Step 4: STOP
DELETION IN SINGLY LINKED LIST
II. Deletion from the End
Algorithm:
Step 1: If FIRST==NULL then write Underflow & Stop
Step 2: PTR=FIRST
Step 3: Repeat Step 4 while LINK(PTR)!=NULL
Step 4: CPT=PTR
PTR=LINK(PTR)
Step 5: LINK(CPT)=NULL
Step 6: LINK(PTR)=AVAIL
AVAIL=PTR
Step 7: Stop
DELETION IN SINGLY LINKED LIST
III. Deletion of given INFO Node
Algorithm:
Step 1: If FIRST==NULL then write Underflow & Stop
Step 2: Read DATA as information of node to be deleted
Step 3: PTR=FIRST
Step 4: Repeat Step 5 while INFO(PTR)!=DATA
Step 5: CPT=PTR
PTR=LINK(PTR)
Step 6: LINK(CPT)=LINK(PTR)
Step 7: LINK(PTR)=AVAIL
AVAIL=PTR
Step 8: Stop
CIRCULAR LINKED LIST
Circular linked list is a linked list where all nodes are connected to form a circle. There is no NULL at the end. A circular linked list can be a singly circular linked list or doubly circular linked list.
CREATION OF CIRCULAR LINKED LIST
1. PTR🡨AVAIL
AVAIL 🡨 LINK(AVAIL)
READ INFO(PTR)
FIRST 🡨 PTR
CH=’Y’
2. Repeat Step 3 to 4 while CH=’Y’
3. CPT 🡨 AVAIL
AVAIL 🡨 LINK(AVAIL)
READ INFO(CPT)
LINK(PTR) 🡨 CPT
PTR 🡨 CPT
4. Press Y or N for more nodes
TRAVERSING OF CIRCULAR LINKED LIST
Process INFO(PTR)
PTR 🡨 LINK(PTR)
PTR 🡨 LINK(PTR)
4. STOP
INSERTION IN CIRCULAR LINKED LIST
Algorithm
2. PTR🡨AVAIL
AVAIL 🡨 LINK(AVAIL)
READ INFO(PTR)
FIRST🡨PTR
LINK(CPT)🡨FIRST
7. Stop
INSERTION IN CIRCULAR LINKED LIST
II. At the End
Algorithm
2. PTR🡨AVAIL
AVAIL 🡨 LINK(AVAIL)
READ INFO(PTR)
DELETION IN CIRCULAR LINKED LIST
Algorithm
2 CPT 🡨 FIRST
FIRST🡨LINK(PTR)
LINK(CPT)🡨FIRST
6. LINK(PTR)🡨AVAIL
AVAIL🡨PTR
7. Stop
DELETION IN CIRCULAR LINKED LIST
II. From the End
Algorithm
2 CPT 🡨 FIRST
CPT 🡨 LINK(CPT)
5. LINK(PTR)🡨FIRST
6. LINK(CPT)🡨AVAIL
AVAIL🡨CPT
7. Stop
DOUBLY LINKED LIST
Doubly linked list
Doubly linked list is a complex type of linked list in which a node contains a pointer to the previous as well as the next node in the sequence. Therefore, in a doubly linked list, a node consists of three parts: node data, pointer to the next node in sequence (next pointer) , pointer to the previous node (previous pointer). A sample node in a doubly linked list is shown in the figure.
�
CREATION OF DOUBLY LINKED LIST
Algorithm
AVAIL🡨RPT(AVAIL)
READ INFO(PTR)
LPT(PTR)🡨NULL
FIRST🡨PTR
4. (a) CPT🡨AVAIL
AVAIL🡨RPT(AVAIL)
READ INFO(CPT)
(b) RPT(PTR)🡨CPT
LPT(CPT)🡨PTR
PTR🡨CPT
FORWARD TRAVERSING OF DOUBLY LINKED LIST
Algorithm
BACKWARD TRAVERSING OF DOUBLY LINKED LIST
Algorithm
INSETION IN DOUBLY LINKED LIST
Algorithm
AVAIL🡨RPT(AVAIL)
READ INFO(PTR)
LPT(FIRST)🡨PTR
LPT(PTR)🡨NULL
FIRST🡨PTR
4. STOP
INSETION IN DOUBLY LINKED LIST
II. At the End
Algorithm
AVAIL🡨RPT(AVAIL)
READ INFO(PTR)
LPT(PTR)🡨CPT
RPT(PTR)🡨NULL
7. STOP
INSETION IN DOUBLY LINKED LIST
III. After a Given Node
Algorithm
AVAIL🡨RPT(AVAIL)
READ INFO(PTR)
RPT(CPT)🡨PTR
LPT(PTR)🡨CPT
RPT(PTR)🡨TPT
LPT(TPT)🡨PTR
8. STOP
DELETION IN DOUBLY LINKED LIST
I. From the Beginning
Algorithm
FIRST🡨RPT(PTR)
LPT(FIRST)🡨NULL
AVAIL🡨PTR
4. STOP
DELETION IN DOUBLY LINKED LIST
II. From the End
Algorithm
RPT(CPT)🡨NULL
AVAIL🡨PTR
4. STOP
DELETION IN DOUBLY LINKED LIST
III. If Specific Node is Given
Algorithm
TPT🡨RPT(PTR)
RPT(CPT)🡨TPT
LPT(TPT)🡨CPT
7. RPT(PTR)🡨AVAIL
AVAIL🡨PTR
8. STOP
DOUBLY CIRCULAR 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.
A circular doubly linked list is shown in the following figure.
MEMORY MANAGEMENT OF CIRCULAR DOUBLY LINKED LIST
CREATION OF CIRCULAR DOUBLY LINKED LIST
AVAIL🡨RPT(AVAIL)
AVAIL🡨LPT(AVAIL)
Read INFO(PTR)
LPT(PTR)🡨PTR
RPT(PTR)🡨PTR
FIRST🡨PTR
2. STORE ‘Y’ to CH
AVAIL🡨RPT(AVAIL)
Read INFO(CPT)
(b) RPT(PTR)🡨CPT
LPT(CPT)🡨PTR
PTR🡨CPT
5. Input Choice <Y/N> for more nodes
6. RPT(PTR)🡨FIRST
LPT(FIRST)🡨PTR
7. Stop
INSERTION IN CIRCULAR DOUBLY LINKED LIST
AVAIL🡨RPT(AVAIL)
AVAIL🡨LPT(AVAIL)
Read INFO(PTR)
FIRST🡨PTR
LPT(FIRST)🡨PTR
RPT🡨TPT(PTR)
INSERTION IN CIRCULAR DOUBLY LINKED LIST
II. At the Beginning
AVAIL🡨RPT(AVAIL)
AVAIL🡨LPT(AVAIL)
Read INFO(PTR)
TPT🡨LPT(FIRST)
LPT(PTR)🡨TPT
5. RPT(PTR)🡨FIRST
LPT(FIRST)🡨PTR
DELETION IN CIRCULAR DOUBLY LINKED LIST
I. At the Beginning
PTR🡨RPT(FIRST)
CPT🡨LPT(FIRST)
3. LPT(PTR)🡨CPT
RPT(CPT)🡨PTR
4. FIRST🡨PTR
5. LPT(TPT)🡨AVAIL
RPT(TPT)🡨AVAIL
AVAIL🡨TPT
DELETION IN CIRCULAR DOUBLY LINKED LIST
II. At the End
CPT🡨LPT(PTR)
3. LPT(FIRST)🡨CPT
RPT(CPT)🡨FIRST
4. LPT(PTR)🡨AVAIL
RPT(PTR)🡨AVAIL
AVAIL🡨PTR
DELETION IN CIRCULAR DOUBLY LINKED LIST
II. At the End
CPT🡨LPT(PTR)
3. LPT(FIRST)🡨CPT
RPT(CPT)🡨FIRST
4. LPT(PTR)🡨AVAIL
RPT(PTR)🡨AVAIL
AVAIL🡨PTR
// Fibonacci Series using Recursion
#include <stdio.h>
int fib(int n)
{
if (n <= 1)
return n;
return fib(n - 1) + fib(n - 2);
}
int main()
{
int n = 9;
printf("%d", fib(n));
getchar();
return 0;
}
#include<stdio.h>
long int multiplyNumbers(int n);
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}
long int multiplyNumbers(int n) {
if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
}