Linear Array

Data structure means, organizing the data by using models, in computer memory.  We can represent the data in two ways – linear data structure and non-linear data structure. A linear data structure, that represents a relationship between elements by successive memory location, is known as an array.  Whereas, a linear data structure that represents a relationship between elements, by a pointer and link, is known as a linked list.  Common non-linear data structures are a tree, graph, etc.

 Linear Array (One dimension array)

A linear array, is a list of finite numbers of elements stored in the memory.  In a linear array, we can store only homogeneous data elements.  Elements of the array form a sequence or linear list, that can have the same type of data.

 Each element of the array, is referred by an index set.  And, the total number of elements in the array list, is the length of an array.  We can accessthese elements with the help of the index set. 

For example, consider an array of employee names with the index set from 0 to 7, which contains 8 elements as shown in fig:

Data Structure

 Now, if we want to retrieve the name ‘Alex’ from the given array, we can do so by calling “employees [3]”.  It gives the element stored in that location, that is ‘Alex’, and so on. 

We can calculate the length, or a total number of elements, of a linear array (LA), by the given formula:

Length (LA)=UB-LB+1

 Here, UB refers to the upper bound, the largest index set of the given array list.  LB refers to the lower bound, smallest index set of the given array list.

 For example, we have an array of employees, in which lower bound is 153 and the upper bound is 234, so we can calculate the total number of elements in the list, by giving the formula.

 234-153+1=82

 We can perform various operations on a linear array:

  • Traversing-  processing each element of the array list.
  • Inserting- adding new elements in the array list.
  • Deleting- removing an element from the array list.
  • Sorting- arranging the elements of the list in some sorting order.
  • Merging- combining the elements of two array lists in a single array list.

Note

 Basically, an array is recommended when we have to perform operations, like retrieving the data, or reading the data.  But, for the operations like insertion and deletion, array is not recommended.

 Traversing Linear Array 

We can process each element of an array with the help of an index set.

 Consider a Linear Array(LA) list given with lower bound(LB) and upper bound(UB) that contains ‘n’ number of elements.  We can traverse the list with the given algorithm.

ALGORITHM 

1.      (initialized counter) K = LB  

  1. Repeat  

3.      while k <= UB  

  1. Process LA[k]  

5.      K = k + 1(End of loop)  

  1. Exit  

Program

1.      public class Traverse {  

  1.  public static void main(String args[]) {  

3.        String[] employee = {  

  1.    “john”,  

5.         “alex”,  

  1.    “rone”,  

7.         “mike”,  

  1.    “zeny”,  

9.         “crist”  

  1.   };  

11.   for (int k = 0; k < employee.length; k++) {  

  1.    System.out.println(employee[k]);  

13.   }  

  1.  }  

15. }  

Output

Data Structure

Inserting and Deleting in Array

 Array insertion, of an element at the end of the list, is quite simple.  We can do so by providing the unallocated space to the new element.  On the other hand, if we need to insert an element in the middle of an array, lots of internal shifting is required to insert the new element.  I.e. half of the elements must be moved downward, to the new location, to enter the new element.

 Similarly, deletion of the element from the end of the array, is quite simple.   But, if we need to delete an element from the middle of the array, then on average, half of the elements must be moved upward, in order to fill the blank space, after deleting the element from the specified location.

 Algorithm for insertion of an element

 Here, LA is a linear array with n number of elements, where K is a positive integer, i.e. K<=n.  This algorithm inserts an element item into the Kth position.

1.      (Initialized counter) J = n  

  1. Repeat step 3 & 4  

3.      while j >= k[Move downward] set LA[J + 1] = LA[J]  

  1.  [Decrement counter] set j = j – 1;  

5.      End of step 2[Insert element] set LA[k] = ITEM[Reset n] set n = n + 1  

  1. Exit  

Program

1.      public class InsertElement {  

  1.  public static void main(String args[]) {  

3.        int n = 6;  

  1.   String name[] = new String[n];  

5.        name[0] = “mike”;  

  1.   name[1] = “rick”;  

7.        name[2] = “max”;  

  1.   name[3] = “christ”;  

9.        name[4] = “larry”;  

  1.   String new_item = “jack”;  

11.   int k = 2;  

  1.   int j = n – 1;  

13.   System.out.println(“elements before inserting new elements”);  

  1.   for (int i = 0; i < n; i++) {  

15.    System.out.println(“position” + “(“ + i + “) “ + name[i]);  

  1.   }  

17.   System.out.println(“elements after inserting new elements”);  

  1.   while (j >= k) {  

19.    name[j] = name[j – 1];  

  1.    j = j – 1;  

21.   }  

  1.   name[k] = new_item;  

23.   for (int i = 0; i < n; i++) {  

  1.    System.out.println(“position” + “(“ + i + “) “ + name[i]);  

25.   }  

  1.  }  

27. }  

Output

Data Structure

Algorithm for deleting an element

 Here, LA is a linear array with ‘n’ number of elements and K is a positive integer number, such that k<=N.  This algorithm removes the element from the Kth position. 

1.      Set Item = LA[k]  

  1. Repeat  

3.      for J = k to N – 1[move J + 1 st element upward] LA[J] = LA[J + 1]  

  1.  [End of step 2]  

5.       [Reset the number of element] N = N – 1  

  1. Exit  

Program

1.      public class Deleting {  

  1.  public static void main(String args[]) {  

3.        int n = 5;  

  1.   String name[] = new String[n];  

5.        name[0] = “mike”;  

  1.   name[1] = “rick”;  

7.        name[2] = “max”;  

  1.   name[3] = “christ”;  

9.        name[4] = “larry”;  

  1.   String del_item;  

11.    int k = 2;  

  1.   System.out.println(“elements before inserting new elements”);  

13.    for (int i = 0; i < n; i++) {  

  1.    System.out.println(“position” + “(” + i + “) ” + name[i]);  

15.    }  

  1.   System.out.println(“elements after deleting the elements”);  

17.    del_item = name[k];  

  1.   for (int j = k; j < n – 1; j++) {  

19.     name[j] = name[j + 1];  

  1.   }  

21.    n = n – 1;  

  1.   for (int i = 0; i < n; i++) {  

23.     System.out.println(“position” + “(” + i + “) ” + name[i]);  

  1.   }  

25.   }  

  1. }    

Output

Data Structure

Related Posts

Comments are closed.

© 2024 Software Engineering - Theme by WPEnjoy · Powered by WordPress