Implementation of LIST ADT Using Array Data Structures

  • Slides: 14
Download presentation
Implementation of LIST ADT Using Array Data Structures

Implementation of LIST ADT Using Array Data Structures

Array? • Array is a collection of homogeneous data elements accessed by a common

Array? • Array is a collection of homogeneous data elements accessed by a common name. • Array elements are differentiated by their position called index or subscript that are always enclosed in square brackets [ ]. Subscript must be expressed as a non-negative integer. • The first item is stored at the index ‘ 0’ and last element at N-1 if the array size if N. • Length of the array= Upper. Bound – Lower. Bound + 1. Where Upper bound is index of last element and Lower bound is index of first element. • Continuous Memory will be allocated for array size. Syntax Storagetype Datatype Arrayname[Size]; Example int X[5];

LIST ADT using Array Data Structures • Simplest way of representing the Lists ADT.

LIST ADT using Array Data Structures • Simplest way of representing the Lists ADT. • Used to store linear data of similar types. • More suitable when the number of elements going to be stored in a List is known in advance (i. e. ) List ADT is fixed in size.

Merits & Demerits ADVANTAGES OF ARRAY DATA STRUCTURE • Easy to implement. • Accessing

Merits & Demerits ADVANTAGES OF ARRAY DATA STRUCTURE • Easy to implement. • Accessing an element is faster. • Take less time for searching an element in List. • No more extra space is needed for processing the elements. DISADVANTAGES OF ARRAY DATA STRUCTURE • Memory utilization is inefficient. It is a Static data structure. So Memory storage space reserved for the elements cannot be used for any purpose. • The size of the array must be known before the compilation time. It can’t be changed after its declaration. • Insert and Delete an element becomes complex and consumes more time because it requires shifting other elements in the Array.

Insertion Operation • Insert operation is to insert one or more data elements into

Insertion Operation • Insert operation is to insert one or more data elements into an array. • Based on the requirement, a new element can be added at the beginning, end, or any given index of array. Algorithm • Step-1: Check the List is full or not. If List is Full then terminate the operation otherwise increment N by 1 and proceed the next step. • Step 2: Shift down the existing elements from (N-1)th position to kth position roll by one. • Step-3: Read the data to be inserted & assign it to the kth position of List.

Insertion - Pseudo code • Let LA be a Linear Array (unordered) with S

Insertion - Pseudo code • Let LA be a Linear Array (unordered) with S size & N elements and K is a positive integer such that K<=N. • Following is the algorithm where ITEM is inserted into the Kth position of LA 1. 2. 3. 4. 5. 6. 7. Do the step 2 to 7 if N<L Set J = N-1 Set N = N+1 Repeat steps 5 and 6 while J >= K 4 Set LA[J+1] = LA[J] Insert. Inter(1, 2) 3 3 Set J = J-1 6 Set LA[K] = ITEM 2 1 5 4 2 3 6 5 N=4

Insertion – C Program

Insertion – C Program

Deletion Operation • Deletion refers to removing an existing element from the array and

Deletion Operation • Deletion refers to removing an existing element from the array and re-organizing all elements of an array. • Based on the requirement, a element can be deleted from beginning, end, or any given index/item of array. Algorithm • Step-1: Check the List is Empty or not. If so then terminate the process, otherwise follow the next step. • Step-2: Read the position K of an element to be deleted in the List. • Step-3: Shift up the elements from (k+1) to Nth Position. • Step-4: Decrement N by 1.

Deletion - Pseudo code • Let LA be a Linear Array (unordered) with N

Deletion - Pseudo code • Let LA be a Linear Array (unordered) with N elements. • Following is the algorithm to delete Kth element of LA 1. 2. 3. 4. 5. 6. Do the step 2 to 6 if N!=0 Set J = K Repeat steps 3 and 4 while J < N Set LA[J-1] = LA[J] Set J = J+1 Delete. Inter(1) Set N = N-1 2 2 3 6 6 N=1

Deletion – C Program

Deletion – C Program

Search Operation • Can perform a search for an array element based on its

Search Operation • Can perform a search for an array element based on its value or its index. • Consider LA is a linear array with N elements and K is a positive integer such that K<=N. • Following is the algorithm to find an element with a value of ITEM using sequential search.

Search – C Program

Search – C Program

Update Operation • Update operation refers to updating an existing element from the array

Update Operation • Update operation refers to updating an existing element from the array at a given index. • Consider LA is a linear array with N elements. Following is the algorithm to update Kth element of LA. • SET LA[K-1] = ITEM

Update – C Program

Update – C Program