You are on page 1of 33

Linear Data Structures

Reference Books
1. Data Structures with C, Seymour Lipschutz

2. Introduction with Algorithms, Thomas H. Cormen, Charles E. Leiserson,


Ronald L. Rivest, Clifford Stein

1/22/2020 PREPARED BY TASNEEA HOSSAIN 2


Array
In arrays, the linear relationship between elements is represented by means of
sequential memory locations.
width is 4 bytes

Actual Address In The 1000 1004 1008 1012


Memory

Elements 123 55 90 12

Index 0 1 2 3

1/22/2020 PREPARED BY TASNEEA HOSSAIN 3


Properties of Arrays:

1. The components of an array are all of the same type.

2. Array is a random access data structure.

3. Array is a static data structure.

4. Access time for an array element is constant, i. e O(1)

5. An array is a suitable structure when a small number of insertions and deletions


are required.

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

1/22/2020 PREPARED BY TASNEEA HOSSAIN 5


Array Declaration
Declaration consists of the following information:
Name of the array
Type of array
Dimension of the array
Declaration of an array in C
int arr[100];
float f[10];

1/22/2020 PREPARED BY TASNEEA HOSSAIN 6


Initialization
Initialization is the process of assigning initial values. Typically declaration and
initialization are combined.
Examples:
int arr[4]={1,3,14,0};
char name[4]={‘n’, ‘a’, ‘m’, ‘e’};

1/22/2020 PREPARED BY TASNEEA HOSSAIN 7


Insertion and Deletion
Inserting an element at the end of the array can be easily done provided the
memory space allocated for the array is large enough to accommodate the
additional element.

 However, if we need to insert an element in the middle of the array then on


average, half of the elements would have to be moved to new locations in order
to accommodate the new element and keep the order of the other elements.

In case of deletion also, the same problem would arise.

1/22/2020 PREPARED BY TASNEEA HOSSAIN 8


Deletion from Array
DELETE(ARR, N, K, ITEM)
K is a positive integer such that K<=N. This algorithm deletes the Kth element from
ARR.
1. Set ITEM:=ARR[K]
2. Repeat for J = K to N-1
[MOVE J+1 st ELEMENT TO LOWER INDICES] Set ARR[J] := ARR[J+1]
[End of loop]
3. [Reset the number N of elements in ARR] Set N:= N -1
4. Exit

1/22/2020 PREPARED BY TASNEEA HOSSAIN 9


Insertion in Array
INSERT(ARR, N, K, ITEM)
1. [Initialize the counter] Set J:=N
2. Repeat steps 3 and 4 while J>=K
3. [Move Jth element to higher indices] Set ARR[J+1]:=ARR[J]
4. [Decrease counter] Set J:=J-1
[End of step 2 loop]
5. [Insert element] Set ARR[K] := ITEM
6.[Reset N]. Set N:=N-1
7. Exit
1/22/2020 PREPARED BY TASNEEA HOSSAIN 10
Searching
The process of finding a particular element in an array is called searching. There
are two popular searching techniques:
Linear Search
Binary Search
The linear search compares each array element with the search key.

1/22/2020 PREPARED BY TASNEEA HOSSAIN 11


Linear Search
If the search key is a member of the array, typically the location of the search
key is reported to indicate the presence of the search key in the array.

Otherwise, a sentinel value is reported to indicate the absence of the search


key in the array.

1/22/2020 PREPARED BY TASNEEA HOSSAIN 12


Binary Search
The prerequisite for binary search is a sorted array.

Binary search works by halving the array into two groups until the element is
found.

1/22/2020 PREPARED BY TASNEEA HOSSAIN 13


Binary Search
int BinarySearch (int A[], int skey){
int low=0, high=SIZE−1, middle;
while(low <= high){
middle = (low+high)/2;
if (skey == A[middle])
return middle;
else if(skey <A[middle])
high = middle − 1;
else
low = middle + 1;
}
return −1;

1/22/2020 PREPARED BY TASNEEA HOSSAIN 14


Sorting
Sorting an array is the ordering the array elements in
◦ ascending (increasing: from min to max), or
◦ descending (decreasing: from max to min) order.

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

1/22/2020 PREPARED BY TASNEEA HOSSAIN 15


Bubble Sort
Smaller values in the list gradually “bubble” their way upward to the top of the
array.

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.

1/22/2020 PREPARED BY TASNEEA HOSSAIN 16


Bubble Sort

 Underlined pairs show the comparisons. For each pass there are size-1
comparisons.
 Total number of comparisons = (size-1)2

1/22/2020 PREPARED BY TASNEEA HOSSAIN 17


Bubble Sort: C Code
/* This program sorts the array elements in the ascending order*/
#include <stdio.h>
#define SIZE 5
void BubbleSort(int [ ]);
int main() {
int a[SIZE]= {2, 1, 5, 3, 2};
int i;
BubbleSort(a);

1/22/2020 PREPARED BY TASNEEA HOSSAIN 18


Bubble Sort: C Code

printf("\n\nThe elements of the array after sorting\n");


for (i=0; i< SIZE; i++)
printf("%d ", a[i]);
return 0;
}
}

1/22/2020 PREPARED BY TASNEEA HOSSAIN 19


Bubble Sort : C Code
void BubbleSort(int A[ ]) {
int i, pass, temp;
for (pass=1; pass < SIZE; pass++)
for (i=0; i < SIZE-1; i++)
if(A[i] > A[i+1]){
temp = A[i];
A[i] = A[i+1];
A[i+1] = temp;

1/22/2020 PREPARED BY TASNEEA HOSSAIN 20


Arrays
Advantage:
Enables fast access to each element
Disadvantage:
Insertion in the middle of the array takes time proportional to the size of the
array.
An array has a fixed length that can’t be changed. If we want to add items to
the list, but the array is full, we have to allocate a whole new array and move all
the items from the old array to the new one.

1/22/2020 PREPARED BY TASNEEA HOSSAIN 21


Stack
A STACK IS A LIFO(Last In, First Out) data structure.
Elements are removed from the stack in reverse order from the way they were
inserted. Element
is pushed
Element
is
popped

m top
t top t t top
w w w
f f f
K K K

1/22/2020 PREPARED BY TASNEEA HOSSAIN 22


Application of stacks
Undo sequence in text editor
Page visited history in web browser

1/22/2020 PREPARED BY TASNEEA HOSSAIN 23


Array implementation of Stack
To implement a stack, items are inserted and removed at the same end (called
the top).
To use an array to implement a stack, an array along with an integer will be
required.

The integer tells us the number of elements in the stack.


And which location is the current top of the stack.

1/22/2020 PREPARED BY TASNEEA HOSSAIN 24


Push and Pop
An empty stack is represented by top = -1.
To add(push) an element
increment top and put the element in that position.
To remove(pop) an element
Get the element from top position
Decrement top

1/22/2020 PREPARED BY TASNEEA HOSSAIN 25


Push and Pop

stack : 1 5 2
top = 2

After pushing the element 4 :


stack : 1 5 2 4
top = 3

After popping the element :


1 5 2 4
stack :
top = 2

1/22/2020 PREPARED BY TASNEEA HOSSAIN 26


Underflow and Overflow
When we try to pop an empty stack we say that the stack underflows.

If the top exceeds the size of the stack then the stack overflows.

1/22/2020 PREPARED BY TASNEEA HOSSAIN 27


Queue
A Queue is an ordered collection of items from which items may be removed at
one end (called the front of the queue) and inserted at the other end( rear of
the queue).
D rear
C rear C
B B
B rear front
A A
A rear A front front
front

Here, the operations are : enqueue (insert) and dequeue(delete)

1/22/2020 PREPARED BY TASNEEA HOSSAIN 28


Application of Queue
Waiting in line

Using shared resources(e.g printer)

Keyboard input buffer

1/22/2020 PREPARED BY TASNEEA HOSSAIN 29


Array Implementation of Queues
0 1 2 3 4 5 6 7
15 18 20
Queue:
Front =0 Rear = 2

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

1/22/2020 PREPARED BY TASNEEA HOSSAIN 30


Does this array implementation display any problem?

Notice how the elements crawl towards the right side.


This will create a problem after a while.

1/22/2020 PREPARED BY TASNEEA HOSSAIN 31


Solution

Using circular array


We can treat the array holding the queue elements as circular
( joined at the ends)

1/22/2020 PREPARED BY TASNEEA HOSSAIN 32


0 1 2 3 4
5 6 7
Queue: 44 55 11 22 33

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;

1/22/2020 PREPARED BY TASNEEA HOSSAIN 33

You might also like