You are on page 1of 56

1

DATA STRUCTURE
In computer science, a data structure is a way of storing data in a computer so that
it can be used efficiently. Often a carefully chosen data structure will allow a more
efficient algorithm to be used. The choice of the data structure often begins from the
choice of an abstract data structure. A well-designed data structure allows a variety of
critical operations to be performed on using as little resources, both execution time and
memory space, as possible.

TYPES OF DATA STRUCTURE


1. Primitive data structures consitute the number and the characters which are
built programs. Examples int.reals,pointer data,charaters data,logical data.
2. Non primitive data structures has two types,they are
• Linear data structure.
• Non linear data structure

Linear data structure: Data structure is said to be linear if its elements forms a
sequence or a linearlist. Example stacks, queues, linked list….

Non linear data structure: Data structure is said to be non linear if its elements does
not form a sequence.Example tree, graphs...

OPERATIONS ON DATA STRUCTURE


A data structure is created to define and process complex data. The data in a data
structure is processed using certain operations

Insertion: Adding new data items into a data structure is called insertion.
Deletion: Removing data items from a data structure is called deletion.
Searching: Finding specific data item ina data structure is called searhing.
Traversing: Accesing each record or item in a data srtructure exactly one for processing
is called traversing. It is also called visiting.
Sorting: Arranging data items in a data structure into a specific order is called sorting.
Merging: Combining two lists of data items into a single list is called merging.

ALGORITHM
An algorithm is a well-defined list of steps for solving a particular problem. The
time and space it uses are two major measures of the efficiency of an algorithm.

ANALYSIS OF ALGORITHM
The analysis of algorithms is a major task in computer science. In order to
compare algorithms, we must have some criteria to measure the efficiency of our
algorithms. Suppose M is an algorithm and suppose n is the size of the input data. The
time and space used by the algorithm M are the two measures for the efficiency of M.
The time is measured by counting the number of key operations – in sorting and
searching algorithms, for example, the number of comparisons. The space is measured by
counting the maximum of memory needed by the algorithm.

Written By: KASHIF HAMEED


2

COMPLEXITY OF ALGORITHM
The complexity of an algorithm is the function f(n) which gives the running time
and/or space in terms of the input size n. Frequently, the storage space required by an
algorithm is simply a multiple of the data size n. The following example illustrates tat the
function f(n) which gives the running time of an algorithm, depends not only on the size
n of the input data but also on the particular data.
Example: Suppose we are given an English short story, and suppose we want to search
through TEXT for the first occurrence of the given 3- letter word W. If W is the 3-letter
word “the” then is likely that w occurs near the beginning of TEXT so f(n) will be small.
On the other hand, if W is the 3-letter word “zoo” then W may not appear in TEXT at all
so f(n) will be large.

SORTING
Sorting means arranging a set of data in some specific order. There are different
methods that are used to sort the data in ascending or descending order. These methods
can be divided into two categories.

Why do we sorting?
 Searching for an element in an array will be more efficient. (Example: Looking for
an information like phone number)
 It’s always nice to see data in sorted display. (Example: Spreadsheet or Database
application)
 Computers sort things much faster.
Examples of Sorting:
 List containing exam scores sorted from Lowest to Highest or from Highest to
Lowest.
 List containing words that were misspelled and be listed in alphabetical order.
 List of student records and sorted by student number or alphabetically by first or last
name.

TYPES OF SORTING:
There are two types of sorting.
1. INTERNAL SORTING
If all the data that is to be sorted can be accommodated at a time in memory then
internal sorting methods are used.

2. EXTERNAL SORTING
When the data to be sorted is so large that some of the data is present in the
memory and some is kept in auxiliary memory (hard disk, floppy, tape etc), then external
sorting methods are used

EXTERNAL SORTING

This sorting is applied to the huge amount of data that can not be accommodated
in the memory all at a time. So the data from the disk is loaded into memory part by part
and each part that is loaded is sorted and the sorted data is stored into some immediate
file. Finally all the sorted parts present in different files are merged into one single file.

Written By: KASHIF HAMEED


3

 Initially the original file (file number 1) is partitioned into two files (file number 2
and 3). Then one item is read from each file and the two items are written in
sorted order in a new file (file number 4).
 Once again the item is read from each partitioned files (file number 2 and 3) and
these two records are written in sorted order in another new file (file number 5).
Thus alternate pair of sorted items are stored in the file number 4 and 5.
 This procedure is repeated till the partitioned files (file number 2 and 3) come to
an end.
Now the following procedure is repeated twice.
 Read one item from file number 4 and 5 and write them in sorted order in file
number 2.
 Read one item from file number 4 and 5 and write them in sorted order in file
number 3.
Note that instead of creating two new files, the partitioned files (2 and 3) are being
reused.
After this the following procedure is repeated 4 times:
 Read one item from file number 2 and 3 and write them in sorted order in file
number 4.
 Read one item from file number 2 and 3 and write them in sorted order in file
number 5
 In this way alternately items are moved from a pair of partitioned files to the pair
of new files and from pair of new files to a pair of partitioned files. This
procedure is repeated till the time we do not end up writing entire data in a single
file. When this happens all the items in this file would be in sorted order.

INTERNAL SORTING TECHNIQUES


1. SELECTION SORT
Suppose the list of numbers A[1],A[2]……….A[N] is in memory. The selection sort
algorithm works as follows:
1. Compare A[1] and A[2] and arrange them in desired order so that A[1]<A[2].
Then compare A[1] and A[3] and arrange them in desired order so that
A[1]<A[3]. Then compare A[1] and A[4] and arrange them in desired order so
that A[1]<A[4].Continue until we compare A[1] and A[N] and arrange them so
that A[1]<A[N].
Observe that step-1 involves N-1 comparisons. (During step-1 the smallest element
is selected to the first position). When step-1 is completed A[1] will contain the
smallest element.
2. Repeat comparisons. Now we compare A[2] and A[3] and arrange them in desired
order so that A[2]<A[3]. Then compare A[2] and A[4] and arrange them in
desired order so that A[2]<A[4].Continue until we compare A[2] and A[N] and
arrange them so that A[2]<A[N].
Step-2 involves N-2 comparisons. When step-2 is completed A[2] will contain the
smallest element.
3. ………………………………………………………………………………………
………………………………………………………………………………………
………………………………………………………………………………………
Step N-1. Compare A[N-1] and A[N] and arrange them so that A[N-1]<A[N] .
After N-1 steps the list will be sorted in increasing order.

Written By: KASHIF HAMEED


4

PRACTICAL EXAMPLE

The output of the project will be

How it Works?
Suppose the user enters 56, 78, 33, 81 and 12 in the array. The above example sorts these
values as follows:

PASS 1 56 78 33 81 12

56 78 33 81 12
33 78 56 81 12
33 78 56 81 12
12 78 56 81 33

PASS 2 12 78 56 81 33
12 56 78 81 33
12 56 78 81 33
12 33 78 81 56
PASS 3 12 33 78 81 56
12 33 78 81 56
12 33 56 81 78
PASS 4 12 33 56 81 78
12 33 56 78 81

Written By: KASHIF HAMEED


5

ALGORITHM

Here DATA is an array with N elements. This algorithm sorts the elements in DATA.
1) Repeat Steps 2 and 3 for K=1 to N-1.
2) Set S=K+1
3) Repeat while S <= N: (Executes pass)
a. If DATA [K] > DATA[S] then
Interchange DATA[K] and DATA[S]
[End of If Structure]
b. Set S = S + 1.
[End of Inner Loop]
[End of Step 1 Outer Loop]
4) Exit.

COMPLEXITY

Algorithm Worst Case Average Case Best Case


Selection Sort O(n2) O(n2) O(n2)

2. BUBBLE SORT

Suppose the list of numbers A[1],A[2]……….A[N] is in memory. The bubble sort


algorithm works as follows:
1. Compare A[1] and A[2] and arrange them in desired order so that A[1]<A[2].
Then compare A[2] and A[3] and arrange them in desired order so that
A[2]<A[3]. Then compare A[3] and A[4] and arrange them in desired order so
that A[3]<A[4].Continue until we compare A[N-1] and A[N] and arrange them
so that A[N-1] < A[N].
Observe that step-1 involves N-1 comparisons. (During step-1 the largest element is
“bubbled up” to the nth position). When step-1 is completed A[N] will contain the
largest element.
2. Repeat step-1 with one less comparison. Now we stop after we compare and
possible rearrange A[N-2] and A[N-1].
Step-2 involves N-2 comparisons. When step-2 is completed the second largest
element will occupy A[N-1].
3. Repeat Step-1 with two fewer comparisons; that is we stop after we compare and
possible rearrange A[N-3] and A[N-2].
4. ………………………………………………………………………………………
………………………………………………………………………………………
………………………………………………………………………………………
Step N-1. Compare A[1] and A[2] and arrange them so that A[1]<A[2] .
After N-1 steps the list will be sorted in increasing order.

Written By: KASHIF HAMEED


6

PRACTICAL EXAMPLE

The output of the project will be

Written By: KASHIF HAMEED


7

How it Works?
Suppose the user enters 56, 78, 33, 81 and 12 in the array. The above example sorts these
values as follows:

PASS 1 56 78 33 81 12
56 78 33 81 12
56 33 78 81 12
56 33 78 81 12
56 33 78 12 81
PASS 2
56 33 78 12 81
33 56 78 12 81
33 56 78 12 81
33 56 12 78 81
PASS 3
33 56 12 78 81
33 56 12 78 81
33 12 56 78 81
PASS 4
33 12 56 78 81
12 33 56 78 81

ALGORITHM

Here DATA is an array with N elements. This algorithm sorts the elements in DATA.
1. Repeat Steps 2 and 3 for K=1 to N-1.
2. Set S=1
3. Repeat while S <= N-K: (Executes pass)
a. If DATA [S] > DATA[S+1] then
Interchange DATA[S] and DATA[S+1]
[End of If Structure]
b. Set S = S + 1.
[End of Inner Loop]
[End of Step 1 Outer Loop]
4. Exit.

COMPLEXITY

Algorithm Worst Case Average Case Best Case


Bubble Sort O(n2) O(n2) O(n2)

Written By: KASHIF HAMEED


8

3.INSERTION SORT

Suppose an array A with n elements A [1], A [2],……..A[N] is in memory. The


insertion sort algorithm scans A from A [1] to A [N], inserting each element A [K] into
its proper position in the previously sorted sub array A [1], A[2],…[K -1]. That is

Pass 1: A[1] by itself is trivially sorted.


Pass 2: A[2] is inserted either before or after A[1] so that A[1], A[2] is sorted.
Pass 3: A[3] is inserted into its proper place in A[1], A[2] that is before A[1]
between A[1] and A[2] , or after A[2] , so that A[1], A[2] and A[3] is
sorted.
Pass 4: A[4] is inserted into its proper place in A[1], A[2] and A[3] so that:
A[1], A[2],A[3]and A[4] is sorted.
………………………………………………………………………………
Pass N. A[N] is inserted into its proper place in A[1], A[2]…….A[N-1] so
that: A[1], A[2]………A[N] is sorted.

PASS 1

56 78 33 42 12
56 78 33 42 12
PASS 2
56 78 33 42 12
56 33 78 42 12
33 56 78 42 12
PASS 3

33 56 78 42 12
33 56 42 78 12
33 42 56 78 12
33 42 56 78 12
PASS 4

33 42 56 78 12
33 42 56 12 78
33 42 12 56 78
33 12 42 56 78
12 33 42 56 78

Written By: KASHIF HAMEED


9

ALGORITHM

(Insertion Sort) INSERTION (A, N)


This algorithm sorts the array A with N elements.
1. Repeat step 2 to 4 for K = 2,3,…..N.
2. Set TEMP = A [K] and P = K – 1.
3. Repeat while TEMP < A [P]
i. Set A [P + 1] = A [P] [Moves element forward]
ii. Set P = P – 1.
[End of loop]
4. Set A [P + 1] = TEMP. [Inserts element in proper place]
[End of step 1 loop]
5. Return.

COMPLEXITY

Algorithm Worst Case Average Case Best Case


Insertion Sort O(n2) O(n2) n-1

Written By: KASHIF HAMEED


10

4.MERGE SORT
Suppose A is a sorted list with r elements and B is a sorted list with s elements.
The operation that combine the elements of A and B into a single sorted list C with
n = r + s elements is called merging. One simple way to merge is to place the elements of
B after the elements of A and then use some sorting algorithm on the entire list. This
method does not take advantage of the fact that A and B are individually sorted. A much
more effective algorithm is given below.
Suppose one is given two sorted decks of cards. At each step the two front cards
are compared and the smaller one is placed in the combined deck. When one of the decks
is empty, all of the remaining cards in the other deck are put at the end of the combined
deck.
22 44 55 77 88 33 66 70

22

22 44 55 77 88 33 66 70

22 33

22 44 55 77 88 33 66 70

22 33 44

22 44 55 77 88 33 66 70

22 33 44 55

22 44 55 77 88 33 66 70

22 33 44 55 66

22 44 55 77 88 33 66 70

22 33 44 55 66 70

22 44 55 77 88 33 66 70

22 33 44 55 66 70 77

22 44 55 77 88 33 66 70

22 33 44 55 66 70 77 88
Written By: KASHIF HAMEED
11

First of all we must always keep track of the locations of the smallest element of
A and the smallest element of B which have not yet been placed in C. Let NA and NB
denote these locations respectively. Also let P donate the location in C to be filled. Thus
initially we set NA = 1, NB = 1 and P = 1. At each step of algorithm we compare
A [NA] and B [NB]
And assign the smallest element to C [P]. Then we increment P by setting P = P +1 and
we either increment NA by setting NA = NA +1 or increment NB by setting NB = NB
+1, according to whether the new element in C has come from A or from B. Furthermore,
if NA > r, then the remaining elements of B are assigned to C, or if NB > s, then the
remaining elements of A are assigned to C.

ALGORITHM

MERGING (A,R,B,S,C)
Let A and B be sorted arrays with R and S elements, respectively. This algorithm
merges A and B into an array C with N = R + S elements.

1. [Initialize] Set NA = 1, NB = 1 and P = 1.


2. [Compare] Repeat while NA <= R and NB <= S.
If A [NA] < B [NB], then
i. [Assign element from A to C] Set C [P] = A [NA]
ii. [Update pointers] Set P = P +1 and NA = NA +1
Else
i. [Assign element from B to C] Set C [P] = B [NB]
ii. [Update pointers] Set P = P +1 and NB = NB +1

[End of if structure]
3. [Assign remaining elements to C]
If NA > R, then
Repeat for K = 0,1,2,3,4……S – NB.
Set C[P + K] = B[NB + K]
[End of loop]
Else
Repeat for K = 0,1,2,3,4……R – NA.
Set C[P + K] = A[NA + K]
[End of loop]
[End of if structure]
4. Exit.

Written By: KASHIF HAMEED


12

5. QUICK SORT
Quick sort is an algorithm of divide and conquer type. That is, the problem of sorting a
set is reduced to the problem of sorting two smaller sets. We illustrate this “Reduction
step” by means of a specific example.
Suppose A is a following list of 12 numbers

44 33 11 55 77 90 40 60 99 22 88 66

The reduction step of the quick sort algorithm finds the final position of one of the
numbers; in this illustration, we use the first number 44. This is accomplished as follows.
Beginning with the last number 66, scan the list from right to left, comparing each
number with 44 and stopping at the first number less than 44. The number is 22.
Interchange 44 and 22 to obtain the list.

22 33 11 55 77 90 40 60 99 44 88 66

(Observe that the number 88 and 66 to the right of 44 are each greater than 44).
Beginning with 22, next scan the list in the opposite direction from left to right,
comparing each number with 44 and stopping at the first number greater than 44. The
number is 55. Interchange 44 and 55 to obtain the list.

22 33 11 44 70 90 40 60 99 55 88 66

(Observe that the number 22, 33 and 11 to the left of 44 are each less than 44). Beginning
with 55, now scan the list in the original direction from right to left, comparing each
number with 44 and stopping at the first number less than 44. The number is 40.
Interchange 44 and 40 to obtain the list.

22 33 11 40 70 90 44 60 99 55 88 66

(Observe that the numbers to the right of 44 are greater than 44). Beginning with 40, next
scan the list in the opposite direction from left to right, comparing each number with 44
and stopping at the first number greater than 44. The number is 77. Interchange 44 and 77
to obtain the list.

22 33 11 40 44 90 70 60 99 55 88 66

(Observe that the numbers to the left of 44 are each less than 44). Beginning with 77,
now scan this list from right to left, until meeting the first number less than 44. We don’t
meet such a number before meeting 44. This means all numbers have been scanned and
compared with 44. Furthermore all numbers less than 44 now from the sub list of
numbers to the left of 44, and all numbers greater than 44 now from the sub list of
numbers to the right of 44, as shown below.

22 33 11 40 44 90 70 60 99 55 88 66

Written By: KASHIF HAMEED


13

22 33 11 40 44 90 70 60 99 55 88 66

22 33 11 40 90 70 60 99 55 88 66

11 33 22 40 66 70 60 99 55 88 90

11 22 33 40 66 70 60 90 55 88 99

66 70 60 88 55 90 99

11 66 70 60 88 55 90 99

33 40 66 70 60 88 55 99

33 55 70 60 88 66

40 55 66 60 80 70

55 60 66 80 70

55 60 66 80 70

55 60 80 70

55 70 80

60 80

70

The sorted array is

11 22 33 40 44 55 60 66 70 80 90 99

COMPLEXITY
Algorithm Worst Case Average Case Best Case
Quick Sort O(n2) Log2n Log2n

6. SHELL SORT
Shell sort is named after its inventor, Donald Shell, who published it in 1959.
It is a generalization of insertion sort, with two observations in mind.
• Insertion sort is efficient if the input is “almost sorted”.
• Insertion sort is inefficient, on average, because it moves values just one position
at a time.
Shell sort improves insertion sort by comparing elements separated by a gap of several
positions. This lets an element take “bigger steps” towards its expected position. Multiple
passes over the data are taken with smaller and smaller gap sizes. The last step of a Shell
sort is a plain insertion sort, but by then, the array of data is guaranteed to be almost
sorted.

Written By: KASHIF HAMEED


14

• Shell sort is also known as diminishing increment sort.


• Advantage of Shell sort is that it’s only efficient for medium size lists. For bigger
lists the algorithm is not the best choice.
• 5 times faster than bubble sort and a little over twice as fast as the insertion sort,
its closest competitor.
• Disadvantage of Shell sort is that it is a complex algorithm and its not nearly as
efficient as the merge, heap and quick sorts.
• The Shell sort is still significantly slower than the merge, heap and quick sorts,
but its relatively simple algorithm makes it a good choice for sorting lists of less
than 5000 items unless speed important. It is also an excellent choice for
repetitive sorting of smaller lists.

Suppose we have an array


18 32 12 5 38 33 16 2
8 numbers to be sorted, Shell increment will be floor (n/2)
floor (8/2) → floor (4) = 4

Increment 4:
Divide the array into 4 sub arrays
18 38
32 33
12 16
5 2

After arranging the sub arrays


18 38
32 33
12 16
2 5
The complete array will be

18 32 12 2 38 33 16 5

floor (4/2) → floor (2) = 2

Increment 2:
Divide the array into 2 sub arrays
18 12 38 16
32 2 33 5
After arranging the sub arrays
12 16 18 38
2 5 32 33
The complete array will be
12 2 16 5 18 32 38 33
floor (2/2) → floor (1) = 1

Increment 1:
Now sort the array by insertion sort and resulting array will be

Written By: KASHIF HAMEED


15

2 5 12 16 18 32 33 38
ARRAY SEARCHING TECHNIQUES

1.LINEAR SEARCH

Suppose a linear array DATA contains n elements and suppose a specific ITEM
of information is given. We want either to find the location LOC of ITEM in the array
DATA or to send some message, such as LOC = 0, to indicate that ITEM does not appear
in DATA. The linear search algorithm solves this problem by comparing ITEM, one by
one, with each element in DATA. That is we compare ITEM with DATA [1], then
DATA [2] and so on, until we find LOC such that ITEM = DATA [LOC].

ALGORITHM

(Linear search) A linear array DATA with N elements and a specific ITEM of
information are given. This algorithm finds the location LOC of ITEM in the array
DATA or sets LOC = 0.

1. [Initialize] Set K: = 1and LOC:= 0.


2. Repeat steps 3 and 4 while LOC =0 and K<=N.
3. If ITEM = DATA [K], then set LOC: = K.
4. Set K: = K+1 [Increments counter]
[End of step 2 loop.]
5. [Successful?]
If LOC = 0 then
Write: ITEM is not in the array DATA.
Else
Write: LOC is the location of ITEM.
[End of if structure]
6. Exit.

Written By: KASHIF HAMEED


16

2. BINARY SEARECH

Suppose DATA is an array which is sorted in increasing numerical order or,


equivalently alphabetically. Then there is extremely efficient algorithm called binary
search which can be used to find the location LOC of a given ITEM of information in
DATA. We indicate the general idea of this algorithm by means of an idealized version
of a familiar everyday example.
Suppose one wants to find the location of some name in telephone directory.
Obviously one does not perform a linear search. Rather one opens the directory in the
middle to determine which half contains the name being sought. Then one opens that half
in the middle to determine which quarter of the directory contains the name. Then one
opens that quarter in the middle to determine which eighth of the directory contains the
name. And so on. Eventually one finds the location of the name since one is reducing the
number of possible locations for it in the directory.
The binary search algorithm applied to our array DATA works as follows. During
each stage of our algorithm, our search for ITEM is reduced to segments of elements of
DATA.

DATA[BEG],DATA[BEG+1],DATA[BEG+2],………………..DATA[END]

Note that the variables BEG and END denote, respectively, the beginning and end
locations of the segment under consideration. The algorithm compares ITEM with the
middle element DATA [MID] of the segment, where MID is obtained by

MID = INT ((BEG + END) /2)


If DATA [MID] = ITEM then the search is successful and we set LOC = MID. Otherwise
a new segment of DATA is obtained is as follows:
If ITEM < DATA [MID], then ITEM can appear only in the left half of the segment:

DATA [BEG], DATA [BEG+1], …………….. DATA [MID-1]


So we reset END = MID –1 and begin searching again
If ITEM > DATA [MID], then ITEM can appear only in the right half of the segment:
DATA [MID+1], DATA [MID+2], …………….. DATA [END]
So we reset BEG = MID +1 and begin searching again.

Initially we begin with the entire array DATA i.e we begin with BEG = 1 and END = n
more generally with BEG=LB and END = UB.
If ITEM is not in the DATA then eventually we obtain
END < BEG
This condition signals that the search is unsuccessful and in such a case we assign LOC =
NULL. Here NULL is a value that lies outside the set of indices of DATA.

Written By: KASHIF HAMEED


17

ALGORITHM
(Binary search) BINARY (DATA, LB, UB, ITEM, LOC)
Here DATA is sorted array with lower bound LB and upper bound UB and ITEM is a
given item of information. The variables BEG, END and MID denote respectively the
beginning, end and middle locations of a segment of elements of DATA. This algorithm
finds the location LOC of ITEM in DATA or sets LOC = NULL.

1) [Initialize segment variables.]


Set BEG = LB, END = UB and MID = INT ((BEG+END) / 2).
2) Repeat steps 3 and 4 while BEG <=END and DATA [MID]≠ITEM.
3) If ITEM < DATA [MID], then
Set END = MID –1
Else
Set BEG = MID +1
[End of if structure]
4) Set MID = INT ((BEG+END) / 2)
[End of step 2 loop]
5) If DATA [MID] = ITEM, then
Set LOC: = MID
Else
Set LOC: = NULL
[End of if structure]
6) Exit

Let DATA be the following sorted 13-element array:

DATA: 11 22 30 33 40 44 55 60 66 77 80 88 99
We apply the binary search to DATA for different values of ITEM.

1. Suppose ITEM = 40. The search for ITEM in the array DATA is shown below where
the values of DATA [BEG] and DATA [END] in each stage are indicated by circles
and the value of DATA [MID] by a square.
a) Initially BEG =1 and END = 13. Hence
MID = INT ((1+13) /2) = 7 and so DATA [MID] = 55
11 22 30 33 40 44 55 60 66 77 80 88 99
b) Since 40 < 55, END has its value changed by END = MID –1 = 6, BEG = 1
Hence MID = INT ((1+6) / 2) = 3 and so DATA [MID] = 30
11 22 30 33 40 44 55 60 66 77 80 88 99
c) Since 40 > 30, BEG has its value changed by BEG = MID +1 = 4, END = 6
Hence MID = INT ((4+6) / 2) = 5 and so DATA [MID] = 40
11 22 30 33 40 44 55 60 66 77 80 88 99
We have found ITEM in location LOC = MID = 5 [Successful]

Written By: KASHIF HAMEED


18

2. Suppose ITEM = 85. The search for ITEM in the array DATA is shown below
a) Initially BEG =1 and END = 13,
MID = INT ((1+13) /2) = 7 and so DATA [MID] = 55
11 22 30 33 40 44 55 60 66 77 80 88 99

b) Since 85 > 55, BEG has its value changed by BEG = MID +1 = 8, END = 13
Hence MID = INT ((8+13) / 2) = 10 and so DATA [MID] = 77
11 22 30 33 40 44 55 60 66 77 80 88 99

c) Since 85 > 77, BEG has its value changed by BEG = MID +1 = 11, END = 13
Hence MID = INT ((11+13) / 2) = 12 and so DATA [MID] = 88
11 22 30 33 40 44 55 60 66 77 80 88 99

d) Since 85 < 88, END has its value changed by END = MID –1 = 11, BEG = 11
Hence MID = INT ((11+11) / 2) = 11 and so DATA [MID] = 80
(Observe that now BEG = END = MID = 11.)
11 22 30 33 40 44 55 60 66 77 80 88 99

Since 85 > 80, BEG has its value changed by BEG = MID +1 = 12. END = 11
But now BEG > END. Hence ITEM does not belong to DATA.
[Unsuccessful]

COMPARISON OF LINEAR AND BINARY SEARCH


The linear search algorithm would be impossible in practice if we were searching
through a list of thousands of items. In this case binary search algorithm is very efficient
because it requires only about 20 comparisons with an initial list of 1000000 elements.
Binary search algorithm requires two conditions. (1) The list must be sorted and (2) one
must have direct access to the middle element in any sub list. This means that one must
essentially use a sorted array to hold the data. But keeping data in a sorted array is very
expensive when there are many insertions and deletions. Unfortunately inserting an
element in an array requires elements to be moved down the list and deleting an element
from an array requires elements to be moved up the list. Accordingly n such situations
one may use a different data structure such as a link list or a binary tree to store the data.
The complexity of the linear search algorithm is given by C(n) = n /2
The complexity of the binary search algorithm is given by C(n) = log2n

Written By: KASHIF HAMEED


19

LINKED LISTS
Why Linked Lists are preferred over arrays?
For storing similar data in memory, we can use either an array or a linked list.
Arrays are simple to understand and elements of an array are accessible but arrays suffer
from the following limitations.
• Arrays have a fixed dimension. Once the size of an array is decided it can not be
increased or decreased during execution.
• Arrays elements are always stored in contiguous memory locations. At time it
might so happen that enough contiguous location might not be available for the
array that we are trying to create.
• Operation like insertion of a new element or deletion of existing element from the
array is pretty tedious. This is because during insertion or deletion each element
has to be shifted one position to the right or to the left.
Linked list overcome all these limitations.
• A linked list can grow and shrink in size during its life time that is there is no
maximum size of linked list.
• As nodes of linked list are stored at different memory locations it hardly happened
that we fall short of memory when required.
• Unlike arrays while inserting or deleting the nodes of the linked list, shifting of
nodes is not required.
SINGLE/ONE WAY/LINEAR LINKED LIST
It is the very common data structure often used to store similar data in memory.
Elements of the linked list are called nodes. Each node consists of a data part and a link
part. A link is a pointer or an address that indicates the location of the node containing the
successor of the list element. N stands for NULL which indicates that this is the last node
in the list.
BUILDING A LINKED LIST
• Allocate the memory for the first node.

200
• Set a value in the data part and NULL in the link part.
70 N
200
• Allocate the memory for the second node.

100
• Set a value in the data part and NULL in the link part.
50 N
100
• Set the link part of the first node with the address of the second.

70 100 50 N
200 100

Written By: KASHIF HAMEED


20

OPEARATIONS ON THE LINKED LIST


To understand different operations on the linked list, we have a structure “node”
to represent a node. The structure “node” contains a data part and a link part.
struct node
{
dnode *link;
int data;
} *p;

The variable “p” has been declared as pointer to a node. We have used this pointer
as pointer to the 1st node in the linked list.
How many nodes get added to the linked list? P would continue to point t the 1st
node in the list. When no node has added to the list, p has been set to NULL to indicate
that the list is empty.
Addition of a node in a linked list
• To an empty list.
• At the end.
• At the beginning.
• In the middle (anywhere)

Append function
This function deals with two situations:
• The node is being added to an empty list.
• The node is being added at the end of the existing list.
In the 1st case the condition
if(p = = NULL) gets satisfied then space is allocated for the node using new operator.
temp = new node;
Data and link part of this node are setup using the statements.
temp→data = num;
temp→link = NULL;

Lastly p is made to point this node.

In the 2nd case when the linked list is not empty, the condition
if(p = = NULL) would fail.Now temp is made to point to the first node in the list.
temp=p;
Then using temp we have traversed through the entire linked list using a statement
while(temp→link !=NULL)
temp= temp→link;
Every time through the loop the statement
temp= temp→link;
makes temp point to the next node in the list. When temp reaches the last node, the
condition
temp→link !=NULL
would fail.

Written By: KASHIF HAMEED


21

Allocate the memory for the new node and set pointer “r” to point this node.
r = new node;
Set a value in the data part and NULL in the link part of the new node. All that now
remains to be done is connecting the previous last node to the new last node. This is done
through statement.
temp→link = r;

p temp

70 200 50 400 10 N

100 200 400


p temp r

70 200 50 400 10 300 10 N


100 200 400 300

Append function

p=NULL;
append(int num)
{
node *temp, *r;
if(p = = NULL)
{
temp = new node;
temp→data = num;
temp→link = NULL;
}
else
{
temp=p;
while(temp→link !=NULL)
temp= temp→link;
r = new node;
r→data = num;
r→link = NULL;
temp→link = r;
}
}

Written By: KASHIF HAMEED


22

Add a node at the beginning


For adding a new node at the beginning, first space is allocated for the node and
data is stored in it.
temp = new node;
temp→data = num;
Now we need to make a link part of this node points to the existing 1st node.
temp→link = p;

Lastly this new node must be made the 1st node in the list. This is done by the statement.
p = temp;

temp p

70 200 50 400 10 300 40 N

100 200 400 300


p temp

70 200 50 400 10 300 40 N

100 200 400 300

Add a node at the beginning


Add_at_beg(int num)
{
node *temp;
temp = new node;
temp→data = num;
temp→link = p;
p = temp;
}
Adding a node after specific node

To begin with, “temp” is set to point the 1st node through statement
temp = p;
Then through loop, we skip number of nodes after which a new node is to be added.
Suppose we wish to add a new node after the 3rd node in the list. Allocate memory for the
new node and set pointer “r” to point this node.
r = new node;
Using “r” set a value in the data part of the node. All that remains to be done is
readjustment of links such that the new node adjusts in between the 3rd and 4th node and
this is achieved through the statement. r
r→link = temp→link;
temp→link = r; 50 N

p temp 300
70 200 50 400 10 N
100 200 400

Written By: KASHIF HAMEED


23

50 400
p temp
300
70 200 50 300 10 N
100 200 400

Adding a node after specific node

Add_after(int loc, int num)


{
node *temp, *r;
temp = p;
for(int i = 0; i<loc ; i++)
{
if(temp = NULL)
{
printf(“There are less nodes in the list”);
return;
}
}
r = new node;
r→data = num;
r→link = temp→link;
temp→link = r;

Written By: KASHIF HAMEED


24

Deletion of a specific node from the linked list


Case (first element)
In this function, through the while loop we have traversed through the entire
linked list, checking at each node whether it is the node to be deleted. If so, we have
checked, if the node being deleted is the first node in the list. If so, we have simply
shifted “p” to the next node and then deleted the earlier node.

Case (Intermediate node)


If the node to be deleted is the intermediate node, then the position of various
pointers and the links before and after deletion is shown below.

P old temp

70 200 50 400 10 300 40 N

100 200 400 300

p old

70 200 50 300 40 N

100 200 300

Deletion of a specific node from the linked list


del(int num)
{
node *temp, *old;
temp = p;

while(temp!=NULL)
{
if(temp→data = num)
{
if(temp= p)
p = temp→link;
else
old→link = temp→link;
delete temp;
return;
}
else
{
old = temp;
temp = temp→link;
}
}
printf(“Element not found”);
}

Written By: KASHIF HAMEED


25

Display function

display()
{
node *temp = p;
while(temp != NULL)
{
printf(“%d”, temp→data);
temp = temp→link;
}
}

Count function
count()
{
node *temp = p;
int c = 0;
while(temp != NULL)
{
temp = temp→link;
c ++;
}
return c;
}
ASCENDING ORDER LINKED LIST
Let us now try to understand the function “add” that is responsible for maintaining the
linked list in the ascending order.
The pointer “p” either holds the address of the 1st node or a NULL value in case of empty
linked list. The add() function takes one parameter num that holds the data that is to be
inserted in the list.
Initially memory is allocated for the new node, its address is stored in pointer ”r” and
using “r” the value of num is stored in its data part.
There are four different places where the new node can be added in the list

a) To an empty list.
b) At the beginning.
c) At the end.
d) In the middle (anywhere)

Case (a) and (b)


A condition is checked through a statement
if( p = NULL || p→data > num)
This is done to check whether the new node to be added is the first node.
In either case “p” is made to point to the new node.
p = r;
The value of the temp is stored in the link part of the new node. Pointer “temp” holds a
value NULL if the list is empty and holds the address of the first node if the new node
that is to be added is added before the 1st node.

Written By: KASHIF HAMEED


26

Case (c) and (d)


If both the conditions are false then we need to search a position where the new node
must be inserted. Thus we need to traverse the list through the while loop.
Initially pointer “temp” points to the 1st node and each time through the while
loop; it is made to point to the next node in the list through statement.
temp = temp→link;
Inside the while loop, num is compared with the data part of the current node (node
pointed by temp) and the data part of the next node (node pointed to by temp→link). The
new node should be inserted if temp→data is smaller than or equal to num and
temp→link→ data is greater than num or if the current node is the last node that is
if((temp→data < =num) && (temp→link→ data > num) || (temp→link= =NULL))

CASE “C”
p temp

50 200 70 400 80 N
100 200 400

p temp r

50 200 70 400 80 300 90 N

100 200 400 300

CASE “D” r
70 N

p temp 300

40 200 50 400 90 N

100 200 400

r
70 400
p temp 300

40 200 50 300 90 N

100 200 400

Written By: KASHIF HAMEED


27

Functions of case a, b, c and d

add( int num)


{
node *temp= p, *r;
r = new node;
r→data = num;
if( p = NULL || p→data > num)
{
p = r;
p→link = temp;
}
else
{
while(temp→link != NULL)
{
if((temp→data < =num) && (temp→link→ data > num) || (temp→link= =NULL))
{
r→link = temp→link;
temp→link = r;
return;
}
}
temp= temp→link;
}
}
Reversing the links
reverse()
{
node *q, *r, *s;
q = p;
r = NULL;
while(q != NULL)
{ s = r;
r = q;
q = q→link;
r→link = s;
}
p = r;
}
In the function reverse(), to traverse the linked list , a variable “q” is required. We have
initialized q with p.
To begin with, we need to store a NULL value in the linked part of the 1st node which is
done through the statements. s = r; r = q; q = q→link;
“r ” is initialized to NULL so “s” would also contain NULL. Now “r” is assigned q so “r”
is also start pointing to the 1st node. Finally r→link is assigned “s”. But if we store a
NULL value in the link part of the 1st node then the address of the 2nd node is lost. So
before storing a NULL value, “q” is made to point to the 2nd node.
q = q→link;

Written By: KASHIF HAMEED


28

During the 2nd iteration of the while loop, “r” points to the 1st node and “q” points to the
2nd node.Now the link part of the 2nd node should point to the 1st node which is done
through the same statement.
s = r; r = q; q = q→link;
After the statement “s” would point to the 1st node and “r” would point to the 2nd node
and r→link starts pointing to the 1st node. But if we store the value of “s” in the link part
of the 2nd node, address of the 3rd node would be lost. So before this step “q” is made to
point to the third node. q = q→link;
While traversing through while loop, each time “q” starts pointing to the next node and
“r” starts pointing to the previous node.
As a result, when the while loop ends, all the links have been adjusted properly.
Finally outside the while loop, the statement p = r;
ensures that the pointer “p” now start pointing to the node which is the last node of the
original list.
p q r = NULL

2 200 8 400 4 300 3 N

100 200 400 300


p r q s = NULL

2 N 8 400 4 300 3 N

100 200 400 300

p s r q

2 N 8 100 4 300 3 N
100 200 400 300

p s r q

2 N 8 100 4 200 3 N

100 200 400 300

q = NULL
p s r

2 N 8 100 4 200 3 400

100 200 400 300

r p

3 400 4 200 8 100 2 N


300 400 200 100

Written By: KASHIF HAMEED


29

SORTING A LINKED LIST


For sorting the elements of the linked list, we can use any of the standard sorting
algorithms. While performing the sorting, when it is time to exchange two elements, we
can adopt any of the following two methods.
a) Exchange the data part of the two nodes, keeping the links intact
p

2 200 8 400 4 300 3 N

100 200 400 300

2 200 3 400 4 300 8 N

100 200 400 300

b) Keep the data in the nodes intact. Simply readjust the links such that effectively the
order of the nodes changes.

2 200 8 400 4 300 3 N

100 200 400 300

2 300 8 N 4 200 3 400


100 200 400 300

Of the two methods, first one is easier to implement, but the second one is likely to be
more efficient. This is because if the data part contains an employee record (containing
name, age, salary etc) then to carry out exchange of this record would be inefficient time
wise as well as space wise. Instead if we adopt second method, since we are only
readjusting the links this would involve only pointers and not trhe bulky structures
representing records.

Written By: KASHIF HAMEED


30

Sort the linked list using selection sort


void sort_sel (int n)
{
int temp;
node *q, *r;
q = p;
for(int i = 0; i <= n-1; i++)
{
r = q→link;
for(int j = i +1; j < n; i++)
{
if(q→data > r→data)
{
temp = q→data;
q→data = r→data;
r→data = temp;
}

r = r→link;
}
q = q→link;
}
}

Sort the linked list using bubble sort


void sort_bubble (int n)
{
int temp ,k ;
node *q, *r;
k = n;
for(int i = 0; i <= n-1; i++, k--)
{
q = p;
r = q→link;
for(int j = 1; j < k; j++)
{
if(q→data > r→data)
{
temp = q→data;
q→data = r→data;
r→data = temp;
}

r = r→link;
q = q→link;

}
}
}

Written By: KASHIF HAMEED


31

CIRCULAR LINKED LIST


The linked list in which link field of he last node contain a pointer back to the first
node rather than a NULL is called circular linked list.

2 200 3 400 4 300 8 100


100 200 400 300

Although a linear linked list is a useful data structure, it has several short comings. For
example, given a pointer p to a node in a linear list, we can not reach any of the nodes
that precede the node to which p is pointing. This disadvantage can be overcome by
circular linked list.
From any point in such a list it is possible to reach any other point in the list. If we begin
at a given node and traverse the entire list, we ultimately end up at the starting point. A
circular linked list does not have a first or last node. We must, therefore, establish a first
and last node by convention. A circular linked list can be used to represent a stack and a
queue.
Addition in the circular linked list

Add_cirq (int num)


{
node *q;
q = new node;
q→data = num;
if( front = NULL)
front = q;
else
rear→link = q;
rear = q;
rear→link = front;
}
front Rear q

50 200 70 400 80 100 30


100 200 400 500
New Node
Front Rear q

50 200 70 400 80 500 30 100


100 200 400 500

Written By: KASHIF HAMEED


32

Deletion of a specific node from circular linked list

Del_cirq (int num)

{
node *q;
int num;
if(front = NULL)
printf(“Queue is empty”);
else
{
if(front = rear)
{
num = front→data;
delete front;
front = NULL;
rear = NULL;
}
else
{
q = front;
num = q→data;
front = front→link;
rear→link = front;
delete q;
}
}
Front Rear

50 200 70 400 80 500 30 100


100 200 400 500

Front Rear

50 200 70 400 80 500 30 200

100 200 400 500

Written By: KASHIF HAMEED


33

Display function

cirq_display()
{
node *q, *p;
q = front;
p = front;
do
{
printf(“%d”, q→data);
q = q→link;
}
while(q != p)
}

DOUBLE LINKED LIST

The linked list in which each node stores not only the address of next node but
also the address of previous node is called double linked list or two way linked list.

Node Prev data next

N 11 100 200 2 400 100 14 N

200 100 400

OPERATIONS ON THE DOUBLE LINKED LIST

To understand different operations on double linked list, we have a structure


“dnode” to represent a node. The structure “dnode” contains a data part and two link parts
to point to next and previous nodes. We have declared two pointers “next” and “prev” of
dnode type. The variable p has been declared as pointer to the 1st node.

struct dnode
{
dnode *prev;
int data;
dnode *next;
} *p;

Written By: KASHIF HAMEED


34

Append function
d_append (int num)
{ dnode *q, *r;
if(q = = NULL)
{ q = new dnode;
q→data = num;
q→prev = NULL;
q→next = NULL;
p = q;
}
else
{ while(q→next !=NULL)
q = q→next;
r = new dnode;
r→data = num;
r→next = NULL;
r→prev = q;
q→next = r;
}
}
Add a node at the beginning
Add_at_beg(int num)
{
dnode *q;
q = new dnode;
q→prev = NULL;
q→next = p;
q→data = num;
p→prev = q;
p = q;
}
Adding a node after specific node in double linked list
Add_after(int loc, int num)
{
dnode *q, *r;
q = p;
for(int i = 0; i<loc ; i++)
{ q = q→next;
if(q = NULL)
{ printf(“There are less nodes in the list”);
return;
}q = q→prev;
r = new dnode;
r→data = num;
r→prev = q;
r→next = q→next;
r→next→prev = r;
q→next = r;
}

Written By: KASHIF HAMEED


35

temp

p q 300
N 90 N

N 11 100 200 2 400 100 14 N

200 100 400


p q

N 11 100 200 2 300 100 90 400 300 14 N

200 100 300 400

Deletion of a specific node from the double linked list


d_del(int num)

{
dnode *q = p;
while(q!=NULL)
{
if(q→data = num)
{
if(q = = p)
{
p = p→next;
p→prev = NULL;
}
else
{
if(q→next = NULL)
q→prev→next = NULL;
else
{
q→prev→next = q→next;
q→next→prev = q→prev;
}
delete q;
}
return;
}
q = q→next;
printf(“Element not found”);
}

Written By: KASHIF HAMEED


36

CIRCULAR DOUBLE LINKED LIST


The double linked list in which “next” pointer field of last node contains address
of 1 node instead of NULL and “prev” pointer field of 1st node contains address of last
st

node instead of NULL is called circular double linked list.

400 11 100 200 2 400 100 14 200

200 100 400

STACK
A stack is a data structure in which addition of new element or deletion of an
existing element always takes place at the same end. This end is known as top of the
stack. When an item is added t the stack the operation is called “push” and when an item
is removed from the stack the operation is called “pop”. Stack is also called LIFO (Last
in First out) list. If the elements are added continuously to the stack it grows at one end.
On deletion of elements the stack shrinks at the same end, as the elements at the top get
removed.
For example stack of plates in cafeteria where every new plate added to the stack
is added at the top. Similarly, every new plate taken off the stack is also from the top of
the stack.
top top

top 5 5 top

top 4 4 4 4 top
top
1 1 1 1 1 1 top
top= NULL 3 3 3 3 3 3 3 3 top = NULL

2 2 2 2 2 2 2 2

Push Operation Pop Operation

Push function
void push( int item)
{
if( top == MAX-1)
{
printf(“Stack is full);
return;
}
top ++;
arr[top] = item;
}

Written By: KASHIF HAMEED


37

Pop function
void pop( )
{
if( top == -1)
{
printf(“Stack is emptyl);
return;
}
int data = arr[top];
top --;
return data;
}
ALGORITHM OF PUSH FUNCTION
Push(arr, top, MAX, item)
This procedure pushes an item onto a stack
1. [Stack already filled?]
if top == MAX-1 then print overflow and return

2. set top = top + 1; [increment top by 1]


3. set arr[top] = item
4. return

ALGORITHM OF POP FUNCTION


Pop(arr, top, MAX, item)
This procedure pop an item from a stack
1. [Stack is empty?]
if top == -1 then print underflow and return

2. set top = top - 1; [decrement top by 1]


3. set arr[top] = item
4. return

STACK AS A LINKED LIST

PUSH FUNCTION
struct node
{
int data;
node *link;
} *top
void stack_push( int num)
{
node *temp;
temp = new node;
temp→data = num;
temp→link = top;
top = temp;
}

Written By: KASHIF HAMEED


38

POP FUNCTION
void stack_pop( )
{
if( top == NULL)
{
printf(“Stack is emptyl);
return;
}
node *temp;
int num;
temp = top;
num = temp→data;
top = top→link;
delete temp;
return num;
}

APPLICATIONS OF STACK
The place where stacks are frequently used is in evaluation of arithmetic
expression. An arithmetic expression consists of operands and operators. The operands
can be numeric values or numeric variables. The operators used in arithmetic expression
represent the operations like addition, subtraction, multiplication, division and
exponentiation.
INFIX NOTATION
While writing an arithmetic expression, the operator symbol is usually placed
between two operands. For example
a+b
a*b+c
b–c/c
This way of representing arithmetic expressions is called infix notation. While
evaluating an infix expression usually the following operator precedence is used.
• Highest priority: Exponentiation ($)
• Next highest priority: Multiplication (*) and Division (/)
• Lowest priority: Addition (+) and Subtraction (-)
If we wish to override these priorities we can do so by using a pair of parentheses
as shown below
(a + b) *c
a * (b - c)

POLISH NOTATION
A Polish mathematician which gives two alternatives to represent an arithmetic
expression. The notations are prefix and postfix notations.
The fundamental property of Polish notation is that the order in which the operations are
to be performed is completely determined by the positions of the operators and operands
in the expression. Hence, parentheses are not required while writing expressions in Polish
notation.

Written By: KASHIF HAMEED


39

PREFIX NOTATION
In prefix notation the operator comes before the operands. For example consider
an arithmetic expression expressed in infix notation as shown below:
a+b
This expression in prefix form would be represented as follows.
+ab
POSTFIX NOTATION
In postfix notation the operator comes after the operands. For example consider
an arithmetic expression expressed in infix notation as shown below:
a+b
This expression in prefix form would be represented as follows.
ab+
The prefix and postfix expressions have three features.
• The operands maintain the same order as in the equivalent infix expression.
• Parentheses are not needed to designate the expression unambiguously.
• While evaluating the expression the priority of the operators is irrelevant.
INFIX TO PREFIX CONVERSION. (RULES)
1. If the character scanned happens to be a space then the character is skipped.
2. If character scanned is a digit or an alphabet, it is added to the target string.
3. If the character scanned happens to be closing parentheses, then it is added to
stack.
4. If the character scanned happens to be an operator then firstly the top most
element from the stack is retrieved. Then, through a while loop, the priorities of
the character scanned and the character popped are compared.
a) If the priority of character popped is higher than the character
scanned, then character popped gets added to the target string.
b) If the character popped has lower or same priority than the character
scanned then the loop terminated. The character popped pushed back to
stack then the character scanned is also pushed to the stack.
5. If the character scanned happens to be opening parentheses, then the operators
entered into the stack are retrieved through a loop. The loop continues till it does
not encounter closing parentheses. The operators popped are added to the target
string.
INFIX TO POSTFIX CONVERSION. (RULES)
1. If the character scanned happens to be a space then the character is skipped.
2. If character scanned is a digit or an alphabet, it is added to the target string.
3. If the character scanned is opening parentheses, then it is added to stack.
4. If the character scanned happens to be an operator then firstly the top most
element from the stack is retrieved. Then, through a while loop, the priorities of
the character scanned and the character popped are compared.
a) If the character popped has higher or same priority than the character
scanned, then character popped gets added to the target string.
b) If the character popped has lower priority than the character scanned then
the loop terminated. The character popped pushed back to stack then the
character scanned is also pushed to the stack.
5. If the character scanned happens to be closing parentheses, then the operators
entered into the stack are retrieved through a loop. The loop continues till it does
not encounter opening parentheses. The operators popped are added to the target
string.

Written By: KASHIF HAMEED


40

INFIX TO PREFIX

Infix expression: 4 $ 2 * 3 – 3 + 8 / 4 / ( 1 + 1)

Reversed Infix expression: ) 1 + 1 ( / 4 / 8 + 3 – 3 * 2 $ 4


Char scanned Stack Prefix Expression
) )
1 ) 1
+ )+ 1
1 )+ 11
( Empty +11
/ / +11
4 / 4+11
/ // 4+11
8 // 84+11
+ + //84+11
3 +– 3//84+11
– +– 3//84+11
3 +– 33 / / 8 4 + 1 1
+ +–* 33 / / 8 4 + 1 1
2 +–* 2 33 / / 8 4 + 1 1
$ +–*$ 2 33 / / 8 4 + 1 1
4 +–*$ 4 2 33 / / 8 4 + 1 1
+–* $4233//84+11
+– *$4233//84+11
+ –*$4233//84+11
Empty +– *$4233//84+11

Infix expression: (A - B) * ( D / E)

Reversed Infix expression: ) E / D ( * ) B – A (


Char scanned Stack Prefix Expression
) )
E ) E
/ )/ E
D )/ DE
( Empty /DE
* * /DE
) *) /DE
B *) B/DE
– *)– B/DE
A *)– AB/DE
( * -AB/DE
Empty *-AB/DE

Written By: KASHIF HAMEED


41

Infix expression: (A + B $ D) / (E - F) + G

Reversed Infix expression: G + ) F – E ( / ) D $ B + A (


Char scanned Stack Prefix Expression
G Empty G
+ + G
) +) G
F +) FG
– +)– FG
E +)– EFG
( + - EFG
/ +/ - EFG
) +/) - EFG
D +/) D - EFG
$ +/)$ D - EFG
B +/)$ BD – EFG
+ + / )+ $ BD – EFG
A + / )+ A $ BD – EFG
( +/ + A $ BD – EFG
+ / + A $ BD – EFG
Empty + / + A $ BD – EFG

PREFIX TO INFIX

Prefix expression: + – * $ 4 2 3 3 / / 8 4 + 1 1
Reversed Prefix expression: 1 1 + 4 8 / / 3 3 2 4 $ * – +

Char scanned Stack


1 1
1 1, 1
+ 1+1
4 4, 1 + 1
8 8, 4, 1 + 1
/ 8 / 4, 1 + 1
/ 8/4/1+1
3 3, 8 / 4 / 1 + 1
3 3, 3, 8 / 4 / 1 + 1
2 2, 3, 3, 8 / 4 / 1 + 1
4 4, 2, 3, 3, 8 / 4 / 1 + 1
$ 4 $ 2, 3, 3, 8 / 4 / 1 + 1
* 4 $ 2 * 3, 3, 8 / 4 / 1 + 1
– 4 $ 2 * 3 – 3, 8 / 4 / 1 + 1
+ 4$2*3–3+8/4/1+1

Written By: KASHIF HAMEED


42

Prefix expression: – / * A + BDE * F + G / HK


Reversed Prefix expression: KH / G + F * EDB + A * / –

Char scanned Stack


K K
H H K
/ H/K
G G H/K
+ G+H/K
F F G+H/K
* F*G+H/K
E E F*G+H/K
D D E F*G+H/K
B B D E F*G+H/K
+ B+D E F*G+H/K
A AB+D E F*G+H/K
* A* B + D E F * G + H / K
/ A* B + D / E F * G + H / K
– A* B + D / E – F * G + H / K

Prefix expression: – / * 10 + 2, 2, 4 * 4 + 2 / 3, 3
Reversed Prefix expression: 3, 3 / 2 + 4 * 4, 2, 2 + 10 * / –

Char scanned Stack


3 3
3 3, 3
/ 1
2 2, 1
+ 3
4 4, 3
* 12
4 4, 12
2 2, 4, 12
2 2, 2, 4, 12
+ 4, 4, 12
10 10, 4, 4, 12
* 40, 4, 12
/ 10, 12
– –2

Written By: KASHIF HAMEED


43

INFIX TO POSTFIX

Infix expression: (A + B $ D) / (E - F) + G

Char scanned Stack Postix Expression


( (
A ( A
+ (+ A
B (+ AB
$ (+$ AB
D (+$ ABD
) Empty ABD$+
/ / ABD$+
( /( ABD$+
E /( ABD$+E
– /(– ABD$+E
F /(– ABD$+EF
) / ABD$+EF–
+ + ABD$+EF–/
G + ABD$+EF–/G
Empty ABD$+EF–/G+

Infix expression: 4 $ 2 * 3 – 3 + 8 / 4 / ( 1 + 1)
Char scanned Stack Postfix Expression
4 Empty 4
$ $ 4
2 $ 4, 2
* * 4,2, $
3 * 4,2, $, 3
– – 4, 2, $, 3, *
3 – 4, 2, $, 3, *, 3
+ + 4, 2, $, 3 * 3 –
8 + 42 $ 3 * 3– 8
/ +/ 42 $ 3 * 3– 8
4 +/ 42 $ 3 * 3–8 4
/ +/ 42 $ 3 * 3– 84 /
( +/( 42 $ 3 * 3– 84 /
1 +/( 42 $ 3 * 3– 84 / 1
+ +/(+ 42 $ 3 * 3– 84 / 1
1 +/(+ 42 $ 3 * 3– 84 / 1 1
) +/ 42 $ 3 * 3– 84 / 1 1 +
+ 42 $ 3 * 3– 84 / 1 1 + /
Empty 42 $ 3 * 3– 84 / 1 1 + / +

Written By: KASHIF HAMEED


44

Infix expression: (A + B) * (C - D) $ E * F
Char scanned Stack Postfix Expression
( (
A ( A
+ (+ A
B (+ AB
) Empty AB +
* * AB +
( *( AB+
C *( AB+C
– *(– AB+C
D *(– A B + CD
) * A B + CD –
$ *$ A B + CD –
E *$ A B + CD – E
* * A B + CD – E $ *
F * A B + CD – E $ * F
Empty A B + CD – E $ * F *

Infix expression: (A – B) * ( D / E)
Char scanned Stack Postfix Expression
( (
A ( A
– (– A
B (– AB
) Empty AB –
* * AB –
( *( AB –
D *( AB – D
/ *(/ AB – D
E *(/ AB – DE
) * AB – DE /
Empty AB – DE / *

POSTFIX TO INFIX
Postfix expression: A B + CD – E $ * F *
Char scanned Stack
A A
B A B
+ A+B
C A+BC
D A+B C D
– A+B C–D
E A+B C–D E
$ A+B C–D$E
* A+B*C–D$E
F A+B*C–D$EF
* A + B * C – D $ E *F

Written By: KASHIF HAMEED


45

Postfix expression: 3, 2, +, 5, 4, –, 1, $, *, 6, *

Char scanned Stack


3 3
2 3, 2
+ 5
5 5, 5
4 5 , 5, 4
– 5, 1
1 5, 1, 1
$ 5, 1
* 5
6 5, 6
* 30

Postfix expression: 4, 2, $, 3, *, 3, –, 8, 4, /, 1, 1, +, /, +

Char scanned Stack


4 4
2 4, 2
$ 4$2
3 4 $ 2, 3
* 4$2*3
3 4 $ 2 * 3, 3
– 4$2*3–3
8 4 $ 2 * 3 – 3, 8
4 4 $ 2 * 3 – 3, 8, 4
/ 4 $ 2 * 3 – 3, 8 / 4
1 4 $ 2 * 3 – 3, 8 / 4, 1
1 4 $ 2 * 3 – 3, 8 / 4, 1, 1
+ 4 $ 2 * 3 – 3, 8 / 4, 1 + 1
/ 4 $ 2 * 3 – 3, 8 / 4 / 1 + 1
+ 4$2*3–3/8/4/1+1

Convert the following infix expressions into their equivalent prefix and postfix
expressions.
1. (A +B) * (C – D) $ E *F
2. A +B *C + (D * E +F) * G
3. A * (B +D)/ E – F * (G + H / K)
Answers
1. *, *, +, A, B, $, –, C, D, E, F and A B + C D – E $ * F *
2. +, +, A, B, C, *, +, *,D, E, F, G and A B C * + D E * F + G * +
3. –, / *, A + B, D, E, *, F, +, G, /, H, K and A B D + * E / F G H K / +

Written By: KASHIF HAMEED


46

Convert the following prefix expressions into their equivalent infix expressions. Also
evaluate them.
1. *, *, +, A, B, $, –, C, D, E, F (where A =6, B =5, C =4, D =3, E =2, F =1)
2. +, /, +, A, $, B, D, –, E, F, G (where A =6, B =5, D =4, E =3, E =2, G =1
3. * - A B / D E (where A = 10, B = 5, D = 16, E = 4)
Answers
1. A +B * C – D $ E *F and 11
2. A + B $ D / E - F + G and 632
3. A – B * D / E and 20

Convert the following postfix expressions into their equivalent infix expressions.
Also evaluate them.
1. 3, 1, +, 2, ↑, 7, 4, –2, *, +, 5, –
2. 6, 2, 3, +, –, 3, 8, 2, /, +, *, 2, ↑, 3, +
3. A, B, +, C, –, B, A, +, C (where A = 1, B = 2, C = 3)
4. A, B, -, D, E, /, * (where A = 10, B = 5, D = 16, E = 4)
Answers
1. 3 + 1 ↑ 2+ 7 – 4 * 4 * 2 – 5 and 17
2. 6 – 2 + 3 * 3 + 8 / 2 ↑ 2 + 3 and 52
3. A + B – C – B + A ↑ C and – 9
4. A – B * D / E and 20
QUEUE
Queue is a linear data structure that permits insertion of a new element at one end
and deletion of an element at the other end. Placing an item in a queue is called
“insertion or enqueue”, which is done at the end of the queue called “rear”. Removing
an item from a queue is called “deletion or dequeue”, which is done at the other end of
the queue called “front”.
The first element tat gets added into the queue is the first one to get removing
from the list. Hence queue is also referred as FIFO (First in First Out) list.
Example:
Consider a queue of people waiting at the bus stop. Each new person who comes
takes his place at the end of the line. And when the bus comes, the people at the front of
the line board first. The first person in the line is the first person to leave.

A B C D E F

Front Rear

Applications of queue as a data structure are even more common than applications
of stacks. While performing tasks on a computer, it is often necessary to wait one’s turn
before having access to some device or process. Within a computer system, there may be
queues of tasks waiting for the linear printer, or for access to disk storage, or in a time
sharing system for the use of the CPU.

Written By: KASHIF HAMEED


47

Representation of a queue as an array


Addition in a queue
void add_q( int item)
{
if( rear ==MAX -1)
{
printf(“Queue is full”);
return;
}
rear++;
arr[rear] = item;
if( front == -1)
front = 0;
}

Deletion in a queue
void del_q( int item)
{
int data;
if( front == -1)
{
printf(“Queue is empty”);
return;
}
data = arr[front];
arr[front] = 0;
if(front == rear)
front = rear = -1;

else
front++;
return data;
}
Representation of a queue as a linked list
Addition in a queue
void add_q( int item)
{
node *temp;
if( temp == NULL)
{ printf(“Queue is full”);
temp→data = item;
temp→link = NULL;
if( front == NULL)
rear = front = temp;
return;
}
rear→link = temp;
rear = rear→link;
}

Written By: KASHIF HAMEED


48

Deletion in a queue
void del_q()
{
int item;
node *temp;
if( front == NULL)
{
printf(“Queue is empty”);
temp = front;
item = front→data;
front = front→link;
delete temp;
}

CIRCULAR QUEUE
In a simple queue, there is a possibility that the queue is reported as full ( since
rear has reached the end of the array), even though in actually there might be empty slots
at the beginning of the queue.
To overcome this limitation, we can implement the queue as circular queue. In
circular queue as we go on adding elements to the queue and reach the end of the array,
the next element is stored in the first slot of the array ( provided it is free).

Front Rear
Queue is Empty -1 -1

FR
Insert 1 element 6 0 0
F R
Insert 2 elements 6 3 4 0 2
F R
Insert 2 elements 6 3 4 8 5 0 Full 4

F R
Delete 3 elements 8 5 3 4
R F
Insert 3 elements 3 2 1 8 5 3 Full 2

F R
Delete 3 elements 2 1 1 2
FR
Delete 1 element 1 2 2

Queue is Empty -1 -1

Written By: KASHIF HAMEED


49

Addition in a circular queue

void add_cirq( int item)


{
if(( rear ==MAX -1&& front == 0) || ( rear + 1 =front))
{
printf(“Queue is full”);
return;
}
if(rear = MAX -1)
rear = 0;
else
rear++;
arr[rear] = item;
if( front == -1)
front = 0;
}

Deletion in a circular queue

void del_cirq( int item)


{
int data;
if( front == -1)
{
printf(“Queue is empty”);
return;
}
data = arr[front];
arr[front] = 0;
if(front == rear)
front = rear = -1;

else
{
if(front == MAX -1)
front = 0;
else
front++;
}
return data;
}

Written By: KASHIF HAMEED


50

DEQUE
It is pronounced as “dequeue” or “deck”. It is short form of double ended queue
and defined as the data structure in which items can be added or deleted at either the front
or rear end but no changes can be made elsewhere in the list. Thus deque is
generalization of both stack and queue.

Insertion
Deletion
A B C D E F
Deletion Insertion

Front Rear

VARIATION OF DEQUE
There are two variations of deque.
1. Input Restricted Deque.
An input restricted deque restricted the insertion of element at one end only but
the deletion of an element can be done at both ends of the deque.

Deletion
Deletion A B C D E F
Insertion

Front Rear

2. Output Restricted Deque.


An output restricted deque restricted the deletion of element at one end only but
the insertion of an element can be done at both ends of the deque.

Deletion
Insertion A B C D E F
Insertion

Front Rear

Written By: KASHIF HAMEED


51

Algorithm to insert values into a deque “DQ” having 5 elements

1. Set front = rear = -1


2. [Enter value to insert]
Input item
3. [Specify front or rear side to insert value]
Input side
4. If side =1 then
[Insert value at the front of the deque]
If front = -1 and rear = -1 then
Rear = front = 0
DQ[front] = item
Else if front > 0 then
Front = front – 1
DQ[front] = item
Else
Print ” No space at front of the deque”
End if
End if
[Insert value from back side]
Else
If front = -1 and rear = -1 then
Rear = front = 0
DQ[rear] = item
Else if rear < 4 then
Rear = rear + 1
DQ[rear] = item
Else
Print ” No space at rear of the deque”
End if
End if
End if
5. Exit

Algorithm to delete values from a deque “DQ” having 5 elements

1. Set front = rear = -1


2. [Specify front or rear side to delete value]
Input side
3. If side =1 then
[Delete value from front of the deque]
If front = rear then
Rear = front = -1
Else if front = 4 then
Front = – 1
Else
Front = front + 1
End if
End if
[Delete value from rear side]
Else
If front = rear then
Front = rear = -1
Else
Rear = rear - 1
End if
End if
4. Exit

Written By: KASHIF HAMEED


52

PRIORITY QUEUES

Priority queue is the collection of elements where the elements are stored
according to their priority level. The order in which the element should get added or
remove is decided by the priority of an element.
Rules:
Following Rules Are Applied To Maintain A Priority Queue.
• The element with the higher priority is processed before any element of low
priority.
• If there are elements with the same priorities, then the element added first in the
queue would get processed first.

Priority queues are used for implementing job scheduling by the operating system where
obs with higher priorities are to be processed first. Another application of priority queues
is simulation systems where priority corresponds to event time.

Write a function to reverse a stack of integers using another queue?

#include<stdio.h>
#include<conio.h>
void pushstack(int);
void popstack();
void printstack();
void pushqueue(int);
void popqueue();
void reversestack();
int front, rear, top, n, i, temp, stack[10], queue[10];
void main()
{
clrscr();
front = top = rear = -1;
printf("Enter the number of elements to be added in stack==");
scanf("%d", &n);
if(n>10)
printf("The number should be between 1 and 10");
else
{
for(i=0; i<noe; i++) {
printf("Enter the element==");
scanf("%d", &temp);
pushstack(temp); }
printstack();
reversestack();
}
getch();
}
/*-------------------------------------------------------------------------------*/

Written By: KASHIF HAMEED


53

void pushstack(int item)


{
top++;
stack[top] = item;
}
/*--------------------------------------------------------------------------------*/
void popstack()
{
temp = stack[top];
top--;
pushqueue(temp);
}
/*--------------------------------------------------------------------------------*/
void printstack()
{
for(i=top; i>=0; i--)
printf("\n Element %d of stack is %d", i, stack[i]);
}
/*--------------------------------------------------------------------------------*/
void reversestack()
{
for(i = top; i>= 0; i--)
popstack();
for(i=0; i<= rear; i++)
popqueue();
printf("\n");
printstack();
}
/*---------------------------------------------------------------------------------*/
void pushqueue(int item)
{
rear++;
queue[rear] = item;
if(front == -1)
front = 0;
}
/*---------------------------------------------------------------------------------*/
void popqueue()
{
temp = queue[front];
if(front == rear)
front = rear = -1;
else
front++;
pushstack(temp);
}

Written By: KASHIF HAMEED


54

TWO DIMENSIONAL ARRAYS


A 2-dimensional array is a collection of elements placed in m rows and n
columns. The syntax used to declare a 2-D array includes two sub scripts, out of which
one specifies the number of rows and the other specifies the number of columns of an
array. These two subscripts are used to reference an element in an array. For example,
arr[3][4] is a 2-D array containing three rows and four columns and arr[0][2] is an
element placed at 0th row and 2nd column in the array. The two dimensional array is also
called matrix and table.

12 3 32 2
34 8 7 4
82 81 5 6

Row major and column major order


Rows and columns of a matrix are only a matter of imagination. When a matrix
gets stored in a memory all elements of it are stored linearly since computer’s memory
can only be viewed as consecutive units of memory. This leads to two possible
arrangements of elements in memory-Row major arrangement and memory-Column
major arrangement.

int a[3][4] = {
{12,3,32,2},
{34,8,7,4},
{82,81,5,6},
};

Row major Order

12 3 32 2 34 8 7 4 82 81 5 6
502 504 506 508 510 512 514 516 518 520 522 524

Column Major Order

12 34 82 3 8 81 32 7 5 2 4 6
502 504 506 508 510 512 514 516 518 520 522 524

In Row Major Order, in general for an array a[m][n] the address of element a[i][j] would
be base address + i * n + j
e.g element 4 is present at a[1][3]. Hence location of 4 would be
= 502 + 1 * 4 + 3
= 502 + 7 (i.e 7 * 2 =14)
= 516

In Column Major Order, in general for an array a[m][n] the address of element a[i][j]
would be base address + j * m + i
e.g element 4 is present at a[1][3]. Hence location of 4 would be
= 502 + 3 * 3 + 1
= 502 + 10 (i.e 10 * 2 =20)
= 522

Written By: KASHIF HAMEED


55

ALGORITHMS
Inserting a value at specified location in an array.
This algorithm insert a value M at location loc in array A having N elements.
1) Input value in M.
2) Input position in loc
[Move elements one step forward]
3) Repeat step 4 to 5 while N > = loc
4) A [N+1] = A [N]
5) N =N -1
[End of step 3 loop]
6) [Insert value M at position loc]
A[loc] = M
7) Exit
Deleting an item from an array at specified location
This algorithm deletes a value at location K in array A having N elements.

1) Input location K
2) if K > N then
Print “Invalid location”
Return
End If
[Move each element from the specified location one step towards the beginning. The
item at the specified location is automatically deleted]
3) Repeat for C = K to N-1
A[C] = A[C+1]
[End of loop]
4) Exit
Search a sub string in a string
This algorithm search a substring “sub” in a string “str”
1) Input string in str.
2) Input substring in sub.
3) Find length of str in len.
4) Find length of sub in sublen.
5) Compute total sub strings in str.
6) T = len – sublen + 1
7) C = 0
8) Repeat step 9 to 10 while C <= T
9) Repeat step 10 for X = 0 to sublen
10) If X = sublen then
Print “sub string found at position =”, C +1
Exit
End If
If sub[X] != str[C + X] then
C=C+1
Go to step 8
End If
[End of step 9 inner loop]
[End of step 8 outer loop]
11) Print “ sub String not found”
12) Exit

Written By: KASHIF HAMEED


56

Computing the length of string


1) Input string in variable str.
2) C = 0
3) Repeat step 4 while str[C] ! = NULL
4) C = C + 1
[End of step 3 loop]
5) Print “Length of string is =” , C
6) Exit

Written By: KASHIF HAMEED

You might also like