0% found this document useful (0 votes)
426 views

7.5 Arrays, Records, Pointers

The document discusses linear data structures and linear arrays. It defines linear arrays as lists of homogeneous data elements that are referenced by consecutive integer indices and stored in successive memory locations. The elements can be accessed directly through their index in constant time. Operations on linear arrays include traversal, search, insertion, deletion, and sorting. Insertion and deletion may require shifting elements to fill gaps. Linear arrays are represented contiguously in memory, with the base address and element size used to calculate any element's location from its index.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
426 views

7.5 Arrays, Records, Pointers

The document discusses linear data structures and linear arrays. It defines linear arrays as lists of homogeneous data elements that are referenced by consecutive integer indices and stored in successive memory locations. The elements can be accessed directly through their index in constant time. Operations on linear arrays include traversal, search, insertion, deletion, and sorting. Insertion and deletion may require shifting elements to fill gaps. Linear arrays are represented contiguously in memory, with the base address and element size used to calculate any element's location from its index.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

7.

5 ARRAYS, RECORDS, POINTERS


INTRODUCTION:
A Data structure is said to be linear if its elements from a sequence or in other
words form a linear list.
Representation in memory:
There are two basic representation in memory. They are:
i. Have a linear relationship between the elements represented by means of
sequential memory locations [ Arrays]
ii. Have the linear relationship between the elements represented by means of
pointer or links [ Linked List]
Operation on Linear Structure:
1. Traversal: Processing each element in the list
2. Search: Finding the location of the element with a given value or the record
with a given key
3. Insertion: Adding a new element to the list
4. Deletion: Removing an elements from the list
5. Sorting: Arranging the elements in some type of order
6. Merging: Combining two list into a single list
7.5.i Linear Arrays:
• A linear array is a list of a finite number of n homogeneous data elements ( that is
data elements of the same type) such that
– The elements are of the arrays are referenced respectively by an index set
consisting of n consecutive numbers
– The elements of the arrays are stored respectively in successive memory
locations
– For example: if an array is of type “int”, it can only store integer elements and
cannot allow the elements of other types such as double, float, char etc.
• The number n of elements is called the length or size of the array.
• The index set consists of the integer 1, 2, … n
• Length or the number of data elements of the array can be obtained from the index
set by :
Length = UB – LB + 1
where
- UB is the largest index called the upper bound
- LB is the smallest index called the lower bound of the arrays
• Element of an array A may be denoted by
– Subscript notation A1, A2, , …. , An
– Parenthesis notation A(1), A(2), …. , A(n)
– Bracket notation A[1], A[2], ….. , A[n]
– The number K in A[K] is called subscript or an index and A[K] is called a
subscripted variable
Example:
• The following diagram represents an integer array that has 12 elements. The index
of the array starts with 0, so the array having 12 elements has indexes from 0 to 11.

7.5.ii Representation of Linear Arrays in Memory

 Let LA be a linear array in the memory of the


computer
 LOC(LA[K]) = address of the element LA[K] of the array LA
 The element of LA are stored in the successive memory cells
 Computer does not need to keep track of the address of every element of LA, but
need to track only the address of the first element of the array denoted by Base(LA)
called the base address of LA.
LOC(LA[K]) = Base(LA) + w(K – lower bound)
where ,
- w is the number of words per memory cell of the array LA [w is also called
size of the data type]
Example:1 Find the address for LA[6]. Each element of the array occupy 1 byte

LOC(LA[K]) = Base(LA) + w(K – lower bound)


LOC(LA[6]) = 200 + 1(6 – 1) = 205

Example:2 Find the address for LA[16].Each element of


the array occupy 2 byte

LOC(LA[K]) =
Base(LA) + w(K – lower bound)
LOC(LA[16]) = 200 + 2(16 – 1) = 230

• Given any value of K, time to calculate LOC(LA[K]) is same


• Given any subscript K one can access and locate the content of LA[K] without
scanning any other element of LA
• A collection A of data element is said to be index if any element of A called Ak can be
located and processed in time that is independent of K
3.3 Traversing Linear Arrays
• Traversing is accessing and processing (also called visiting ) each element of the
data structure exactly ones.

Algorithm: (Traversing a Linear Array) This algorithm traverses a linear array LA with
LB and UB.
1. Repeat for K = LB to UB
Apply PROCESS to LA[K]
[End of Loop]
2. Exit

3.4 Inserting and Deleting


• Insertion: Operation of adding an element to the collection A.
– Beginning Half of the element must be moved downward to new locations to
accommodate the new element and keep the order of the other
– Middle element
– End ( inserting element at the end can be easily done provided the memory
space allocated for the array is large enough to accommodate the additional
element)

Insertion is not possible without loss of data if the array is FULL


Insertion Algorithm:
INSERT (LA, N , K , ITEM) [LA is a linear array with N elements and K is a positive
integers such that K ≤ N. This algorithm insert an element ITEM into the K th position
in LA ]
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

• Deletion: Operation of removing an element from A.


– Beginning Each subsequent element must be moved one location
– Middle upward in order to “fill up” yhe array
– End (easily done )

No data item can be deleted from an empty array


Deletion Algorithm:
DELETE (LA, N , K , ITEM) [LA is a linear array with N elements and K is a positive
integers such that K ≤ N. This algorithm deletes Kth element from LA ]
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

You might also like