0% found this document useful (0 votes)
4 views31 pages

2023 INTRODUCTION To Data Structure

Uploaded by

Collins Ifeanyi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views31 pages

2023 INTRODUCTION To Data Structure

Uploaded by

Collins Ifeanyi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 31

FUNDAMENTALS OF DATA STRUCTURE

DATA
Data is a basic fact about an entity or concept presented in a formalized manner
suitable for communication, interpretation and processing either manual or automatic.
It is basic elementary facts about entities or events. It is the raw material for any
processing procedure. The term data refers to qualitative or quantitative attributes of a
variable or set of variables. Data (plural of “datum”) is typically the result of
measurements and can be the basis of graphs, images, or observations of a set of
variables. Data is often viewed as the lowest level of abstraction from which
information and then knowledge is derived and this sense (Data can be described as
some abstraction simplification of reality).
A data is a single unit of values. It is a raw fact, which becomes information after
processing. Data items for example, date is called group items if it can be divided into
subsystems. The date, for instance is represented by day, month and year; each is
called an elementary item, because it cannot be sub-divided into sub-items. It is
indeed treated as a single item.
An entity is used to describe anything that has certain attributes or properties, which
may be assigned values.

Structure :- a representation of data is called Structure. Examples of structure Graph,


Arrays, List etc.
Data Attributes
Data have the following attributes
Data name: the name is the used named within a program or database system to
describe or refer to a data item. For example surname, age etc.
Data value: This is the sequence of character stored up as the data. For example a
data name “SURNAME” may have data value value “SULEIMAN”.
Data Range: Data range is a set of values, which the data can assume or from which
the data value can be derived. (All possible values that could be assigned to a given
attribute of an entity set). For example, data name AGE for human being has a range
of 0 to may be 120 or 150. It can’t be negative or string.
1
Data Type
A type is a set of values and a set of operations operating on those values. There are
three categories of types in most programming language.
Base types (Numeric integer, real, double, char, Boolean) etc. These are the primitive
types of provided more or less directly by the underlying machine.
Compound types (arrays, pointer, records, structs, union, classes, and so on). These
types are constructed from aggregation of the base types. These two broad types (Base
and Compound) may be referred to as standard primitive data types.
User defined type: Some compilers made provision for the user to define his own
types. The format for its definition may vary from programming language to
programming language, but the general method used include what is called
enumeration which attempts to list all the values that the can assume.
Examples of use define types may include:
Type shape = (rectangle, square, ellipse, circle)
Type color = (red, yellow, green)
Type sex = (Male, female)
Type weekday = (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday,
Sunday).
Type currency = (fran, mark, pound, dollar, shilling, lira, guilder, krone, ruble,
cruzeiro, yen).
Type destination = (hell, purgatory, heaven)
Purpose of type information
Type information has 4 purposes:
1. It allows us to associate meaning to sequence of bit in the computer memory.
This is because all data and instructions are store in the same manner as
sequence of bits.
2. It is useful during program development to improve readability and debugging.
3. It helps simplify implementation, e.g. it is easier and more efficient for
implementations to allocate storage for integers only, rather that arbitrary
value.
4. It allows checking for compatibility between operation and operands before
execution.
2
Units of Data
 Character is the smallest unit of information. It includes letters, digits, and special
symbols such as + (plus sign), _ (minus sign, /, /, $, a, b…z, A, B…Z etc. every
character requires one byte of memory unit for storage in computer system.
 Field is consecutive storage position of values. It is unit of data within a record
e.g student’s number, name, and age. In a database concept fields are usually in
columns of a given table. A field is also a category under which a group of data
can be classified and stored.
 A record is a collection of logically related data fields; e.g Data relating to
students in students file. In a database table records are usually in rows.
 A file is a collection of logically related records; e.g students file, stock file ETC.

Basic Terminology
 Data − Data are values or set of values or anything to give information is
called data. Example Student Name, Student Roll Number
 Data Item − Data item refers to single unit of values.
 Group Items − Data items that are divided into sub items are called as Group
Items.
 Elementary Items − Data items that cannot be divided are called as
Elementary Items.
 Attribute and Entity − An entity is that which contains certain attributes or
properties, which may be assigned values.
 Entity Set − Entities of similar attributes form an entity set.
 Field − Field is a single elementary unit of information representing an
attribute of an entity.
 Record − Record is a collection of field values of a given entity.
 File − File is a collection of records of the entities in a given entity set.
 Structure :- a representation of data is called Structure. Examples of structure
Graph, Arrays, List etc.

Data Structure is a way to store and organize data so that it will be used efficiently
(better ways). It is a way of organizing all data items and relationship to each other.

Name Roll Number Branch

John 02 IT

3
NUMERIC STRUCTURE
A number is a mathematical object used in counting and measuring. A notational
symbol, which represents a number, is called a numeral, but in common usage the
word number is used for both the abstract and the symbol, as well as for the word for
the number. In addition to their use in counting and measuring, numerals are often
used for labels (telephone numbers), for ordering (serial numbers), and for codes
(ISBNS). In mathematics, the definition of numbers has been extended over years to
include such numbers as zero, negative numbers, rational numbers, irrational
numbers, and complex numbers.
In computing, a number or numeric structure refers to a data structure that is made of
digits only and used for arithmetic operation. With this definition, the ISBN numbers
and labels are not applicable. A number system is defined by the system. Each digit
in a specific number has a position value and an absolute value.

Classes of Numbers
Numbers can be classified into sets, called number systems. (for different methods of
expressing numbers with symbols, such as the Roman numerals, see numeral
systems).
i. Natural Numbers: The most familiar numbers are the natural numbers or
counting numbers: one, two, three and so on. In the base ten (Decimal) number
system, which is the system used universally today for arithmetic operations,
the symbol for natural numbers are written using ten digits: 0, 1, 2, 3, 4, 5 6, 7,
8, and 9. The rightmost digit of a natural number has a place value of one, and
every other digits has a place value ten times that of the place value of the digit
to its right. The symbol for the set of all natural numbers is N, also written N.
ii. Integers: Integer is a broader class of number that includes the natural
numbers, the negatives of the natural numbers and zero. Negative numbers are
numbers that are less than zero. They are the opposites of positive numbers.
For examples, if the positive number indicates a bank deposit, then a negative
number indicates a withdrawal of the same amount. Negative numbers are
usually written a negative sign (called a minus sign) in front of the number they

4
are the opposite of. Thus the opposite or negative of 7 is written -7. The set of
integers is represented by Z (German Zahl, plural Zahlen), also written Z.
iii. Rational Numbers: A set of rational numbers include integers, positive or
negative and the fractional numbers. A rational number is a number that can be
expressed as a fraction with an integer numerator and a natural number
denominator. Two different fractions may correspond to the same rational
number e.g ½ and 3/6. The symbol for rational numbers is Q (the Quotient). A
rational number expressed in decimal either terminates or perpetually recurs. A
number that is not in the set of rational number is referred to as irrational
numbers.
iv. Real Number: The real number includes both the rational numbers and
irrational numbers. The real numbers include all of the measuring numbers.
Real number can be located on the number line. Real numbers are usually
written using decimal numeral in which a decimal point is placed to the right of
the digit with place value one. Each digit to the right of the decimal point has a
place value one-tenth of the place value of the digit to the left. Thus:
123.456
Represents 1 hundred, 2 tends, 3 ones, 4 tenth, 5 hundredth and 6 thousandth.
Every real number corresponds to a point on the number line. The real number
also has an important and highly technical property called the least upper
bound property. The symbol for the real number is R.
v. Complex Number: Moving to a greater level of abstraction, the real number
can be extended to the complex numbers. This set of number arose historically,
from the question of whether a negative number can have a square root. This
led to the invention of a new number, the square root of negative one, denoted
by i (or j), a symbol assigned by Leonard Eula, and called the imaginary unit.
The complex numbers consist of all numbers of the form.
a +bi
Where a and b are real numbers. In the expression a + bi, the real number a is
called the real part and bi is called the imaginary part. If the real part of a

5
complex number is zero, then the number is called an imaginary number or is
referred to as purely imaginary. If the imaginary part is zero, the number is a
real number. Thus the real numbers are a subset of the complex numbers. The
symbol of the complex number is C.

STRING STRUCTURES
The String data structure is the backbone of programming languages and the
building blocks of communication. String data structures are one of the most
fundamental and widely used tools in computer science and programming. They
allow for the representation and manipulation of text and character sequences in a
variety of ways. The string data structure is a powerful tool that can be used to store
and process large amounts of text data, from simple strings to complex sentences.

It is a fundamental data structure that is used in many programming languages to


store and manipulate text-based data. In most programming languages, strings are
implemented as an array of characters, with each character having a unique index
position within the array.

Every programming language consists of a character set. A character set will have
alphabets, numbers and special characters. A finite sequence of zero or more
characters from the character set is called a string. Strings are represented by
enclosing inside quotation marks. A string with length 0 is called an empty string.

STRING STORAGE
A string generally has three types of structures that are used for storing strings.
i. Fixed length structure

ii. Variable length storage

iii. Linked storage

TYPES OF STRING
Different types of string operations are:
1. Substrings
6
2. Indexing
3. Concatenation
4. Length
5. Text Processing Operations
i. Insertion
ii. Deletion
iii. Replacing

SUBSTRINGS
A substring is the basic unit of access in a string. Unlike other array elements,
substrings have their own meaning. To access a substring we need the following
information,
i. Name of the string.
ii. Position of the first character of the substring in the given string.
iii. Length of the substring.

INDEXING
Indexing operation is used to find the position of the first appearance of a string
pattern in a given string. It is also called pattern matching. INDEX operation returns a
0 if there is no such pattern in that string.

CONCATENATION
Let assume A and B are two different strings. A string consisting of characters in A
are followed by characters in B is called concatenation of A and B which is often
represented as A//B.
Suppose string A = ‘Hello’ and B = ‘World’ then A//B will be ‘HelloWorld’.
Suppose a string S=”Evening” then the substring of S will be ‘Ev’, ‘ni’, ‘ng’ which
can be labeled as P = ‘Ev’ Q= ‘ni’ R = ‘ng’ then concatenation will S=P//Q//R
(S=’Evening’)

LENGTH
The number of characters in a string is called the length of that string. The length of
S= ‘Evening’ will be S = 7

TEXT PROCESSING OPERATIONS


The processing of printed matter such as letters and articles are called word
processing. Basic operations associated with word processing are,
i. Replacement
ii. Insertion
iii. Deletion
7
Word processing operations can be done using string operations.

Insertion: Insertion is the introduction of a string into the body of another string,
starting at a particular position. The syntax of insertion is as below:

INSERT (TEXT, POSITION, STRING) operation inserts STRING in TEXT so that


STRING begins in POSITION.

For example INSERT (‘ABCDE’, 2,’PQR’) = ‘APQRBCDE’

Deletion: Deletion is the opposite of insertion. It is the removal of a substring from


the string. The string length is shortened by substring removed. Deletion is sometimes
referred to as string removal. The syntax is of deletion is shown below:
DELETE (TEXT, POSITION, LENGTH) delete the substring which begins at
certain POSITION and has length shortened.
For example DELETE (‘ABCDEF’, 2, 3, 4) = ‘AEF’
Replacing: Replacement can be seen as removal of a substring and insertion of
another string. Replacement is represented by:
REPLACE (TEXT, PATTERN1, PATTERN2) replaces the first occurrence of
PATTERN1 by PATTERNP2 in TEXT.
For example REPLACE (‘PQRSTU’,’RS’,’X’) = PQXTU

String functions and its meaning


1) strlen() - Returns the string's length.
2) strlwr() - This command lowercases a string.

3) istrupr() - It transforms a string to uppercase .

4) strcat() - appends one string to the end of another.

5) strncat() - This command appends the first n characters of a string to the end
of another string.

6) strcpy() - to copy a string into another string.

7) strncpy() - This command copies the first n characters of a string into another.
8
8) strcmp() - function that compares two strings.

9) strncmp() - compares two strings' first n characters.

10) strcmpi() - This function compares two strings without regard to case I
indicates that this function ignores case).

11) stricmp() - compares two strings regardless of case (identical to strcmpi).

12) strnicmp() – This function compares the first n characters of two strings. There
is no difference in case.

13) strdup() - This command duplicates a string.

14) strchr() - Finds the first instance of a character in a string.

15) strrchr() - Returns the position of a given character in a string.

16) strstr() - Looks for the first instance of a string in another string.

17) strset() - This command changes all characters in a string to a specific


character.

18) strnset() - This command changes the first n characters of a string to a specific
character.

19) strrev() - It reverses a string

Advantages of String
i. Text Processing: Strings are used to represent text in programming languages.
They can be used to manipulate and process text in various ways, such as
searching, replacing, parsing, and formatting.
ii. Data Representation: Strings can be used to represent other data types, such
as numbers, dates, and times. For example, you can use a string to represent a
date in the format “YYYY-MM-DD”, or a time in the format “HH:MM:SS”.

9
iii. Ease of Use: Strings are easy to use and manipulate. They can be concatenated,
sliced, and reversed, among other things. They also have a simple and intuitive
syntax, making them accessible to programmers of all skill levels.
iv. Compatibility: Strings are widely used across programming languages,
making them a universal data type. This means that strings can be easily
transferred between different systems and platforms, making them a reliable
and efficient way to communicate and share data.
v. Memory Efficiency: Strings are usually stored in a contiguous block of
memory, which makes them efficient to allocate and de-allocate. This means
that they can be used to represent large amounts of data without taking up too
much memory.

Disadvantages of String
i. Memory Consumption: Strings can consume a lot of memory, especially
when working with large strings or many strings. This can be a problem in
memory-constrained environments, such as embedded systems or mobile
devices.
ii. Immutability: In many programming languages, strings are immutable,
meaning that they cannot be changed once they are created. This can be a
disadvantage when working with large or complex strings that require frequent
modifications, as it can lead to inefficiencies and memory overhead.
iii. Performance Overhead: String operations can be slower than operations on
other data types, especially when working with large or complex strings. This
is because string operations often involve copying and reallocating memory,
which can be time-consuming.
iv. Encoding and Decoding Overhead: Strings can have different character
encodings, which can lead to overhead when converting between them. This
can be a problem when working with data from different sources or when
communicating with systems that use different encodings.

10
v. Security Vulnerabilities: Strings can be vulnerable to security vulnerabilities,
such as buffer overflows or injection attacks, if not handled properly. This is
because strings can be manipulated by attackers to execute arbitrary code or
access sensitive data.

11
DATA STRUCTURE

The logical or mathematical model of particular organization of data is called its data
structures. A data structure is a specialized format for organizing and storing data. It is
a particular way of organizing data in a computer so that it can be used efficiently. It is
an interrelationship among data elements that determine how data is recorded,
manipulated, stored, and presented by a database.
Data structures can implement one or more particular abstract data types, which are
the means of specifying the contract of operations and their complexities. Different
kinds of data structures are suited to different kinds of applications, and some are
highly specialized to specific tasks.

Data structure provides a means of managing a large amount of data efficiently for
uses such as large databases, and internet indexing services. Usually, efficient data
structures are keys to designing efficient algorithms. Some formal design methods and
programming languages emphasize data structures, rather than algorithms, as the key
factor in software design.

Data structure is an agreement about how to store a collection of objects in memory,


what operations we can perform on that data, the algorithms for those operations, and
how time and space efficient those algorithms are. An abstract data structure is a
conceptual organization without regard to how data is organized on the machine.

Data Structure is a systematic way to organize data in order to use it efficiently.


Following terms are the foundation terms of a data structure.
 Interface − Each data structure has an interface. Interface represents the
set of operations that a data structure supports. An interface only
provides the list of supported operations, type of parameters they can
accept and return type of these operations.
 Implementation − Implementation provides the internal representation
of a data structure. Implementation also provides the definition of the
algorithms used in the operations of the data structure.

12
Characteristics of a Data Structure
 Correctness − Data structure implementation should implement its
interface correctly.
 Time Complexity − Running time or the execution time of operations
of data structure must be as small as possible.
 Space Complexity − Memory usage of a data structure operation should
be as little as possible.

Main functions of data structures


1. Seek to identify and develop entities, operations and appropriate classes of
problems to use them.
2. Determine representations for abstract entities to implement abstract operations
on concrete representations.

Need for Data Structure


As applications are getting complex and data rich, there are three common problems
that applications face now-a-days.
 Data Search − Consider an inventory of 1 million(106) items of a store. If the
application is to search an item, it has to search an item in 1 million(106) items
every time slowing down the search. As data grows, search will become
slower.
 Processor Speed − Processor speed although being very high, falls limited if
the data grows to billion records.
 Multiple Requests − As thousands of users can search data simultaneously on
a web server, even the fast server fails while searching the data.
To solve the above-mentioned problems, data structures come to rescue. Data
can be organized in a data structure in such a way that all items may not be
required to be searched, and the required data can be searched almost instantly.
Examples of structured data
 Dates and Times
 Cell phone numbers
 Banking transaction information
 Customers names, postal addresses and email addresses.
 Credit card Numbers

Areas in Which Data Structures Are Applied Extensively?


 Compiler Design, Operating System,
 Database Management System, Statistical analysis package,
 Numerical Analysis, Graphics,
 Artificial Intelligence, Simulation

13
TYPES OF DATA STRUCTURE
Data Structure

Primitive Data Structure Non Primitive Data Structure

Linear Non Linear


Integer Float Character Data Structure Data Structure
Structure
Array Trees
Stacks Graph

Queue

Link
List

TYPES OF DATA STRUCTURES


Data structures are broadly divided into two:
 Primitive Data Structure and
 Non Primitive Data Structure or Abstract Data Structure

Primitive Data Structure: These are basic structures and are directly operated by
machine instruction example integer, float, character.

Non Primitive Data Structure or Abstract Data Structure: These are derived from
the primitive Data Structure it’s a collection of same type or different type primitive
data structure example Array, Stack, Trees, Linked List, Graph, Queue etc.

CLASSIFICATION OF DATA STRUCTURES


Data structures are broadly divided into two:
 Linear Data Structures
 Non-Linear Data Structures.

14
Linear Data Structures
Linear data structures are data structures in which individual data elements are stored
and accessed linearly in the computer memory. A data structure is said to be linear if
its elements form a sequence, or, in other words, a linear list. There are two basic
ways of representing such linear structures in memory. One way is to have the linear
relationship between the elements represented by means of sequential memory
locations. These linear structures are called arrays. The other ways is to have the
linear relationship between the elements represented by means of pointers or links.
These linear structures are called linked lists.

Non-Linear Data Structures


A non-linear data structure, as the name implies, is a data structure in which the data
items are not stored linearly in the computer memory, but data items can be processed
using some techniques or rules. Examples to non-linear structures are trees and
graphs.

TYPES OF ARRAY
There are majorly two types of arrays, they are:
 One dimensional usually called (list or linear array) which has only one value
as its index or subscript.
 Multidimensional (Two dimensional Array). A two dimensional array is called
a table or matrix.
The elements array A, may be denoted as follows;
A1, A2, …, An or
A(1), A(2), …, A (n) or
A[1], A[2], … A[n],
Where 1 is the lower bound (LB) of the array and n the upper bound (UB) of the
array. The simplest type of data structure is a linear array. An array is sometime called
a subscripted variable.

15
The structure of an array
The array is probably the most widely used data structure, in some languages it is even
the only one available. An array consists of components which are all of the same
type, called its base type. It is therefore call a homogeneous structure. The array is a
random-access structure, because all components can be selected at random and are
equally quickly accessible. In order to denote an individual component, the name of
the entire structure is augmented by the index selecting the component. This index is
to be an integer between 0 and n-1, (or 1 and n in some cases) where n is the number
of elements or the size, of the array. An individual component of an array can be
selected by an index.
The use of arrays may allow us to simplify our processing. We can use array to help
read and analyze repetitive data with a minimum of coding. An array and a loop can
make the program smaller.

Array Operations
The operations normally performed on array include:
Traversal: Processing or applying a process or operation to each elements of an array.
Traversal is also a process of visiting each element once.
Search: Finding the location of the element with a given value or the record with a
given key or the position(s) of element(s) that satisfies a particular requirement.

Insertion: Adding a new element to the array. Insertion of an element can be done:
 At the beginning
 At the end and
 At any given index of an array.

Deletion: Removing an element from the array and re-organizing it. You can also do
deletion in different ways:
 At the beginning
 At the end

Sorting: Arranging the elements in some type of order (ascending, descending,


alphabetical etc)

Merging: Combining two arrays to form a single array.

(a) Insertion. Adding a new element to the list.


0 1 2 3
10 20 30 40

10 20 30 40 50

16
0 1 2 3 4
(b) Deletion. Removing an element from the list. 10 20 30 40 50 5
0
0 1 2 3
10 20 30 40 Remove or
Delete

(c) Traversal:-. Processing each element in the list.

(d) Search:-. Finding the location of the element with a given value or the record
with a given key. 0 1 2 3 Index
10 20 30 40

Search 10 = A [2]

(e) Merging:- Combining two lists into a single list.


0 1 2 3
0 1 2 3 4 5 6 7
10 20 30 40
10 20 30 40 50 60 70 80
0 1 2 3
50 60 70 80

(f) Sorting:- Arranging the elements in some type of order.

20, 30, 10, 5, 6, 10


5 6 10 20 30 40

17
INSERTION OPERATION
Insert operation is to insert one or more data elements into an array. Based on the
requirement, a new element can be added at the beginning, end, or any given index of
array. Here, we see a practical implementation of insertion operation, where we add
data at the end of the array −

Algorithm
Let Array be a linear unordered array of MAX elements.
Example Result
Let LA be a Linear Array (unordered) with N elements and K is a positive integer
such that K<=N. Following is the algorithm where ITEM is inserted into the Kth
position of LA –

Array Insertions
In the previous section, we have learnt how the insertion operation works. It is not
always necessary that an element is inserted at the end of an array. Following can be a
situation with array insertion −
 Insertion at the beginning of an array
 Insertion at the given index of an array
 Insertion after the given index of an array
 Insertion before the given index of an array

18
Insertion at the Beginning of an Array
When the insertion happens at the beginning, it causes all the existing data items to
shift one step downward. Here, we design and implement an algorithm to insert an
element at the beginning of an array.

Algorithm for insertion operation at the beginning of an Array


We assume A is an array with N elements. The maximum numbers of elements it can
store is defined by MAX. We shall first check if an array has any empty space to store
any element and then we proceed with the insertion process.

Insertion at the Given Index of an Array


In this scenario, we are given the exact location (index) of an array where a new data
element (value) needs to be inserted. First we shall check if the array is full, if it is
not, then we shall move all data elements from that location one step downward. This
will make room for a new data element.

Algorithm for insertion operation at a given index


We assume A is an array with N elements. The maximum numbers of elements it can
store is defined by MAX.

19
Insertion After the Given Index of an Array
In this scenario we are given a location (index) of an array after which a new data
element (value) has to be inserted. Only the seek process varies, the rest of the
activities are the same as in the previous example.

Algorithm for insertion operation after a given index of an Array


We assume A is an array with N elements. The maximum numbers of elements it can
store is defined by MAX.

Insertion Before the Given Index of an Array


In this scenario we are given a location (index) of an array before which a new data
element (value) has to be inserted. This time we seek till index-1, i.e., one location
ahead of the given index. Rest of the activities are the same as in the previous
example.
Algorithm for insertion operation before a given index of an Array
We assume A is an array with N elements. The maximum numbers of elements it can
store is defined by MAX

20
EXERCISE 1
Suppose A is a linear Array that has N element. Perform an insertion operation of the
element X at the Kth position. Given that N=5, K=7.
A[0] A[1] A[2] A[3] A[4] A[5]
5 3 1 6 4
K A[0] A[1] A[2] A[3] A[4] A[5]
5 3 1 7 6 4
Algorithm for the above insertion operation K
Array.Insert (A, Max-Loc, N, K, Item)
Step 1: i=N–1
Step 2: while K<=i
A (i + 1) = A[i]
i=i–1
Step 3: A[K] = x
Step 4: Set N = N – 1
Step 5: Exit

EXERCISE 2
Suppose A is a linear Array that has N element. Perform an insertion operation of the
element at the position.
Given the following MaxLoc = 12 N=8, Loc=5, ITEM = 100
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] A[10] A[11]
4 11 7 17 22 3 6 2

ITEM
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] A[10] A[11]
4 11 7 17 100 22 3 6 2

Algorithm for the above insertion operation


Array.Insert(A, MaxLoc, n, Loc, ITEM)
If (MaxLoc == n)
{
Print “overflow”
}
For (i = n, i>=Loc, i--)
{
A[i] = a[i – 1]
}
A[Loc] = ITEM
n=n+1
Exit

21
DELETION OPERATION
Deletion refers to removing an existing element from the array and re-organizing all
elements of an array.

Algorithm
Consider LA is a linear array with N elements and K is a positive integer such that
K<=N. Following is the algorithm to delete an element available at the Kth position of
LA.

Exercise 3
Suppose A is a linear Array that has N element. Perform a deletion operation.
Given the following MaxLoc = 12, N=8, Loc=4, ITEM = ?
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] A[10] A[11]
4 11 7 17 22 3 6 2

ITEM = ?
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] A[10] A[11]
4 11 7 22 3 6 2

Algorithm for the above deletion operation


Array.Delete (A, MaxLoc, n, ITEM, Loc)
If (n == 0)
{
Print “overflow”
Exit
}
ITEM = A [Loc]
For (i = (Loc – 1), i<=(n-2), i++)
A[i] = a[i + 1]
n=n-1
Exit

22
EXERCISE 4
Suppose A is a Linear Array that has N elements. Perform deletion operation at Kth
position. Given that N = 5

A[0] A[1] A[2] A[3] A[4]


5 3 1 4 6
K
A[0] A[1] A[2] A[3]
5 3 4 6

SOLUTION
Algorithm for the above deletion operation
Array.delete (A, N,K)
Step 1: X = A[K]
Step 2: I=K+1
Step 3: while i<=upper-bound
A[i-1] = A[i]
i=i+1
Step 4: Set N = N - 1
Step 5: Exit

23
SEARCH OPERATION

You can perform a search for an array element based on its value or its index.

Algorithm
Consider LA is a linear array with N elements and K is a positive integer such that
K<=N. Following is the algorithm to find an element with a value of ITEM using
sequential search.

Exercise 5
Suppose A is a linear Array that has N element. Perform a search operation given that
K = 50?
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7]
A 10 20 30 40 50 86 11 32

Solution
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7]
A 10 20 30 40 50 86 11 32

A[i] = = Key
A [0] == 50 …? Return 0;
A [1] == 50 …? Return 0;
A [2] == 50 …? Return 0;
A [3] == 50 …? Return 0;
A [4] == 50 …? Return 4;

24
EXERCISE 6
Suppose A is a linear Array that has N element. Perform a search operation given that
K = 24?
A[0] A[1] A[2] A[3] A[4] A[5]
A 25 35 6 24 86 34

Solution
A[i] = = Key
A [0] == 4 …? Return 0;
A [1] == 4 …? Return 0;
A [2] == 4 …? Return 0;
A [3] == 24 …? Return 3;

TRAVERSING OPERATION
Processing each element in the list.
Let A be a collection of data elements stored in the memory of the computer. Suppose
we want to either print the contents of each element of A or to count the number of
elements of A with a given property. This can be accomplished by traversing A, that
is, by accessing and processing (frequently called visiting) each element of A exactly
once.

The following algorithm is used to traversing a linear array LA.


As we know already, here, LA is a linear array with lower bound LB and upper
bound UB. This algorithm traverses LA applying an operation PROCESS to each
element of LA.

We also state an alternative form of the algorithm which uses a repeat-for loop instead
of the repeat-while loop. This algorithm traverses a linear array LA with lower bound
LB and upper bound UB.

25
Exercise 7
Let A be a collection of data elements stored in the memory of the computer. Print the
contents of each element of A with a given property.

Lower-bound (UB) Upper-bound (UB)

A[0] A[1] A[2] A[3] A[4] A[5]


A 5 3 1 7 6 4

Here, A is a Linear Array with Lower-bound (LB) and Upper-bound (UB). The
Algorithm traverse A and apply operation PROCESS to each element of A.

Algorithm for the above traversed operation


Step 1: Set i = LB
Step 2: Repeat for = LB to UB
Step 3: Apply PROCESS to A(i)
Step4: Exit

UPDATE OPERATION
Update operation refers to updating an existing element from the array at a given
index.
Algorithm
Consider LA is a linear array with N elements and K is a positive integer such that
K<=N. Following is the algorithm to update an element available at the K th position of
LA.

26
DEPARTMENT OF COMPUTER
SCIENCE
FACULTY OF PURE AND APPLIED SCIENCES
FEDERAL UNIVERSITY WUKARI
First Semester 2018/2019 Session

Course Code: CSC 203 Time Allowed: 2 Hours 30MINS


Course Title: FUNDAMENTALS OF DATA STRUCTURES (3 Credit Units)

MARKING GUIDE

EXERCISES
i. What is a data structure?
------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------
ii. What are some applications of data structures?
------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------
-------------------------------------------------
iii. Why is data structure important?
------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------
--------------------------------------------------------------------
iv. 1. Define Data
v. 2. What do you mean by Data Item, Elementary Data Item and Group Item?
vi.
vii. Q1. List out the areas in which data structures are applied extensively?
viii. Ans: Compiler Design, Operating System, Database Management System,
Statistical analysis package, Numerical Analysis, Graphics, Artificial Intelligence,
Simulation
ix.
x. Q2. What are the major data structures used in the following areas : RDBMS,
Network data model & Hierarchical data model. ?
xi. Ans: The major data structures used are as follows:
xii. •RDBMS - Array (i.e. Array of structures)
27
xiii. •Network data model - Graph
xiv. •Hierarchical data model – Trees

2a. State the algorithms for implementing insertion and deletion operations on a queue. (6
marks)
Algorithm for implementation of insertion operation on a queue.
procedure INSERT Q (Q, n, ITEM, REAR)
/*insert item ITEM into Q with capacity n*/
if (REAR = n) then QUEUE_FULL;
REAR = REAR + 1; /*increment REAR*/
Q [REAR} = ITEM; /*insert ITEM as the rear element*/
end INSERT Q
Algorithm for implementation of deletion operation on a queue.
procedure DELETE Q (Q, FRONT, REAR, ITEM)
if (FRONT +REAR) then QUEUE_EMPTY;
FRONT = FRONT + 1;
ITEM = Q [FRONT];
end DELETION Q
4c. States six (6) language design principles. (6 marks)
Language design principles are Simplicity, Regularity, Orthogonality, Type completeness,
Parameterization, Analogy, Correspondence, Clarity, Referential, Sub types, Expressivity,
Extensibility, Safety, Information, Explicit, Privacy, Abstraction, Qualification,
Representation, Generalization, Efficiency and Modularity.

28
DEPARTMENT OF COMPUTER
SCIENCE
FACULTY OF PURE AND APPLIED SCIENCES
FEDERAL UNIVERSITY WUKARI
First Semester 2018/2019 Session

Course Code: CSC 203 Time Allowed: 2 Hours 30MINS


Course Title: FUNDAMENTALS OF DATA STRUCTURES (3 Credit Units)

What does it mean to say a computer have its own unique instruction set..?
It means that a machine – language program for one type of computer cannot be run
on another type of computer without significant alterations.

In terms of software trends list 2 characteristics of each Generation of programming


language
1st generation Software Trend User written Programs, Machine languages
2nd Generation Software Trend Packaged Programs, Symbolic Language
3rd Generation Software Trend Operating Systems, High Level Language
4th Generation Software Trend Database managing sys, Microcomputer
packages
5th Generation Software Trend Natural Language, Expert Assisted
packages, Multipurpose graphics

List out the various basis on which a Programming language can be classified.?
- Intended applications areas such as business, numerical, simulation etc.
- Whether the language is to be compiler or interpreter.
- Whether the processing required is, Sequential or Parallel.
- Whether the language is deterministic or not.
- The nature of programming language such as Procedural, functional,
declarative, object – oriented.

29
- Data structures such as lists, arrays, records, graphs, strings etc.

List 2 advantages and 2 disadvantages of procedural language


- The program statements is English lie and so easier to work with.
- Because of their Enlish like nature, less time is required to program a
problem.
- Once coded programs are easier to understand and modify.
- The programming language are machine independent
Disadvantages
Program execution is slow
The languages use computer resources less efficiently

Give 5 comparison of the Machine Language, Assembly Language and High Level
Language.
The High Level Language are sub – divided into three generations name them.
3rd Generation or Procedural Language
4th Generation or Problem Oriented Language
5th Generations or Natural Language

List 6 characteristics attributes of high level language


Machine independent
English command and keywords
Fewer instructions
Detailed knowledge of internal working is not required ‘standardized and portal
Better self documentation is possible
It is Easier to write and understand
Symbolic object – oriented programming allows for interfaces and interaction
Modular and object oriented programming allows for interfaces and interaction.

30
The portability of the programs allows for its convenient and commercial
The programs allow for deletion of erro and correction

31

You might also like