information technology department of linear data

42
Department of Information Technology Tejas Chauhan Linear Data Structures & Their Representation Unit#2 Data Structure 01CE0301 / 3130702

Upload: others

Post on 04-Apr-2022

1 views

Category:

Documents


0 download

TRANSCRIPT

Department of Information Technology

Tejas Chauhan

Linear Data Structures & Their RepresentationUnit#2

Data Structure01CE0301 / 3130702

Department of Information Technology

Unit#2: Linear Data Structures & their representation

Highlights● Array● Stack● Queue● Linked List

2

Department of Information Technology

Unit#2: Linear Data Structures & their representation

Highlights● Array● Stack● Queue● Linked List

3

● Representation of arrays ● Sparse matrix and its

representation ● Storage Structures for arrays ● Applications of arrays

Department of Information Technology

Unit#2: Linear Data Structures & their representation

Arrays

● Structures of related data items● Static entity – same size throughout program

● Array● Group of consecutive memory locations● Same name and type

● To declare an array, specify● Array name● Array size

● To refer to an element, specify● Array name● Position number of element

4

Department of Information Technology

Unit#2: Linear Data Structures & their representation

Arrays

● int marks[5];

● Array elements are like normal variables● c[ 0 ] = 3;● printf( "%d", c[ 0 ] );

● Perform operations in subscript. If x equals 3● c[ 5 - 2 ] == c[ 3 ] == c[ x ]

5

0 1 2 3 4

Array

Index

marks[0] marks[1] marks[2] marks[3] marks[4]Elements

Department of Information Technology

Unit#2: Linear Data Structures & their representation

Declaring Arrays

● When declaring arrays, specify ● Name● Type of array● Number of elements

● arrayType arrayName[ numberOfElements ];

● Examples:● int c[ 10 ];

● float myArray[ 3284 ];

● Declaring multiple arrays of same type● Format similar to regular variables

● Example: int b[ 100 ], x[ 27 ];

6

Department of Information Technology

Unit#2: Linear Data Structures & their representation

Examples Using Arrays

● Initializers● int n[ 5 ] = { 1, 2, 3, 4, 5 };

● If not enough initializers, rightmost elements become 0● int n[ 5 ] = { 0 } //All elements 0

● If size omitted, initializers determine it● int n[ ] = { 1, 2, 3, 4, 5 };● 5 initializers, therefore 5 element array

7

Department of Information Technology

Unit#2: Linear Data Structures & their representation

Passing Arrays to Functions

● Passing arrays● To pass an array argument to a function, specify the

name of the array without any brackets● int myArray[ 24 ];● myFunction( myArray);

8

Department of Information Technology

Unit#2: Linear Data Structures & their representation

Passing Arrays to Functions

● Arrays passed as call-by-reference● Name of array is address of first element● Function knows where the array is stored● Modifies original memory locations

9

Department of Information Technology

Unit#2: Linear Data Structures & their representation

Passing Arrays to Functions

● Function prototype● void modifyArray( int b[], int arraySize );● Parameter names optional in prototype

● int b[] could be written int []

● int arraySize could be simply int

10

Department of Information Technology

Unit#2: Linear Data Structures & their representation

Operations on Arrays

● Traversal● Searching● Insertion● Deletion● Merging

11

Department of Information Technology

Unit#2: Linear Data Structures & their representation

● Traversing array element means accessing each and every element of array for specific purpose.

● Let ‘A’ is an array of homogeneous data elements● Traversing can include printing of elements,

● Counting number of elements, or

● Performing any process on these elements.

Operations on Arrays :Traversal

12

● Traversal ● Searching ● Insertion ● Deletion ● Merging

Department of Information Technology

Unit#2: Linear Data Structures & their representation

● Algorithm for array traversal:

STEP1: [Initialization] Set Itr = lower_bound

STEP2: Repeat steps 3 to 4 while Itr <= upper_bound

STEP3: Apply Process to A[Itr]

STEP4: Set Itr = Itr + 1

[End of Loop]

STEP5: Exit

Operations on Arrays :Traversal

13

● Traversal ● Searching ● Insertion ● Deletion ● Merging

Department of Information Technology

Unit#2: Linear Data Structures & their representation

● Exercise:

Assume that there is an array Marks[] such that the index of the array specifies roll number and value represents marks obtained. Ex. Marks[4] = 78.

1. Find the total number of students who have secured 80 or more marks.

2. Print the roll number and marks of all students who have got distinction.

Operations on Arrays :Traversal

14

● Traversal ● Searching ● Insertion ● Deletion ● Merging

Department of Information Technology

Unit#2: Linear Data Structures & their representation

● Searching means to find whether a particular value is present in the array or not.

● Two popular methods:● Linear Search● Binary Search

● Which one should I use? – Depends on the values.

● Both methods will be covered in Unit#5

Operations on Arrays :Searching

15

● Traversal ● Searching ● Insertion ● Deletion ● Merging

Department of Information Technology

Unit#2: Linear Data Structures & their representation

● Inserting an element in the array means adding a new data element in an already existing array.

● Add 20 in the array.

Operations on Arrays :Insertion

16

11 12 13

0 1 2 3 4

Array

Index

marks[0] marks[1] marks[2] marks[3] marks[4]Elements

● Traversal ● Searching ● Insertion ● Deletion ● Merging

Department of Information Technology

Unit#2: Linear Data Structures & their representation

● Insert an Element at the End:● If the element has to be inserted at the end, then

task is simple. ● Simply, increment upper_bound by 1 and assign the

values.● If an array is declared to contain 10 elements, but

currently it has only 8 elements, then we can add two more elements.

● But if it already has 10 elements, then not possible.

Operations on Arrays :Insertion

17

● Traversal ● Searching ● Insertion ● Deletion ● Merging

Department of Information Technology

Unit#2: Linear Data Structures & their representation

● Insert an Element at the End:

Algorithm:

STEP1: Set upper_bound= upper_bound + 1

STEP2: Set A[upper_bound] = VALUE

STEP3: Exit

Operations on Arrays :Insertion

18

● Traversal ● Searching ● Insertion ● Deletion ● Merging

Department of Information Technology

Unit#2: Linear Data Structures & their representation

● Insert an Element in Middle/Start:● The algorithm will be declared as

INSERT(A, N, POS, VAL)● A, the array in which element has to be inserted● N, the last offset in the array● POS, the position at which element has to be inserted● VAL, the value that has to be inserted

Operations on Arrays :Insertion

19

● Traversal ● Searching ● Insertion ● Deletion ● Merging

Department of Information Technology

Unit#2: Linear Data Structures & their representation

● INSERT(A, 4, 2, 15)

Operations on Arrays :Insertion

20

18 17 19 14

0 1 2 3 4 5

Array

Index

marks[0] marks[1] marks[2] marks[3] marks[4] marks[5]Elements

18 17 19 14

18 17 19 14

18 17 19 14

18 15 17 19 14

● Traversal ● Searching ● Insertion ● Deletion ● Merging

Department of Information Technology

Unit#2: Linear Data Structures & their representation

● Insert an Element in Middle/Start:

Algorithm: INSERT(A, N, POS, VAL)STEP1: [Initialization] Set I = NSTEP2: SET N = N + 1STEP3: Repeat Steps 4 and 5 while I >= POSSTEP4: SET A[I+1] = A[I]STEP5: SET I = I - 1

[End of Loop]STEP6: SET A[POS] = VALSTEP7: Exit

Operations on Arrays :Insertion

21

● Traversal ● Searching ● Insertion ● Deletion ● Merging

Department of Information Technology

Unit#2: Linear Data Structures & their representation

● Deleting an element from an array means removing a data element from an already existing array.

● Delete 13 from the array.

Operations on Arrays :Deletion

22

11 12 13 14 15

0 1 2 3 4

Array

Index

marks[0] marks[1] marks[2] marks[3] marks[4]Elements

● Traversal ● Searching ● Insertion ● Deletion ● Merging

Department of Information Technology

Unit#2: Linear Data Structures & their representation

● Delete an Element from End:● If the element has to be deleted from the end of the

array, then quite simple. ● Subtract 1 from upper_bound.

● Algorithm:

STEP1: Set upper_bound= upper_bound - 1

STEP2: Exit

Operations on Arrays :Deletion

23

● Traversal ● Searching ● Insertion ● Deletion ● Merging

Department of Information Technology

Unit#2: Linear Data Structures & their representation

● Delete an Element from Middle:● The algorithm will be declared as

DELETE(A, N, POS)● A, the array in which element has to be inserted

● N, last offset in the array

● POS, the position from which element has to be deleted

Operations on Arrays :Deletion

24

● Traversal ● Searching ● Insertion ● Deletion ● Merging

Department of Information Technology

Unit#2: Linear Data Structures & their representation

● DELETE(A, 5, 2)

Operations on Arrays :Deletion

25

18 17 19 14 16

0 1 2 3 4 5

Array

Index

marks[0] marks[1] marks[2] marks[3] marks[4] marks[5]Elements

● Traversal ● Searching ● Insertion ● Deletion ● Merging

18 19 14 16

18 19 14 16

18 19 14 16

18 19 14 16

Department of Information Technology

Unit#2: Linear Data Structures & their representation

● Delete an Element from Middle:

Algorithm: DELETE(A, N, POS)STEP1: [Initialization] Set I = POSSTEP2: Repeat Steps 3 and 4 while I <= N - 1STEP3: SET A[I] = A[I + 1]STEP4: SET I = I + 1

[End of Loop]STEP5: SET N = N - 1STEP6: Exit

Operations on Arrays :Deletion

26

● Traversal ● Searching ● Insertion ● Deletion ● Merging

Department of Information Technology

Unit#2: Linear Data Structures & their representation

● Merging two arrays in a third array:

● Unsorted arrays:● First copying contents of first array into the third array● And then copying the contents of the second array

into third array.Operations on Arrays :Merging

27

● Traversal ● Searching ● Insertion ● Deletion ● Merging

34 56 84 52 74 42 76 46 81 62

Array1 Array2

34 56 84 52 74 42 76 46 81 62

Array3

Department of Information Technology

Unit#2: Linear Data Structures & their representation

● Merging two arrays in a third array:

● Sorted arrays:● Compare front elements from both arrays and bring

appropriate element to the third array.Operations on Arrays :Merging

28

● Traversal ● Searching ● Insertion ● Deletion ● Merging

34 52 56 74 84 42 46 62 76 81

Array1 Array2

34 42 46 52 56 62 74 76 81 84

Array3

Department of Information Technology

Unit#2: Linear Data Structures & their representation

TwoDimensional Arrays

● They are used to store information that we normally represent in table form.

● Two-dimensional arrays, like one-dimensional arrays, are homogeneous.

● Examples:● A seating plan for a room (organized by rows and

columns),● A monthly budget (organized by category and

month),● A grade book where rows might correspond to

individual students and columns to student scores.

29

Department of Information Technology

Unit#2: Linear Data Structures & their representation

TwoDimensional Arrays

● Two-Dimensional Array Initialization:

int A[3][4] = { {8, 2, 6, 5}, //row 0

{6, 3, 1 ,0}, //row 1

{8, 7, 9, 6} }; //row 2

30

8 2 6 5

6 3 1 0

8 7 9 6

Array A: 0

1

2

0 1 2 3

Department of Information Technology

Unit#2: Linear Data Structures & their representation

Operations on 2D Array

● Transpose● Sum● Difference● Product

31

Department of Information Technology

Unit#2: Linear Data Structures & their representation

Array Element Address Calculation

● If Array A[4][4] has a base address of 5000, then calculate the address of element A[3][2].

32

00 01 02 03

10 11 12 13

20 21 22 23

30 31 32 33

00 01 02 03 10 11 12 13 ...

5000

5002

5004

5006

Address of A[i][j] = Base_Address + (((i-1)*n)+(j-1))*size_of(element)

Department of Information Technology

Unit#2: Linear Data Structures & their representation

Sparse matrix

● Sparse matrix is a matrix which has many elements with a value zero.

● It is beneficial to use specialized algorithms and data structures that take advantage of the sparse structure of the matrix. – Utilize memory.

33

Department of Information Technology

Unit#2: Linear Data Structures & their representation

Sparse matrix

● Lower-triangular Sparse matrix:● All elements above the main diagonal have a value

zero.

● To store this, we can use 1D array which stores only non-zero elements.

34

1

5 3

2 7 1

3 1 4 2

9 2 8 1 7

Department of Information Technology

Unit#2: Linear Data Structures & their representation

Sparse matrix

● The mapping between 1D array and 2D matrix can be done as:1. Row-wise mapping:

The content of the array A[] will be {1, 5, 3, 2, 7, 1, 3, 1, 4, 2, 9, 2, 8, 1, 7}

2. Column-wise mapping: The content of the array A[] will be {1, 5, 2, 3, 9, 3, 7, 1, 2, 1, 4, 8, 2, 1, 7}

35

1

5 3

2 7 1

3 1 4 2

9 2 8 1 7

Department of Information Technology

Unit#2: Linear Data Structures & their representation

Sparse matrix

● Upper-triangular Sparse matrix:● All elements below the main diagonal have a value

zero.

● Exercise: Row-wise, Column-wise mapping.

36

1 2 3 4 5

6 7 8 9

1 2 7

4 5

2

Department of Information Technology

Unit#2: Linear Data Structures & their representation

Sparse matrix

● Tridiagonal matrix:● All elements other than main diagonal and two

diagonal besides, have a value zero.

● To store this, we can use 1D array which stores only non-zero elements.

37

1 6

3 2 7

4 3 8

5 4 9

6 5

Department of Information Technology

Unit#2: Linear Data Structures & their representation

Sparse matrix

● The mapping between 1D array and Tridiagonal matrix can be done as:1. Diagonal-wise mapping:

The content of the array A[] will be { 1, 2, 3, 4, 5, 6, 7, 8, 9, 3, 4, 5, 6 }

38

1 6

3 2 7

4 3 8

5 4 9

6 5

Department of Information Technology

Unit#2: Linear Data Structures & their representation

Applications of Array

● Widely used to implement mathematical vectors, matrices, and other rectangular tables.

● Many databases include 1D arrays whose elements are records.

● To implement Data Structures like heaps, hash tables, stacks, queue.

39

Department of Information Technology

Unit#2: Linear Data Structures & their representation

Review● Array● Stack● Queue● Linked List

40

● Representation of arrays ● Sparse matrix and its

representation ● Storage Structures for arrays ● Applications of arrays

Department of Information Technology

Unit#2: Linear Data Structures & their representation

Up Next● Array● Stack● Queue● Linked List

41

● Stack definitions and concepts ● operations on stacks

( push, pop, peep, change) ● Polish Expressions and their

compilation● Tower of Hanoi

Department of Information Technology

Unit#2: Linear Data Structures & their representation

Thank You.

42