Unit-III.
Linear Data Structures using Sequential Organisation
/---/
Syllabus:
Concept of sequential organization, Concept of Linear data structures, arrays as ADT, Storage representation of array – Row major and Column major & their address calculation, Multidimensional arrays, Concept of ordered list.
Applications: Polynomial representation using array, Concept of Sparse Matrix, it’s usage & representation using arrays, Algorithms for sparse matrix operations like addition, simple transpose, fast transpose & multiplication.
Analysis of the algorithms used.
/---/
Concept of sequential organization :
“A process of storing data in contiguous memory locations in computer memory is called sequential data organization”.
Features:
➢ In sequential organization data gets stored in adjacent memory locations.
➢ Data gets stored linearly.
➢ Each element will be access with the help of index or address. ➢ Sequential data organization is one of the simplest form of data
storage.
➢ It is easy to understand.
➢ Its elements can get randomly accessed with the help of index or address of element.
Why sequential organization is important?
Concept of Linear data structures:
➢ A data structure is said to be linear, if data elements stored in these data structures forms a contineous sequence.
e.g. Array, Linked list, queue , stack, strings etc.
➢ Linear data structure is one in which, while traversing sequentially, from one element it can reach to only one element directly. e.g. Link List, stack,queue,array.
➢ In Linear data structure a relationship among the data elements is one to one. Such data structures are good to represent one to one relationship among data elements but difficult to represent one to many relationship. So it is clear that to represent complex data which is having one to many or many to many relationship using linear data structures. In link list each node has a link which point to another node.
➢ 2D array, though it seems to be non-linear but it is actually linear data structure. This is because memory is single dimensional. When 2D array is stored in the memory, it is stored as a single dimensional array in either row-major or column-major form. Similarly all multi-dimensional arrays are also linear, for the same reason.
➢ There are two ways of representing linear data structures in memory.
1. Sequencial Memory Organization:
One way is to have the linear relationship between the elements by means of sequential memory locations. Such linear structures are called arrays.
2. Linked Organization:
The other way is to have the linear relationship between the elements represented by means of links. Such linear data structures are called linked list.
How the linked organization is different from sequential organization? Explain different types of the link lists. [6]
Answer:
The example of sequential organization is Array & Link organization is link list.
S
r
oorganization
Sequential
Sr Link organization
1 Total amount of memory required is calculated during program compilation time and memory allocation takes place during the time
of program loading.
2 Memory allocated is of fixed size. Once allocated can not be changed . Overallocation of memory may get wasted. Under allocation of memory may becomes insufficient.
2 Memory for nodes in the Link list gets allocated during the time of program execution. So it has no fixed size. Link list is dynamic in nature. Memory can grow & shrink as an when required.
3 Insertion and deletion operations on Sequential organization is tedious work because it need to shift elements to the right or left side positions. It takes too much time.
3 To insert and delete any node in the link list is very simple task. Because here just need to manipulate the addresses of the nodes in the link lists. It takes less time.
4 Allocates static memory for elements.
4 Allocates dynamic memory for elements.
5 We can randomly access any
element in the array. 5 We can not randomly access any element or node in the link list. To access any element we have to traverse link list from starting node to the required node.
6 Array elements gets stored
in adjacent memory
locations.
6 In link list nodes gets stored in memory randomly wherever free memory is available.
Sequential Memory Organization:
A memory organization in which elements gets stored in adjacent/contiguous memory locations is called as sequential memory organization.
The best example of sequential memory organization is Array,String, structure, Stack, Queue. When we implement all these data structures using sequential memory organization it allocates adjacent memory locations to each element.
Advantages of sequential Memory organization:
2. It is easy to implement and access.
3. Elements can be access randomly e.g. array
Disadvantages of sequential Memory organization:
1. Wastage of memory: When Memory allocation takes place during the time of program compilation, we can't predict exact memory required. If we declare more memory than required, then it will waste some memory allocated.
E.g. int arr[50]; This statement will allocate total 100bytes of
contiguous memory and if we are storing only 10 integer elements
into this array, then there will be only 20bytes usage and
wastage of 80bytes of memory.
2. Insufficient memory:
3. Insertion and deletion of elements are complex because it needs to shift data right/left.
4. No reuse of memory: Sequential memory is allocated at the time of program loading, once it is allocated, it remains allocated in RAM, and can not get used for another variables in the program.
5. In some situations, sufficient memory is available till it is not allowed to store data if it is not continuous. Example- need to store array of 10 integer elements, sufficient memory is available but not continuous, then that memory can't be used for storing 10 integers.
Process to Define Abstract Data Type
:The important steps in defining ADT are as follows: i) State what data gets stored in data structure .
ii)Describe the way in which data elements are related to each other.
iii) State possible operations takesplace on data structure.
An ADT consists of two parts:
a)Value Definition Part:
It tells what value is stored in data structure and what relationship between data elements. It contains following two things:
i)Definition Clause: In definition clause we have to declare data with data type which gets stored in data structure. This clause is required compulsorily.
primary condition on data stored if required. This clause is optional.
b)Operator Definition Part: It only tells what operations/functions to be performed on data stored in data structure. It does not tell how these operations takes place.
It contains following three sections:
i)Header : Header specifies name, data type of data passed and return data type of function.
ii)Precondition: Precondition tells, is there any primary condition required. It is optional.
iii)Post condition: Final result generated.
Abstract Data Type for an Array (ADT for an Array):
We know that an array is a collection of an elements of similar data types which allocates contiguous memory locations and elements will be accessed with the help of array index. Here, to write ADT for an array, we have to give
what data to store in array?
What is the relationship of data elements with each others? What are possible operations on Array?
ADT for an Array:
abstract typedef<<integer>,integer Size> Array; Condition :
1) Array size must be interger constant greater than 0.
2) Array index must be less than Size-1 and greater than equal to 0.
Create(): Create function will create empty array of specified size.
Insert(Array , Index , Data)
Precondition : 0 <= Index < Size Postcondition : Array[Index] = Data
Integer Retrieve(Array , Index , Size)
Integer Data;
Precondition : 0 <= Index < Size Postcondition : Data = Array[Index]
Integer Search(Array , Key, Size)
Integer Data;
Postcondition : Data = Search(Array,key,Size)
if Data = 0 , Key element not found if Data = 1, key element is found
Sort(Array , Size)
Sort(Array,Size,ASC)
Array elements are sorted in specified i.e. ascending order.
OR In simple way as follows,
ADT of an Array:
struct Array
{
int Arr[SIZE];
};
Here, Arr = Array of integers containing SIZE of elements
SIZE = Total number of elements possible to store in given Array
Each element in array gets stored in contiguous memory locations.
Operations Possible on Array:
Array Create(int n): It will create empty array of size 'n'.
Value Retrieve(Array,Index): It will give value present in given array “Array” at specified index “Index”.
Void Store(Array,n):It accepts 'n' elements and store in the given array
Void Display(Array,n): It display all “n” elements from array
“Array”.
void Search(Array,value):It search specified value in given “Array”.
Storage representation of an array :
In computer memory array elements gets stored in contiguous memory locations. It is nothing but sequential storage organization. We will see how,array is represented in computer memory as follows: Array can be either – single dimensional, double dimensional or multidimensional
1-Dimensional Array:
We know that 1-D array is a collection of elements of similar data types, it get stored in adjacent memory locations and access with array index.
How single dimensional array elements gets stored in computer Memory:
The name of array gives us base address of array or address of starting element. i.e. When we refer Arr it gives us address 1000.
e.g.
printf(“%u”,Arr); This statement will display base address 1000
Say suppose we have to display element present at index 3 in given array Arr, we will write following statements.
printf(“%d”,Arr[3]);
printf(“%d”,3[Arr]);
printf(“%d”,*(Arr + 3));
main()
{
int Arr[5]={1,2,3,4,5}; int i;
clrscr();
printf("\n\n\tArray elements : "); for(i=0;i<5;i++)
{
printf(" %d ",Arr[i]); }
printf("\n\n\tArray element addresses: "); for(i=0;i<5;i++)
{
printf(" %u ",&Arr[i]); }
printf("\n\n\n\tBase address of Arr = %u",Arr); printf("\n\n\tvalue of 3[Arr] = %d",3[Arr]); printf("\n\n\tvalue of Arr[3] = %d",Arr[3]); printf("\n\n\tvalue of *(Arr+3) = %d",*(Arr+3)); printf("\n\n\tvalue of *(3+Arr) = %d",*(3+Arr)); getch();
}
Output:
Array elements : 1 2 3 4 5
Array element addresses: 65516 65518 65520 65522 65524
Base address of Arr = 65516
value of 3[Arr] = 4
value of Arr[3] = 4
value of *(Arr+3) = 4
value of *(3+Arr) = 4
2-Dimensional Array:
3-Dimensional Array: