DS. Unit I

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

DEPARTMENT OF COMPUTER SCIENCE

Dr.L.Bullayya college : Visakhapatnam


ANDHRA PRADESH STATE COUNCIL OF HIGHER EDUCATION
(A Statutory body of the Government of Andhra Pradesh)
3rd,4th and 5th floors, Neeladri Towers,Sri Ram Nagar, 6th
Battalion Road, Atmakur(V), Mangalagiri(M), Guntur-522 503,
Andhra Pradesh Web:www.apsche.org Email:
acapsche@gmail.com

RREEVVIISSEED
D SSYYLLLLA
ABBU
USS O
OFF B
B..SScc.. ((C
COOM
MPPU
UTTEER
R SSC
CIIEEN
NCCEE// IIN
NFFO
ORRMMA
ATTIIO
ON N
TTEEC
CHNOLOGY) UNDER CBCS FRAMEWORK WITH EFFECT FROM 2020-2002211
H N O L O G Y ) U ND ER C B C S F R A M E WO R K W IT H E FFE C T FR O M 202 0- 2

STRUCTURE OF COMPUTER SCIENCE /INFORMATION TECHNOLOGY (IT)

B.Sc. with Computer Science as one of the Core Subjects


Discipline : Computer Science

Hrs.
PAPER
YEAR SEM. SUBJECT per Credits IA ES Total
CODE
Week
I C1 Problem Solving in C 4 3 25 75 100

I C1-P Problem Solving in C Lab 2 2 50 50


First
Year II C2 Data Structures using C 4 3 25 75 100

Data Structures using C


II C2-P 2 2 50 50
Lab
III C3 Database Management 4 3 25 75 100
System
III C3-P Database Management 2 2 50 50
Second System Lab
Year C4 Object Oriented 4 3 25 75 100
IV
Programming using Java
Object Oriented 2 2 50 50
IV C4-P Programming using Java Lab

V C5 Operating Systems 4 3 25 75 100


Third
V C5-P Operating Systems Lab 2 2 50 50
Year
using C/Java
DEPARTMENT OF COMPUTER SCIENCE
Dr.L.Bullayya college : Visakhapatnam
I B.SC. COMPUTER SCIENCE - II Semester : SYLLABUS

Semester Course Code Course Title Hours Credits


II C2 DATA STRUCTURES USING C 60 3

Unit I
Introduction to Data Structures : Introduction to the Theory of Data Structures, Data
Representation, Abstract Data Types, Data Types, Primitive Data Types, Data Structure and
Structured Type, Atomic Type, Difference between Abstract Data Types, Data Types and Data
Structures, Refinement Stages.

Unit II
Linked Lists : Introduction to Lists and Linked Lists, Basic Linked List Operations, Doubly Linked List,
Circular Linked List, Linked List versus Arrays.

Unit III
Stacks: Introduction to Stacks, Stack as an Abstract Data Type, Representation of Stacks through
Array, Representation of Stacks through Linked Lists, Stacks and Recursion.
Queues: Introduction, Queue as an Abstract data Type, Representation of Queues, Circular Queues,
Double Ended Queues- Deques, Priority Queues, Application of Queues.

UNIT - IV
Binary Trees: Introduction to Non- Linear Data Structures, introduction Binary Trees, Types of Trees, Basic Definition
of Binary Trees, Properties of Binary Trees, Representation of Binary Trees, Operations on a Binary Search Tree,
Binary Tree Traversal, Counting Number of Binary Trees, Applications of Binary Tree

UNIT - V
Searching and sorting: Sorting - An Introduction, Bubble Sort, Insertion Sort, merge Sort, Searching - An
Introduction, Linear or Sequential Search, Binary Search, Indexed Sequential Search.
Graphs: Introduction to Graphs, Terms Associated with Graphs, Sequential Representation of Graphs, Linked
Representation of Graphs, Traversal of Graphs, Spanning Trees, Shortest Path, Application of Graphs.

Text 8ooks:
1. “Data Structures using C", ISRD group Second Edition, TPH
2. “Data Structures through C”, Yashavant Kanetkar, BPB Publications

2
DEPARTMENT OF COMPUTER SCIENCE
Dr.L.Bullayya college : Visakhapatnam
Semester Course Code Course Title Hours Credits
II C2-P DATA STRUCTURES USING C LAB 30 2

LAB LIST
1. Write a program to read N numbers into an array and perform the following on an array

a. Add an element at the begging of anarray


b. Insert an element at given index ofarray
c. Update a element using a values and index
d. Delete an existing element
2. Write a program using stacks to convert a given

a. Postfix expression to prefix


b. Prefix expression to postfix
c. infix expression to postfix
3. Write Programs to implement the Stack operations using an array
4. Write Programs to implement the Stack operations using Liked List.

S. Write Programs to implement the Queue operations using an array.

6. Write Programs to implement the Queue operations using Liked List.


7. Write a program for arithmetic expression evaluation.
8. Wr1te a program for Binary Search Tree Traversals
9. Wr1te a program to implement dequeue using a doubly linked list.
10. Write a program to search an item in a given list using the following Searching Algorithms
a. Linear Search
b. Binary Search.

11. Write a program for implementation of the following Sorting Algorithms


a. Bubble Sort
b. Insertion Sort
c. Quick Sort

12. Write a program for polynomial addition using single LinkedList


13. Write a program to find out shortest path between givenSource Node and Destination Node in a
given graph using Dijkstrar’s algorithm.
14. Write a program to implement Depth First Search graph traversals algorithm

15. Write a program to implement Breadth First Search graph traversals algorithm

3
UNIT I
Introduction to Data Structures : Introduction to the Theory of Data Structures, Data
Representation, Abstract Data Types, Data Types, Primitive Data Types, Data Structure and
Structured Type, Atomic Type, Difference between Abstract Data Types, Data Types and Data
Structures, Refinement Stages.

DATA STRUCTURES

Data Structure is a way of organizing the data, not only stored items and also stores its
relationship.
(or)
A data may be organized in many ways such as the Logical (or) mathematical
representation of given data is called as "data structure".
(or)
The method to store the information and computes the memory is collectively known as
Data structure.
Data Structure can be defined as the group of data elements which provides an efficient
way of storing and organizing data in the computer, so that it can be used efficiently.
Some examples of Data Structures are arrays, Linked List, Stack, Queue, etc.
Data Structure plays a very important role in various algorithms, it allows the
programmer to handle the data efficiently.

NEED OF THE DATA STRUCTURE


1. A data structure helps us to understand the relationship of one data element With other.
2. Data structure helps us to analyze the data in a logical or mathematical manner.
3. Data structure helps us to store the data and organize in logical or mathematical
manner
4. Modern day computing problems are complex and large. So, there is a need to handle
large amount of data which is overcome using data structure.
5. The collecting of data and link dynamically over time is organized by using various
efficient algorithms in data structure.

CHARACTERISTICS OF A DATA STRUCTURE


1. Correctness
Data structure implementation should implement its interface correctly.
2. Time Complexity
Running time or the execution time of operations of data structure must be as small
as possible.
3. Space Complexity
Memory usage of a data structure operation should be as minimal as possible.

4
OPERATIONS ON DATA STRUCTURES
The operations involve in data structure are as follows.
1. Create
Used to allocate/reserve memory for the data element(s).
2. Destroy
This operation deallocate/destroy the memory space assigned to the specified data
structure.
3. Selection
Accessing a particular data within a data structure.
4. Update
For Updation (insertion or deletion) of data in the data structure.
5. Searching
Used to find out the presence of the specified data item in the list of data
6. Sorting
Process of arranging all data items either in ascending or in descending order.
7. Merging
Process of combining data items of two different sorted lists of data items into a
single list.
8. Traversing
Traversing the data structure means visiting each element of the data structure in
order to perform some specific operation like searching or sorting.
Example: If we need to calculate the average of the marks obtained by a student in 6
different subject, we need to traverse the complete array of marks and calculate the total
sum, then we will divide that sum by the number of subjects i.e. 6, in order to find the
average.
9. Insertion
Insertion can be defined as the process of adding the elements to the data structure at
any location. If the size of data structure is n then we can only insert n-1 data
elements into it.
10. Deletion
The process of removing an element from the data structure is called Deletion. We can
delete an element from the data structure at any random location.

ADVANTAGES OF DATA STRUCTURES


We need data structures because there are several advantages of using them, few of them are
as follows:

1. Data Organization:
We need a proper way of organizing the data so that it can accessed efficiently when we
need that particular data. DS provides different ways of data organization so we have
options to store the data in different data structures based on the requirement.

2. Efficiency
The main reason we organize the data is to improve the efficiency. We can store the
data in arrays then why do we need linked lists and other data structures? because
when we need to perform several operations such as add, delete update and search on
arrays, it takes more time in arrays than some of the other data structures. So, the fact
that we are interested in other data structures is because of the efficiency.

5
3. Reusability
Data structure can be reused. Once we have implemented a data structure, the same
data structure can be used later.

4. Abstraction
Data structure hides (abstracts) the implementation details from the user. The user
access the data through an interface and enjoys the benefits of data structures, the
important complex implementation details are hidden from the user.

DATA REPRESENTATION
Data Representation refers to the form in which data is stored, processed, and
transmitted. Devices such as smart phones, iPods, and computers store data in digital
formats that can be handled by electronic circuitry.

Integer Representation
An integer is the basic data type which is commonly used for storing negative as
well as non-negative integer numbers. The non-negative data is represented using
binary number system. In this, each bit position represents the power of 2. The
rightmost bit position represents 20 which is 1, the next represents 21 which is 2,
then 22 which is 4 and so on.

Real Number Representation


The method used to represent real numbers in computers is floating-point
notation. In this notation, the real number is represented by a number called a
mantissa, times a base raised to an integer power, callcd an exponent.

Character Representation
The information in computers is not always interpreted numerically. For example,
to store the salary of an employee we use the integer representation of the data
but with salary we also need to store the name of the employee which requires a
different data representation. Such information is usually represented in character
string form. There are different codes available to store data in character form such
as BCD, EBCDIC and ASCII.

POPULAR TEXT CODES


In general, there are 4 popular text codes are available to represent the data, they are
1. EBCDIC
2. ASCII
3. Extended ASCII
4. UNICODE
In the system of text codes, numbers would represent the letters of the alphabet,
punctuation marks and other symbols.

The four most popular text codes are

EBCDIC
EBCDIC stands for Extended Binary Coded Decimal Interchange Code. EBCDIC is an eight –
bit code that defines 256 symbols. It is still used in IBM mainframe and midrange systems.

6
ASCII
ASCII (pronounced As-key) stands for the American Standard Code for Information
Interchange. Today, the ASCII character set is by far the most commonly used in computers of
all types. ASCII is an eight-bit code that specifies characters for values from 0 to 127.

Extended ASCII
Extended ASCII is an eight-bit code that specifies the characters for values form 128 to 255.
The first 40 symbols represent pronunciation and special punctuation. The remaining symbols
are graphic symbols.

Unicode
The Unicode worldwide character standard provides up to four bytes – 32 bits – to represent
each letter, number, or symbol, with four bytes, enough Unicode codes can be created to
represent more than 4 billion different characters or symbols.
This total is enough for every unique character and symbol in the world, including the
vast Chinese, .Korean, and Japanese character sets. One major advantage of Unicode over
other text codes is its compatibility with ASCII codes.

DATA REPRESENTATION AS NUMBER SYSTEMS


The numbers which are representing for counting, calculating and processing fall into 4 types.
In general, we used the numbers from 0-9, which represents as the Decimals. And in
computers, it can only used two numbers, that are 0 and 1, these are called the Binary
Numbers.
For our convenience we have certain other two representations are also available. So,
we have mainly 4 types of Number Systems are available.
They are
1. Binary Number System (0 to 1) Total two numbers
2. Octal Number System (0-7) Total eight numbers
3. Decimal Number System (0-9) Total ten numbers
4. Hexa Decimal Number System (0-15) Total sixteen numbers
(0-9, and A-F)→ (0-F)
1. Binary Number System
This Number System is used only two digits, i.e. Binary Digits – bits- 0,1. These are the digits
which can directly understood by the computer.
These numbers can be represented with the combination of 0’s and 1’s, and can be
base 2, and represented as
(1100100110)2
2. Octal Number System
This Number System is used the numbers from 0 to 7 only, i.e. totally 8 digits are used for
forming the numbers. These can be represented with the combination of 0 to 7 digits only, and
the base is 8, can be represented as
(123)8, (376)8
3. Decimal Number System
This Number System is used by the humans, this uses the numbers from 0 to 9, i.e. totally 10
digits are used for forming the numbers.
These numbers have base 10, but by default we are using these numbers, so we need
not mention the base for Decimal Number System. This can be represented as
345, 987, 36, 9 or (69)10

7
4. Hexa Decimal Number System
This Number System is used the Numbers from 0 to 15, i.e. totally 16 digits, here we used to
represent the numbers from 0 to 9, and 10-A, 11-B, 12-C, 13-D, 14-E, 15-F. So, the numbers
0-F are used. These numbers having the base 16, and can be represented as
(789)16, (90876)16, (A98F)16

COMPUTERS USE THE BINARY NUMBER SYSTEM


In a computer all data is represented by the state of the computer’s electronic switches. A
switch has only two possible states –on and off – so it can represent only two numeric values.
To a computer, when a switch is off, it represents 0, when a switch is on, it represents 1.
Because there are only two values, computers are said to function in base 2, which is also
known as the binary system.

BITS AND BYTES


A value is represented by each switch’s state – whether the switch is turned on or off – is
called a bit (binary digit). A bit is the smallest possible unit of data a computer can recognize or
use to represent anything meaningful, the computer uses bits in groups.
A group of eight bits is called as byte. Half of a byte is called as nibble. With one byte,
the computer can represent one of 256 different symbols or characters because the eight 1s
and 0s can be combined in 256 different ways.
The byte is an extremely important unit because those are enough different eight bit
combinations to represent all the characters on the keyboard, including all the letters, numbers,
punctuation marks, and other symbols.
The least significant memory location is called BIT.
The least significant memory measurement is called BYTE,
So, the measurement of the storage capacity will be done using Bytes.
A Byte can be represented as combination of 8 bits.

Byte →
8 bits

Unit Equals
4 bits 1 Nibble
8 bits 1 Byte
1024 Bytes 1 Kilo Byte
1024 Kilo Bytes 1 Mega Byte
1024 Mega Bytes 1 Giga Byte
1024 Giga Bytes 1 Terra Byte
1024 Terra Bytes 1 Peta Byte
1024 Peta Bytes 1 Exa Byte
1024 Exa Bytes 1 Zetta Byte
1024 Zetta Bytes 1 Yotta Byte
1024 Yotta Bytes 1 Bronto Byte
1024 Bonto Bytes 1 Geop Byte ( to be continued..)

8
ADT – ABSTRACT DATA TYPE
In computer science, an abstract data type is a mathematical model for data types. An
abstract data type is defined by its behavior from the point of view of a user, of the data,
specifically in terms of possible values, possible operations on data of this type, and the
behavior of these operations.
Abstract Data type (ADT) is a type (or class) for objects whose behavior is defined by
a set of value and a set of operations. ... Think of ADT as a black box which hides the inner
structure and design of the data type. Now we'll define three ADTs namely List ADT, Stack
ADT, Queue ADT.
An Abstract Data Type (ADT) is an abstract concept defined by axioms which
represent some data and operations on that data. Abstract Data Types are focused on what,
not how (they're framed declaratively, and do not specify algorithms or data structures).
Common examples include lists, stacks, sets, etc.
There are two parts to each ADT- Public and Private
1. The public or external part, which consists of:
• The conceptual picture (the user's view of what the object looks like, how the
structure is organized)
• The conceptual operations (what the user can do to the ADT)
2. The private or internal part, which consists of:
• The representation (how the structure is actually stored)
• The implementation of the operations (the actual code)

ADT ( ABSTRACT DATA TYPES )


• ADT stands for Abstract Data Type.
• It is an abstraction of a data structure.
• Abstract Data Type is a mathematical model of a data structure .
• It describes a container which holds a finite number of objects where the object may be
associated through a given binary relationship.
• It is logical description of how the data and operation allowed without regard to how they
will be implemented.
• ADT concerns only with the data is representing and not with how it will eventually be
constructed.
• It is a set of object and operation .foe example: List, Insert, Delete, Search, Sort.
• It consists of following three parts 1.Data 2.Operation, 3.Error
1. Data : It describes the structure of the data used in the ADT.
2. Operation : It describes valid operation for the ADT. It describes its interface.
3. Error : It describes how to deal with the error that can occur.

ADVANTAGES OF ADT
• ADT is reusable and ensures robust data structure .
• It reduces coding efforts.
• Encapsulation ensures that data cannot be corrupted.
• ADT is based on principles of Object Oriented Programming (OOP) and software
Engineering (SE).
• It specific error conditions associated with operation.

9
DATA TYPE
A data type is a set of data values having predefined characteristics. You will have
encountered data types during computer programming. In sense, it describes which type of
data is stored in the variable.
Ex: integer, float, character and Boolean.
Generally in all programming languages we have a limited number of such data types. The
range of values that can be stored in each of these data types is defined by the languages and
the computer hardware that the high level language is used.

Data types can be broken up to two types


I. Scalar or primitive data types
II. Structured or non- primitive data types.
I. Scalar or primitive data type
A simple (scalar) data type consists of a collection of or ordered values and set of operations
that can be performed on those values. The C,C++, and Java programming languages refers
to scalar types primitive data types. A scalar variable can hold only one piece of data at a
time. So in C, C++ , and java scalar data type include int , char , float, and double , along
with others. Scalar variable of the same type can be arranged into ascending or descending
order based on the value.
A. Integer Types
Integer types can hold whole numbers as 286,-32…. The size of the values that can be
stored depends
on the integer data type. Java supports four types of integer as shown below. These can
be positive or negative.

Integer -
Type Size (in bytes) Min. value Max. value
byte 1 -128 127
short 2 -32,768 32,767
int 4 -2,147,483,648 2,147,483,647
-
long 8 9,223,372,036,854,775,8 9,223,372,036,854,775,80
08 8

B. Floating point types


Floating point types can hold numbers containing fractional parts such as 7.5, 9.04, etc..
Floating point
Type Size (in bytes) Min. value Max. value
Float 4 3.4 e-038 3.4 e+038
long 8 1.7 e -308 1.7 e +308

C. Character type
Java provide a character data type called char to store character constants .The char type
assumes a size of 2 bytes but it can hold only a single characters.

D. Boolean type
Boolean type is used when we want to test a particular condition during the execution of
the program. There are only 2 values that Boolean type can take: true or false. Only one
bit is required to store Boolean values.

10
II. Structured or non- primitive data types
Non - primitive data types hold a collection of data values. This collection will generally
consists of the primitive data types.
Example of this would include
ARRAYS,
RECORDS (STRUCTURES),
CLASSES AND
FILES.
These data types, which are created by programmers, are extremely important and are the
building block of data structures.
Non-primitive data types in java languages are arrays, classes, interface etc. These are
also examples of reference data types.

TYPES OF STRUCTURES
There are mainly two types of structures, they are
1. Storage structure: It is the representation of the data structure in the computer
memory.
2. File structure : It is the representation of the storage structure in the auxiliary
memory.

1. STORAGE STRUCTURES
The memory that is allocated to a variable or a constant is stored in the main memory
of the computer that is RAM which gets deleted as soon as the function ends. This
representation of allocating the memory is called Storage Structure.

1. several types of data storage exist in most of the computer systems. They vary in
speed of access, cost per unit of data and reliability.
a) Cache
Most costly and fastest form of storage. Usually very small, and managed by the
operation system. It is placed between CPU and Main Memory.
b) Main Memory
The storage area for data available to be operated on General –purpose
machine instruction operate on main memory.
Contents of main memory are usually lost in a power failure or ”crash”.
Usually too small (even with megabytes) and too expensive.
c) Flash memory
EEPROM ( Electrically Erasable programmable read –only memory)
Data in flash memory survive from power failure.
· Reading data from flash memory takes about 10 nano –sec s (roughly as fast
as from main memory), and writing data into flash memory is more
complicated :write –once takes about 4-10 micro secs.
It has found its popularity as replacement for disks for storing small volumes of
data (5-10 megabyte)
d) Tape storage
It is used primarily for backup and archival data. Cheaper, but much slower
access, since tape must be read sequentially from the beginning.
Used as protection from disk failures!
e) Magnetic disk storage
Typically the entire database is stored on disk

11
Data must be moved from disk to main memory in order for the data to be
operated on.
After operations are performed, data must be copied back to disk if any changes
were made.
Disk storage is called direct access storage as it possible to read data on the
disk in any order (unlike sequential access).
Disk storage usually survives power failure and system crashes
f) Optical storage
CD-Rom (compact disk - read only memory), WORM(write-once read many) disk
(for archival storage of data), and juke box (containing as few drives and
numerous disks loaded on demand).

2. Another classification : Primary, secondary and tertiary storage.


a) Primary storage : the fast test storage media, such as cache and main memory.
b) secondary (or on-line ) storage : the next level of the hierarchy.
Ex: magnetic disks.
c) Tertiary (or off-line) storage : magnetic tapes and optical disk juke boxes.

3. Volatility of storage, volatile storage loses its contents when the power is removed
without power back up, data in the volatile storage (the part of the hierarchy from main
memory up) must be written to non-volatile storage for safekeeping.

2. FILE STRUCTURES
A file can be defined as a collection of data or information.
There are two types of files. They are Program files and Data Files.
Program files can be described as files containing software instructions.
A file structure allows applications to read, write and modify data. It might also
support finding the data that matches some search criteria or reading though the data
in some particular order.

TYPES OF THE FILES STRUCTURES


1. Sequential files
2. Direct or Random files
3. Index Sequential files
4. Hashed files
1. Sequential files
· The records are in one order
· There is no key value.
· Gaps are left so that new records can be added there to maintain ordering.
· New record is added in gaps left between records according to ordering.
· These files were stored in magnetic tapes.
· When the size of the files increases, the time required to access data becomes
more. This is because there are no key.
· Logical ordering may not be equal to physical ordering.
· These are used in master files.
· Examples Arranging cards in sequential order (A 2 3 4 5 6 7 8 9 10 J Q K)
2. Direct or Random files
· Each record is associated with a direct function.
· There are key values.
· The records have mapping.
12
· Disk devices like CD are used.
· Searching is quite faster.
· Hashing is its extension.
· Due to mapping more space is needed.
· It is more complex than the previous 2 files.
3. Index Sequential file
· This was invented by IBM.
· These uses indices.
· Assess is sequential.
· Indexing and sorting is random.
· These have keys.
· A group of keys are given one index.
· Disk device is used for storage.
· Searching is fast as the index is searched and then the key is searched.
· This is used in banking.
4. Hashed files
· An alternative to storing the index as hash table is to not have an index at all.
· Instead, hash on the key to find the addressing to resolve collisions.
· The usual hashing consideration arises.

CLASSIFICATION OF DATA STRUCTURE


The data structures are mainly classified into two categories. They are
I. Primitive data Structure
II. Non-Primitive data Structure

I. Primitive Data Structure


Primitive data structures are those structure which are readily available in a programming
language i.e. they can be directly operated upon by programming instructions. Here we are
concerned with structuring of data at their most primitive level with in a computer. The
storage representation or structure (Memory representation) and the possible operations
(arithmetic operations, relational operations) for these types of structure are predefined and
the user cannot change this.

13
The storage structure of these data structure are
A. Integer
B. Float
C. character
D. Boolean

A. Integer: Integer as we known numbers or natural numbers. Which can be either


positive or negative . Some languages have variations of the integers type.
For example : C\ C++ implements the following variations : byte, shorts, int and long.

B. Real Number or Float: In the several languages real numbers are implemented as
floating point numbers and double precision floating point numbers.
A real number consists of two parts, mantissa and characteristic. A real number
data type is generally denoted with the term float. There are many varieties of floating
point notation and each has individual characteristic .
A real number is represented using the following expression : m x n r, where m is
the mantissa and n is the base (which is fixed 10) and r is the exponent.
A floating point notation facilitates storage of number with extremely large and
extremely small values. The range of values that can be represented in a float data
types are from -3.4 e -38 to 3.4 +38

C. Characters: A character data type relates to the representation of alpha numeric data.
Traditionally the coding system is ASCII .These are all 8-bit system. Most of the
computers use eight bits to represent a character. That is 256 character can be
represented. In java language 16-bit Unicode system as implemented character data
type represents 2 bytes in java.

D. Boolean: Boolean data type allows for two possible values: true or false. Only one bit
is required to store Boolean values.

II. NON- PRIMITIVE DATA STRUCTURES


1. Linear data structure and
2. Non –linear data structure
1. Linear Data Structures
A linear data structure s the relationship of an important property called as adjacency
between the elements. The concept of adjacency may indicate either a linear or sequential
relationship i.e., if we are able to identify the position of an element we should be able to
identify the position of the previous element .The different linear data structures are arrays
, linked lists, stacks and queues.
a) Arrays
Array is a collection of elements. It is used in mathematical problems like matrix, algebra
etc. each element of an array is referenced by a subscripted variable or value, called
subscript or index enclosed in brackets.

Definition: “An array is defined as a set of homogeneous (same data type) data items.
They can have a common name and stored in continues locations of the memory.”
The individual values of array are called as elements. Array can be any valid data type
item.

Int A[n] =
A[0] A[1] A[2] A[3] …… A[n-1]
14
Declaration:
Array will be declared in a program like other variables. The declaration of the array has
the following
Syntax: datatype array_name [ ];
datatype [ ] array_name;
In this datatype is any one of the java supported data types, which indicates the type of
elements of array. array_name is a valid identifier, Size indicates the number of
elements of the array.
It should be a positive integer. Every array must have a subscript.
Arrays can be classified into
1. Single Dimensional Array (or ) Linear Array
2. Multi Dimensional Array. (or) Non Linear Array

1. Single Dimensional Array (or) Linear Array


It is a list of items, whose elements are specified by one subscript. It is also called as
one dimensional array.
Syntax: datatype array_name []=new datatype[size];
EX: int a[]=new int[5];
Individual elements of the array are accessed by specifying their subscript number. The
subscript number of the array always starts zero.

2. Multi / two Dimensional Array


These are collection of homogeneous elements where the elements are ordered in a
number of rows and columns. To access an element in the two dimensional array we
require two subscripts.
int arr [ ][ ] = new int [5][5]
1. If an array has two sub scripts, then it is called as two dimensional array.
These are otherwise called asmatrix arrays or table arrays.
2. These are used to organize in terms of rows and columns.
3. Each element in two dimensional array is identified by name of the array, its
row index and its column index.

It is the representation of matrix format


0 1 2 3
0 a[0][0] a[0][1] a[0][2] a[0][3]
1 a[1][0] a[1][1] a[1][2] a[1][3]
2 a[2][0] a[2][1] a[2][2] a[2][3]
3 a[3][0] a[3][1] a[3][2] a[3][3]

We can create a 2D array as follows.


datatype array-name[ ][ ];
or
datatype[ ][ ] array-name;
Example:
int a[ ][ ];
int[ ][ ] a;

15
Operations on Arrays:
1. Creation of array
2. Initialization of array
3. Accepting data into array
4. Displaying contents of array
5. Insertions
6. Deletions
7. Search
8. Sort
Advantages of arrays:
1. As arrays store all the elements in continuous locations, it becomes easy to access all
the elements.
2. Arrays allow the user to store elements of same type in sequential manner.
3. Array elements can be accessed randomly (need not to follow any order).
4. Arrays avoid no. of variables & their naming.
Disadvantages of arrays:
1. Array size is fixed. The user cannot grew or shrink the size dynamically.
2. Optimum utilization of memory may not be achieved (i.e., there is a scope for wasting
the memory).
3. Arrays allow only similar kind of items as data.

b) Linked Lists
Linked list is a linear collection of data elements called nodes, and a node consist of two
parts : data Info and Link Info gives information and Link is an reference of next node.
DATA LINK
Here data/info and link/address pointer both are stored. `So, extra space is needed to
store the pointer associated with each element and that access is take more time.

Here we have different types of linked lists are available.


They are
1. Single linked list
2. Double linked list
3. Circular linked list

1. SINGLE LINKED LIST


A single linked list is a linked list that contains of one data field part and only one pointer
field. This pointer points the next element in the list. In this list every node points the next
node and the last node contains a null pointer.
Here, Info part which stores the information, Address or pointer part which holds the
address of next element of same type. Linked list isalso known as self-referential
structure.

16
2. DOUBLE LINKED LIST
In a double linked list all nodes are linked together by multiple links, which help in
accessing the both next node and previous node for any item with in the list. Therefore
each node in a double linked list links to the left node and the right node. This helps to
travel the list in forward direction and in backward direction.
Contains Left Link, Data, Right Link
L LINK DATA R LINK

Two way Node


In single linked list we can transverse only one direction. This is why it is called one
way list. In many cases we transverse backward as well as forward for this we can use
double linked list.
A node in double linked list contains a data field and two pointer field’s one pointer
pointing the previous node or left node and the other pointing the next node or right node.

NULL
LINK DATA LINK LINK DATA LINK LINK DATA LINK

Head node middle node Last node

c) Stacks
Stack is a list of elements. In stack, an element may be inserted or deleted at one end
which is known as Top of the stack. It performs two operations. Push and Pop. Push
means adding an element into stack. Pop means delete an element from the stack.

Here, both insertion and deletion can takes place at the top of the stack. When we
are taking the operations like insertion and deletion, the last inserted element will be the
first element to delete from the stack.
So, It is also called Last-in-First –out (LIFO) type of list.
Advantages : It provides Last in, First out access can be made easy of operations
Disadvantage : Slow in access to other elements

Operations on Stack:
Push: Adds an item onto the stack. PUSH (s, i); Adds the item i to the top of stack.
Pop: Removes the most-recently-pushed item from the stack. POP (s); Removes the top
element and returns it as a function value.

Implementation of Stack: A stack can be implemented using two ways:


• Array
• Linked list

17
d) Queues
Queue is linear list of element. It can permit insertion of an element at one end, and
deletion of an element in another end. The end at which the element is deleted or removed
from the queue is called as front, and the end at which insertion of an element into the
queue is called rear. So, the insertion and deletion can takes place at front and rear ends
only.
In queue, elements are added at one end called rear and the existing element are
deleted from other and called front.

The first element that is inserted into the queue, is the first one to get deleted from it.
Hence, It is also called as First-in-First- out(FIFO) list.
In general, Queue means a sequential process to follow the order.
Advantages : It provides First in, First out process of data operations
Disadvantage : Slow in access to other elements

Operations on Queue:
Enqueue: Adds an item onto the end of the queue ENQUEUE(Q, i); Adds the item i
onto the end ofqueue.
Dequeue: Removes the item from the front of the queue. DEQUEUE (Q); Removes
the firstelement and returns it as a function value.

Queue Implementation: Queue can be implemented in two ways.


• Static implementation (using arrays)
• Dynamic implementation (using painters)

2. NON – LINEAR DATA STRUCTURES


A non-linear data structures can exhibit any property other than adjacency between the
element . Non-linear data structure may exhibit either a hierarchical relationship or a parent
child relationship . The different non – linear data structures are trees and graphs.

a) Trees
A TREE can be defined as a finite set of data elements. A Tree is a flexible, versatile and
powerful non– linear data structures. It is used to represent data items processing
hierarchical relationship between the grandfather and his children & Grand children.
It is an ideal data structures for representing hierarchical data.
A tree is a set of one or more nodes, such that
1) There is a specially designed node called root.
2) The remaining nodes are portioned into disjoints sets, T1, , Tn. These sets of T1,
-----, Tn are called the sub trees of the root.

18
Advantages: It provides quick search, insert and delete operations
Disadvantages : Complicated deletion algorithms
b) Graphs
Graph is a non-linear data structure mainly used in the applications for finding shortest
path root. This data structure consists of number of non-empty set of vertices as well as
edges.
A graph ‘G’ consists of a set of ‘V’ vertices (nodes) and set of ‘E’ edges (arcs)
To represent a graph G= {V, E}, here V is a non-empty set of vertices.
E is a set of pairs of vertices, these pairs are called edges.
Generally, graphs are two types, they are
1. undirected graphs
2. directed graphs.

Advantages : It provides best models for real world situations


Disadvantages : Some algorithms are very slow and very complex.

ATOMIC TYPE
Generally, a data structure is represented by a memory block, which has two
parts:
• Data storage
• Address storage
This facilitates in storing the data and also stores pointers in the address part.
An atomic type data is a data structure that contains only the data items and not
the pointers. Thus, for a list of data items, several atomic type nodes may exist
each with a single data item corresponding to one of the legal data types. The list
is maintained using a list node which contains pointers to these atomic nodes and a
type indicator indicating the type of atomic node to which it points. Whenever a test
node is inserted in the list, its address is stored in the next free element of the list
of pointers.

a list of atomic nodes maintained using list of nodes. In each node, type
represents the type of data stored in the atomic node to which the list node points.
19
Here, I stands for integer type, 2 for real number and 3 for character type or
any different assumption can be made at implementation level to indicate different
data types.

REFINEMENT STAGES
The best approach to solve a complex problem is to divide it into smaller parts
such that each part becomes an independent module which is easy to manage.
An example of this approach is the System Development Life Cycle (SDLC)
methodology. This helps in understanding the problem, analyzing solutions, and
handling the problems efficiently.
The principle underlying writing large programs is the top-down refinement. While
writing the main program, we decide exactly how the work will be divided into various
functions and then, in the refinement process, it is further decided what will be the
task of each function, and what inputs are to be given and results to be
obtained. The data and actions of the functions are specified precisely. Similarly,
the purpose of studying Abstract data types is to find out some general principles that
will help in designing efficient programs. There exists a similarity between the
process of top down refinement of algorithms and the top-down specification of the
data structures. In algorithm design, we begin with the problem and slowly specify
more details until we develop a complete program. In data specification, we begin
with the selection of mathematical concepts and abstract data types required for our
problem, and slowly specify more details until finally we can describe our data
structures in terms of programming language.
The application or the nature of problem determines the number of refinement
stages required in the specification process. Different problems have different
number of refinement stages, but in general, there are four levels of refinement
processes:
• Conceptual or abstract level
• Algorithmic or data structures
• Programming or implementation
• Applications

Conceptual Level
At this level we decide how the data is related to each other, and what operations are
needed. Details about how to store data and how various operations are performed
on that data are not decided at this level

Algorithmic or Data Structure Level


At data structure level we decide about the operations on the data as needed by our
problem. For example, we decide what kind of data structure will be required to solve
the problem-contiguous list will be preferred for finding the length of a list, or for

20
retrieving any element, whereas for the evaluation of any expression into prefix or
postfix, stacks will be used.
Programming or Implementation Level
At implementation level, we decide the details of how the data structures will be
represented in the computer memory. For example, we decide whether the linked
lists will be implemented with pointers or with the cursors in an array.
Application Level
This level settles all details required for particular application such as names for
variables or special requirements for the operations imposed by applications

The first two levels are often called conceptual. The middle two levels can be called
algorithmic as they are concerned with representing data and the operations
performed on the same. Last level is basically concerned with programming at the
conceptual level, the queue has been selected as an abstract data type and
further at the next level circular queue has been selected, as this could provide
the best solution. Last level shows the operations which can be performed on the
data, for the Airport simulation,

21
UNIT I
IMPORTANT QUESTIONS

1. What is Data Structures? Explain


2. Explain different types of structures?
3. What is Data Representation? Explain
4. What is ADT? Explain
5. What is Data Type? Explain different types of Data Types?
6. Explain the classification of Data Structures?
7. What is an atomic type?
8. Differentiate Data Type, Data Structures and ADT?
9. Write about Refinement stages?

22
Syllabus: Principles of programming and Analysis of algorithms: software engineering,
program design, algorithms, different approaches to designing an algorithm, complexity,
big ‘o’ notation, algorithm analysis, structured approaches to programming, recursion,
tips and techniques for writing c programming
1.What is software engineering?
Software Engineering is a systematic, disciplined, quantifiable study and approach to the
design, development, operation, and maintenance of a software system.

Objectives of Software Engineering:


1. Maintainability –
It should be feasible for the software to evolve to meet changing requirements.
2. Efficiency –
The software should not make wasteful use of computing devices such as memory,
processor cycles, etc.
3. Correctness –
A software product is correct if the different requirements as specified in the SRS document
have been correctly implemented.
4. Reusability –
A software product has good reusability if the different modules of the product can easily be
reused to develop new products.
5. Testability –
Here software facilitates both the establishment of test criteria and the evaluation of the
software with respect to those criteria.
6. Reliability –
It is an attribute of software quality. The extent to which a program can be expected to
perform its desired function, over an arbitrary time period.
7. Portability –
In this case, the software can be transferred from one computer system or environment to
another.
8. Adaptability –
In this case, the software allows differing system constraints and the user needs to be
satisfied by making changes to the software.
9. Interoperability – Capability of 2 or more functional units to process data cooperatively.

Developing good software is a tedious process which keeps on going i.e. under development for
a long time before the software or the program takes the final shape. This process is often
termed as Software Development Life Cycle (SDLC). Here, the output of one stage becomes the
input of next stage.

What is SDLC?
Developing good software is a tedious process which keeps on going i.e. under development for
a long time before the software or the program takes the final shape. This process is often
termed as Software Development Life Cycle (SDLC). Here, the output of one stage becomes the
input of next stage.

The various steps involved in the Software Development Life Cycle are as follows:
Planning, Analysis, Design, Development, Testing, Implementation and Maintenance

• Analyse the problem with precision


• Create a prototype and experiment with it until all requirements are finalized

23
• Design an algorithm for the task using the tools of the data structure
• Verify the algorithmic steps
• Analyse the algorithm for checking its requirements
• Code the algorithm to any suitable programming language
• Test and evaluate the code
• Refine and repeat the preceding steps until the software is complete
• Optimize the code to improve performance
• Maintain the application that you have designed so that it meets the upcoming client's and users
need

Program Design
Program design is an important stage of software development. This phase takes the help of
algorithms and different concepts of data structures to solve the problem(s) that is proposed.

Different approaches to Designing Algorithm


Complex code can be subdivided into smaller units called modules. The advantage of modularity
is that it allows the principle of separation of concerns to be applied into two phases are -

• While dealing with details of each module in isolation


• While dealing with overall characteristics of all modules and their relationships

Modularity enhances design clarity, which in turn eases implementation and readability.
Debugging, testing, documenting and maintenance of product also increase due to modularity.

What Do You Mean by Complexity of Algorithm?


Complexity is an essential concept in Data structure. When you talk about complexity is related to
computer, you call it as computational complexity. It can be termed as the characterization of
time and space requirements for solving a problem using some specific algorithm. These
requirements are expressed regarding one single parameter which is used to represent the size
of the problem.
Example:
Let 'n' denotes the size of the problem. Then the time required for a specific type of algorithm for
solving a problem can be expressed as:
f: R -> R, where f is the function and f(n) are the most significant amount of time needed. Thus,
you can conclude that analysis of any program requires two vital concepts:

• Time Complexity
• Space Complexity

Time Complexity of a program can be defined as the amount of time the computer takes to run a
program to its completion. On the other hand, the space complexity of an algorithm can be
defined as the memory that it needs to run that algorithm to its completion.

Big ‘O’ notation and Algorithm Analysis


Explain Various approaches to design algorithms:
24
1. Top-down approach
2. Bottom-up approach

Now let's discuss both of them:

Top-down approach

The top-down approach starts by identifying the major components of the system or program
decomposing them into their lower-level components and iterating until the desired level of
modular complexity is achieved. The top-down method takes the form of stepwise working and
refinement of instructions. Thus, the top-down approach starts from an abstract design, and each
step is refined into more concrete level until the final refined stage is not reached.
Bottom –up approach

The bottom-up design starts with designing the most basic or primitive components and proceeds
to the higher-level component. It works with layers of abstraction. Starting from below, the
operation that provides a layer of abstraction is implemented. These operations are further used
to implement more powerful operations and still higher layers of abstraction until the final stage is
reached.

What is Big - O notation?

When resolving a computer-related problem, there will frequently be more than just one solution.
These individual solutions will often be in the shape of different algorithms or instructions having
different logic, and you will normally want to compare the algorithms to see which one is more
proficient. So, this is where Big O analysis helps program developers to give programmers some
basis for computing and measuring the efficiency of a specific algorithm.
If f(n) represents the computing time of some algorithm and g(n) represents a known standard
function like n, n2, n log n, then to write:
f(n) is O g(n)
which means that f(n) of n equals to the biggest order of the function, i.e., the g(n).
So what Big - O does? It helps to determine the time as well as space complexity of the
algorithm. Using Big - O notation, the time taken by the algorithm and the space required to run
the algorithm can be ascertained. Some of the lists of common computing times of algorithms in
order of performance are as follows:

• O (1)
• O (log n)
• O (n)
• O (nlog n)
• O (n2)
• O (n3)
25
• O (2n)

Thus, algorithm with their computational complexity can be rated as per the mentioned order of
performance.

Algorithm Analysis

This complexity is used to analyse the algorithm in the data structure. There are various ways of
solving a problem and there exists different algorithms which can be designed to solve the
problem.
Consequently, analysis of algorithms focuses on the computation of space and time complexity.
Here are various types of time complexities which can be analysed for the algorithm:

• Best case time complexity: The best-case time complexity of an algorithm is a measure of the
minimum time that the algorithm will require for an input of size 'n.' The running time of many
algorithms varies not only for the inputs of different sizes but also for the different inputs of the
same size.
• Worst case time Complexity: The worst-case time complexity of an algorithm is a measure of
the minimum time that the algorithm will require for an input of size 'n.' Therefore, if various
• algorithms for sorting are taken into account and say 'n,' input data items are supplied in reverse
order for a sorting algorithm, then the algorithm will require n2 operations to perform the sort
which will correspond to the worst-case time complexity of the algorithm.
• Average Time complexity Algorithm: This is the time that the algorithm will require to execute
a typical input data of size 'n' is known as the average case time complexity.

Structured approaches to programming:


What do you mean by structured programming in C?
Structured Programming

Structured programming is a paradigm that aims to make programs easier to comprehend from a
reader’s point of view. It does this by linearising the flow of control through a program. In
structured programming, execution follows the writing order of the code.

Structured programming caught favour with programming languages for its iconic opposition to
the keyword goto, aiming to reduce the prevalence of spaghetti code. Some other controversial
features that most languages have not adopted are avoiding early exit and opposition to
exceptions for control flow.

What are the elementary structures of structured programs?

• Block: It is a command or a set of commands that the program executes linearly. The
sequence has a single point of entry (first line) and exit (last line).
• Selection: It is the branching of the flow of control based on the outcome of a condition.
Two sequences are specified: the ‘if’ block when the condition is true and the ‘else’ block
when it is false. The ‘else’ block is optional and can be a no-op.
• Iteration: It is the repetition of a block as long as it meets a specific condition. The
evaluation of the condition happens at the start or the end of the block. When the condition
results in false, the loop terminates and moves on to the next block.

26
• Nesting: The above building blocks can be nested because conditions and iterations,
when encapsulated, have singular entry-exit points and behave just like any other block.
• Subroutines: Since entire programs now have singular entry-exit points, encapsulating
them into subroutines allows us to invoke blocks by one identifier.

What is recursion?

Recursion in data structures

• Recursion is one of the most powerful tools in a programming language, but one of the
most threatening topics-as most of the beginners and not surprising to even experienced
students feel.
• When function is called within the same function, it is known as recursion in C. The
function which calls the same function, is known as recursive function.
• Recursion is defined as defining anything in terms of itself. Recursion is used to solve
problems involving iterations, in reverse order.

Types of Recursions
There are two types of Recursions

• Direct recursion
• Indirect recursion

Direct Recursion
When in the body of a method there is a call to the same method, we say that the method
is directly recursive.
There are three types of Direct Recursion

• Linear Recursion
• Binary Recursion
• Multiple Recursion

Linear Recursion

• Linear recursion begins by testing for a set of base cases there should be at least one.
27
In Linear recursion we follow as under:

• Perform a single recursive call. This recursive step may involve a test that decides which
of several possible recursive calls to make, but it should ultimately choose to make just
one of these calls each time we perform this step.
• Define each possible recursion call, so that it makes progress towards a base case.

Binary Recursion

• Binary recursion occurs whenever there are two recursive calls for each non base case.

Multiple Recursion

• In multiple recursion we make not just one or two but many recursive calls.

//C program for GCD using recursion


#include int
Find_GCD(int, int);
void main()
{
int n1, n2, gcd;
scanf(“%d %d”,&n1, &n2);
gcd = Find_GCD(n1, &n2);
printf(“GCD of %d and %d is %d”, n1, n2, gcd);
}
int Find_GCD(int m, int n)
{
int gcdVal;
if(n>m)
{
gcdVal = Find_GCD(n,m);
}
else if(n==0)
{
gcdVal = m;
}
else
{
gcdVal = Find_GCD(n, m%n);
}
return(gcdVal);
}

Disadvantages of Recursion

• It consumes more storage space the recursive calls along with automatic variables are
stored on the stack.
• The computer may run out of memory if the recursive calls are not checked.
• It is not more efficient in terms of speed and execution time.
• According to some computer professionals, recursion does not offer any concrete
advantage over non-recursive procedures/functions.
• Recursive solution is always logical and it is very difficult to trace. (Debug and
understand).
• In recursive we must have an if statement somewhere to force the function to return
without the recursive call being executed, otherwise the function will never return.
• Recursion takes a lot of stack space, usually not considerable when the program is small
and running on a PC.
• Recursion uses more processor time.
28
• Recursion is not advocated when the problem can be through iteration.
• Recursion may be treated as a software tool to be applied carefully and selectively.

Difference between recursion and iteration


Iteration Recursion
In iteration, a problem is converted into a train of steps Recursion is like piling all of those steps on top of each other a
that are finished one at a time, one after another then quashing the mall into the solution.
With iteration, each step clearly leads on to the next, like In recursion, each step replicates itself at a smaller scale, so t
stepping stones across a river all of them combined together eventually solve the problem.
Any iterative problem is solved recursively Not all recursive problem can solved by iteration
It does not use Stack It uses Stack

29

You might also like