Linear Data Structures
Linear Data Structures
Reference Books
1. Data Structures with C, Seymour Lipschutz
Elements 123 55 90 12
Index 0 1 2 3
6. An array is a suitable structure when a lot of searching and retrieval are required.
1/22/2020 PREPARED BY TASNEEA HOSSAIN 4
Operations on Linear Data Structure
a) Traversal : processing each element in the list
b) Search: finding the location of the element with a given value or the record
with a given key
c) Insertion: adding a new element to the list
d) Deletion: removing an element from the list
e) Sorting: arranging the elements in some type of order
f) Merging: combining two lists into a single list
Binary search works by halving the array into two groups until the element is
found.
Example:
◦ {2, 1, 5, 3, 2} {1, 2, 2, 3, 5} ascending order
◦ {2, 1, 5, 3, 2} {5, 3, 2, 2, 1} descending order
The technique makes several passes through the array. On each pass successive
pairs of elements are compared. If the pair is in increasing order (or equal) the
pair is unchanged. If a pair is in descending order, their values are swapped in
the array.
Underlined pairs show the comparisons. For each pass there are size-1
comparisons.
Total number of comparisons = (size-1)2
m top
t top t t top
w w w
f f f
K K K
stack : 1 5 2
top = 2
If the top exceeds the size of the stack then the stack overflows.
0 1 2 3 4 5 6 7
After insertion: 15 18 20 90
Rear = 3
Front =0
0 1 2 3 4 5 6 7
After deletion: 18 20 90
Front =1 Rear = 3
rear front
= 1 = 5
Elements were added to this queue in the order 11, 22, 33, 44, 55, and will be removed in the
same order
Use: front = (front + 1) % myQueue.length;
and: rear = (rear + 1) % myQueue.length;