Unit 4. Data Structure-15.9.2022
Unit 4. Data Structure-15.9.2022
Data Structure
In the modern world, data and its information have significance, and there are different
implementations taking place to store it in different ways. Data is simply a collection of facts and
figures, or set of values in a particular format that refers to a single set of item values. The data
items are then classified into sub-items, which is the group of items that are not called the simple
primary form of the item.
In the context of computers, the data structure is a specific way of storing and organizing data in
the computer's memory so that these data can be easily retrieved and efficiently used when
needed later. The data can be managed in many different ways, such as a logical or mathematical
model for a particular organization of data is called a data structure.
There are two types of data structure – Linear and Non-linear. In linear data structure the
elements are arranged in the linear order or in a sequence. The examples of the linear data
structure are: Arrays, Queues, Stacks, Linked lists. In non-linear data structure the data is
arranged with hierarchical relationship between different elements. The examples of the non-
linear data structure are: Tree and Graph.
In this unit you will learn all these data structures with their memory representation and
algorithms for traversal, insertion and deletion of elements.
In the above algorithm of traversing of array, initially the counter K is set to lower bound means
the lowest value of the index variable. Then the elements of the array are accessed one by one by
incrementing the counter K till the counter has reached to the upper bound. This process will be
repeated till the value of K is less than or equal to upper bound. Thus, it is possible to traverse or
access array elements.
This process can be used for reading or printing the element or making some computation on the
array elements.
The computation of complexity of this algorithm will be of the order n. If there are n elements in
the array then it is necessary to visit each element once. This will increase the complexity of the
order of n.
Insertion
Insertion means adding element into already created array. An element can be easily added at the
end if the memory space is available. To add an element in between the two elements then it is
necessary to create a space between these two elements first. To create a space at particular
position, it is required to shift the rest of elements down. The following algorithm inserts the
element “ITEM” at the Kth position.
Algorithm for insertion of element in a linear array
(Inserting into a Linear Array) INSERT (LA, N, K, ITEM)
Here LA is a linear array with N elements and K is a positive integer such that K <= N
This algorithm inserts an elements ITEM into the Kth position in LA.
1. [Initialize counter.] Set J: = N.
2. Repeat Steps 3 and 4 white J >= K.
3. [Move Jth element downward] Set LA[J + 1]:= LA[J].
4. [Decrease counter.] Set J: = J – 1.
[End of step 2 loop.]
5. [Insert element.] Set LA[K]: = ITEM.
6. [Reset N] Set N := N + 1.
7. Exit.
In the above algorithm LA is linear array and N is the number of elements in the array, ITEM is
the value of the element of array to be added at the Kth position. So, K should be always less than
or equal to N.
Initially set the counter variable J to N by equation J = N. Then move the J th element to downward
by one position to create the space in that position. This process will be repeated until the counter
Queue is linear data structure in which insertion of the elements takes place at one end and
deletion takes place at another end. The new element is inserted in the rear end or back end and
deletion of the element takes place from the front end. So this data structure is called as a First In
First Out list (FIFO). In this data structure the element that is inserted first will be deleted first,
just as the person who enters in the queue first can exit first.
Fig. 19.2 Operations on Queue
There are two common operations performed on queue that are insertion and deletion, also called
as Enqueue and Dequeue.
Enqueue – Whenever an insertion of element is performed at rear end, then this operation is
called as Enqueue.
Dequeue – Whenever a deletion of element is performed from front end, then this operation is
called as Dequeue.
Queue is a very useful data structure used to store data values temporary in the memory. Queue
is used to write algorithms or programs to achieve sharing of the resources . For example, in the
laboratory of school there is one printer shared among the number of students. Then such printer
sharing is achieved by using the queue data structure.
Representation of queue using array
A queue can be represented by using linear array. There are two variables – Front and Rear that
are used for insertions and deletions operations. Initially the value of front is -1 which indicates
the queue is empty. Consider an array of five elements with values E, M, E, R, G, E as shown in
Figure 19.4. The Front has initial value 0, which will point to the element E. The Rear has value 5
that will point to the last character E.
Insertion of an element in the