You are on page 1of 11

1

DSC–3C Data Structures BS306 Theory 4 Hours/Week 4 credits


Practical 2 Hours/Week 1 credit
Unit – I
Fundamental Concepts: Introduction to Data Structures, Types of Data Structures, Introduction to
Algorithm,Pseudo-code, Flow Chart, Analysis of Algorithms.
Linear Data Structure Using Arrays: 1-D Arrays, 2-D Arrays, N-D Arrays, Memory Representation and
Address Calculation of 1-D, 2-D, N-D Arrays, Concept of Ordered List, String Manipulation, Pros and Cons
of Arrays.
Stacks: Concept, Primitive Operations, Abstract Data Type, Representation Stacks Using Arrays, Prefix,
Infix, Postfix Notations for Arithmetic Expression, Applications of Stacks– Converting Infix Expression to
Postfix Expression, Evaluating the Postfix Expression, Checking Well-formed (Nested) Parenthesis,
Processing of Function Calls, Reversing a String.
Unit – II
Recursion: Introduction, Recurrence, Use of Stack in Recursion, Variants of Recursion, Execution of
Recursive Calls, Recursive Functions, Iteration versus Recursion.
Queues: Concept, Primitive Operations, Abstract Data Type, Representation Queues Using Arrays, Circular
Queue, Double-Ended Queue, Applications of Queues.
Linked Lists: Introduction, Concept, Terminology, Primitive Operations-creating, inserting, deleting,
traversing, Representation of Linked Lists, Linked List Abstract Data Type, Linked List Variants - Singly
Linked List, Doubly Linked List, Linear and Circular Linked List, Representation Stacks and Queues Using
Linked Singly Lists, Application of Linked List–Garbage Collection.
Unit – III
Trees: Introduction, Representation of a General Tree, Binary Tree Introduction, Binary Tree Abstract Data
Type, Implementation of Binary Trees, Binary Tree Traversals – Preorder, Inorder, Postorder Traversals,
Applications of Binary Trees Briefly.
Graphs: Introduction, Graph Abstract Data Type, Representation of Graphs, Graph Traversal – Depth-First
Search, Breadth-First Search, Spanning Tree – Prim’s Algorithm, Kruskal’s Algorithm.
Hashing: Introduction, Hash Functions, Collision Resolution Strategies.
Unit – IV
Searching and Sorting: Sequential (Linear) Search, Binary Search, Bubble Sort, Insertion Sort, Selection
Sort,
Quick Sort, Merge Sort, and Comparison of Sorting Techniques.
Heaps: Concept, Implementation, Abstract Data Type, Heap Sort.

Text Varsha H. Patil, Data Structures Using C++


………………………………………………………………………………………………………………
Model paper : Data Structurers Max marks : 80 Time : 3hrs
Section – A (5x4M=20Marks)
Answer any five of the following eight questions. Each carries four marks.
Q1. From unit – 1
Q2. From unit – 1
Q3. From unit – 2
Q4. From unit – 2
Q5. From unit – 3
Q6. From unit – 3
Q7. From unit – 4
Q8. From unit – 4
Section – B(4x15M=60Marks)
Answer all the following four questions. Each carries FIFTEEN marks.
Q9. (a) or (b) from unit – 1
Q10. (a) or (b) from unit – 2
Q11. (a) or (b) from unit – 3
Q12. (a) or (b) from unit – 4
2

Data Structure
Introduction
Data Structure can be defined as the group of data elements which provides an efficient way of storing and
organising data in the computer so that it can be used efficiently. Some examples of Data Structures are
arrays, Linked List, Stack, Queue, etc. Data Structures are widely used in almost every aspect of Computer
Science i.e. Operating System, Compiler Design, Artifical intelligence, Graphics and many more.
Data Structures are the main part of many computer science algorithms as they enable the programmers to
handle the data in an efficient way. It plays a vitle role in enhancing the performance of a software or a
program as the main function of the software is to store and retrieve the user's data as fast as possible

Basic Terminology
Data structures are the building blocks of any program or the software. Choosing the appropriate data
structure for a program is the most difficult task for a programmer. Following terminology is used as far as
data structures are concerned.
Data: Data can be defined as an elementary value or the collection of values, for example, student's name
and its id are the data about the student.
Group Items: Data items which have subordinate data items are called Group item, for example, name of a
student can have first name and the last name.
Record: Record can be defined as the collection of various data items, for example, if we talk about the
student entity, then its name, address, course and marks can be grouped together to form the record for the
student.
File: A File is a collection of various records of one type of entity, for example, if there are 60 employees in
the class, then there will be 20 records in the related file where each record contains the data about each
employee.
Attribute and Entity: An entity represents the class of certain objects. it contains various attributes. Each
attribute represents the particular property of that entity.
Field: Field is a single elementary unit of information representing the attribute of an entity.
Need of Data Structures
As applications are getting complexed and amount of data is increasing day by day, there may arrise the
following problems:
Processor speed: To handle very large amout of data, high speed processing is required, but as the data is
growing day by day to the billions of files per entity, processor may fail to deal with that much amount of
data.
Data Search: Consider an inventory size of 106 items in a store, If our application needs to search for a
particular item, it needs to traverse 106 items every time, results in slowing down the search process.
Multiple requests: If thousands of users are searching the data simultaneously on a web server, then there
are the chances that a very large server can be failed during that process
in order to solve the above problems, data structures are used. Data is organized to form a data structure in
such a way that all items are not required to be searched and required data can be searched instantly.
Advantages of Data Structures
Efficiency: Efficiency of a program depends upon the choice of data structures. For example: suppose, we
have some data and we need to perform the search for a perticular record. In that case, if we organize our
data in an array, we will have to search sequentially element by element. hence, using array may not be very
efficient here. There are better data structures which can make the search process efficient like ordered
array, binary search tree or hash tables.
Reusability: Data structures are reusable, i.e. once we have implemented a particular data structure, we can
use it at any other place. Implementation of data structures can be compiled into libraries which can be used
by different clients.
Abstraction: Data structure is specified by the ADT which provides a level of abstraction. The client
program uses the data structure through interface only, without getting into the implementation details.
3

Data Structure Classification

Linear Data Structures: A data structure is called linear if all of its elements are arranged in the linear
order. In linear data structures, the elements are stored in non-hierarchical way where each element has the
successors and predecessors except the first and last element.

Types of Linear Data Structures are given below:


Arrays: An array is a collection of similar type of data items and each data item is called an element of the
array. The data type of the element may be any valid data type like char, int, float or double.
The elements of array share the same variable name but each one carries a different index number known as
subscript. The array can be one dimensional, two dimensional or multidimensional.
The individual elements of the array age are:
age[0], age[1], age[2], age[3],......... age[98], age[99].
Linked List: Linked list is a linear data structure which is used to maintain a list in the memory. It can be
seen as the collection of nodes stored at non-contiguous memory locations. Each node of the list contains a
pointer to its adjacent node.
Stack: Stack is a linear list in which insertion and deletions are allowed only at one end, called top.
A stack is an abstract data type (ADT), can be implemented in most of the programming languages. It is
named as stack because it behaves like a real-world stack, for example: - piles of plates or deck of cards etc.
Queue: Queue is a linear list in which elements can be inserted only at one end called rear and deleted only
at the other end called front.
It is an abstract data structure, similar to stack. Queue is opened at both end therefore it follows First-In-
First-Out (FIFO) methodology for storing the data items.
Non Linear Data Structures: This data structure does not form a sequence i.e. each item or element is
connected with two or more other items in a non-linear arrangement. The data elements are not arranged in
sequential structure.
Types of Non Linear Data Structures are given below:
Trees: Trees are multilevel data structures with a hierarchical relationship among its elements known as
nodes. The bottommost nodes in the hierarchy are called leaf node while the topmost node is called root
node. Each node contains pointers to point adjacent nodes.
4

Tree data structure is based on the parent-child relationship among the nodes. Each node in the tree can have
more than one children except the leaf nodes whereas each node can have atmost one parent except the root
node. Trees can be classified into many categories which will be discussed later in this tutorial.
Graphs: Graphs can be defined as the pictorial representation of the set of elements (represented by
vertices) connected by the links known as edges. A graph is different from tree in the sense that a graph can
have cycle while the tree cannot have the one.

Operations on data structure


1) Traversing: Every data structure contains the set of data elements. 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 devide that sum by the
number of subjects i.e. 6, in order to find the average.
2) 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.
3) 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.
If we try to delete an element from an empty data structure then underflow occurs.
4) Searching: The process of finding the location of an element within the data structure is called Searching.
There are two algorithms to perform searching, Linear Search and Binary Search. We will discuss each one
of them later in this tutorial.
5) Sorting: The process of arranging the data structure in a specific order is known as Sorting. There are
many algorithms that can be used to perform sorting, for example, insertion sort, selection sort, bubble sort,
etc.
6) Merging: When two lists List A and List B of size M and N respectively, of similar type of elements,
clubbed or joined to produce the third list, List C of size (M+N), then this process is called merging
Algorithm
An algorithm is a procedure having well defined steps for solving a particular problem. Algorithm is
finite set of logic or instructions, written in order for accomplish the certain predefined task. It is not
the complete program or code, it is just a solution (logic) of a problem, which can be represented
either as an informal description using a Flowchart or Pseudo code.

The major categories of algorithms are given below:

o Sort: Algorithm developed for sorting the items in certain order.


o Search: Algorithm developed for searching the items inside a data structure.
o Delete: Algorithm developed for deleting the existing element from the data structure.
o Insert: Algorithm developed for inserting an item inside a data structure.
o Update: Algorithm developed for updating the existing element inside a data structure.

The performance of algorithm is measured on the basis of following properties:

o Time complexity: It is a way of representing the amount of time needed by a program to run
to the completion.
o Space complexity: It is the amount of memory space required by an algorithm, during a
course of its execution. Space complexity is required in situations when limited memory is
available and for the multi user system.
5

Each algorithm must have:

o Specification: Description of the computational procedure.


o Pre-conditions: The condition(s) on input.
o Body of the Algorithm: A sequence of clear and unambiguous instructions.
o Post-conditions: The condition(s) on output.

Example: Design an algorithm to multiply the two numbers x and y and display the result in z.

o Step 1 START
o Step 2 declare three integers x, y & z
o Step 3 define values of x & y
o Step 4 multiply values of x & y
o Step 5 store the output of step 4 in z
o Step 6 print z
o Step 7 STOP

. Alternatively the algorithm can be written as ?

o Step 1 START MULTIPLY


o Step 2 get values of x & y
o Step 3 z← x * y
o Step 4 display z
o Step 5 STOP

Characteristics of an Algorithm
An algorithm must follow the mentioned below characteristics:

o Input: An algorithm must have 0 or well defined inputs.


o Output: An algorithm must have 1 or well defined outputs, and should match with the desired
output.
o Feasibility: An algorithm must be terminated after the finite number of steps.
o Independent: An algorithm must have step-by-step directions which is independent of any
programming code.
o Unambiguous: An algorithm must be unambiguous and clear. Each of their steps and
input/outputs must be clear and lead to only one meaning.
6

Algorithm Design Tools: Pseudocode and Flowchart


The two popular tools used in the representation of algorithms are the following:
1. Pseudocode
2. Flowchart
Pseudocode :
An algorithm can be written in any of the natural languages such as English, German, French, etc. One of
the commonly used tools to define algorithms is the pseudocode.
A pseudocode is an English-like presentation of the code required for an algorithm. It is partly English and
partly computer language structure code. The structure code is nothing but syntax constructs of a
programming language (in a slightly modified format). For example, some language structure constructs
such as arrays or pointers are not used in the English language, hence they are borrowed from programming
languages.
Pseudocode Notations
Pseudocode is a precise description of a solution as compared to a flowchart. To get a complete description
of the solution with respect to problem definition, pre–post conditions and return value details are to be
included in the algorithm header. In addition, information about the variables used and the purpose are to be
viewed clearly. To help anyone get all this information at a glance, the pseudocode uses various notations
such
as header, purpose, pre–post conditions, return, variables, statement numbers, and sub-algorithms.

Algorithm Header
A header includes the name of the algorithm, the parameters, and the list of pre and post conditions. This
information is important to know about the algorithm just by reading the header, not the complete algorithm.
Therefore, the header information must be complete enough to communicate to the programmer everything he or
she must know to use the algorithm. The header makes the pseudocode readable. In this Algorithm , there are two
parameters, an array A and the total number of elements in the array, that is, its size N. The parameters could be
called either by reference (ref) or by value (val). The type is included in pointed brackets after the identifier. The
algorithm is to sort the array A of size N.
Algorithm 1.1
Algorithm sort(ref A<integer>, val N<integer>)
Pre array A to be sorted
Post sorted array A
Return None
1.if(N < 1) goto step (4)
2.M = N − 1
3.For I = 1 to M do
For J = I + 1 to N do
begin
if(A(I)> A(J))
then
begin
T = A(I)
A(I) = A(J)
A(J) = T
end
end if
end
4.stop

Purpose
The purpose is a brief description about what the algorithm does. It should be as brief as possible, describing
the general algorithm processing, but should not describe all of the processing. For example, in Algorithm
7

1.1, the purpose just tells that this algorithm sorts the array of integers and does not need to state that the
array is sorted and where the result is stored.

Algorithm 1.2
Algorithm : searches for an element in an array.
Algorithm search (val list<array>,val X<integer>)
Pre list containing data array to be searched and
argument containing data to be located
Post None
Return Location
1.Let list be the array and X be the element to be searched
2.For I = 1 to N do
begin
if(List(I) = X)
then
Return I
end if
end
3.Return -1
4.stop
Condition and Return Statements
The pre condition states the pre-requirements for the parameters, if any. For example, in an algorithm for set
operations, the pre condition may state that the input should be a group of elements without duplicates.
Sometimes, there are no pre conditions, in which case, we still list the pre condition with the statement
Nothing, as shown here.
Pre Nothing
If there are several input parameters, then the pre condition should be shown for each.
For example, a simple array search Algorithm 1.2 has the following header:
algorithm search (val list<array>, val argument<integer>)
Search array for specific item and return index location.
Pre : list containing data array to be searched, argument containing data to be located in the list
Post : None
Return : Location if found else return −1 indicating that the element is not found
In this search, two parameters are passed by value. The pre condition specifies that the two input parameters,
list and argument, must be initialized. If a binary search were being used, the pre condition would also state
that the array data must be ordered.
The post condition identifies any action taken and the status of any output parameters.
In Algorithm 1.1, the post condition is the array containing sorted data. If a value is returned, it will be
identified by a return condition.
Statement Numbers
The statements in an algorithm are numbered sequentially. For conditional or un-conditional
jumps and also for iteration statements, numbering helps identify the statements uniquely.
Any label system such as the decimal or roman numbers or even alphabets can be used
to label the statements. If decimal notation is used, then the statements within the iterative constructs can be
numbered as 4.1, 4.2, and so on. This notation helps indent the
algorithm properly.
4 while(i < 10) do
begin
4.1 x = x * y
4.2 i = i + 1
end
8

Variables
Variables are needed in algorithms. We need not define every variable used in the algorithm. The use of
meaningful variable names is appreciated as the context of the data is indicated by its name. Hungarian
notation is suggested for variable naming. It is suggested to use descriptive and meaningful variable names
to guarantee that the meaning is understood properly.
The variable name used in an algorithm can be continued to be used when the respective algorithm is coded
in a particular language. These meaningful variables make the code easier to understand, debug, and modify.
It is suggested to follow a few thumb rules as:
1. It is better to use descriptive variable names instead of single character names. Often, we use
variables such as x, y, z, a, or b and i or j for index variables of loops, matrices, and array
indices. For example, instead of using i and j as index variables for a twodimensional array, it
is suggested to use row and column as index variables. In searching algorithms, the suggested
variable for the element to be searched is target instead of x.
For a table of weather information, City and Temperature will be a better row index and
column
index variables, respectively.
2. It is suggested to avoid usage of short forms and generic names. For example, use ListofColors
instead of lcol or NumberOfStudents instead of nostud. The reason being short forms do not
necessarily mean what they intend. Commonly used generic names are index, count, number,
sum, total, row, column, etc. These variables are used in various modules of a program and may
have many instances. Adding a good qualifier to the generic name results in better understanding
to read, debug, or modify the code.
3. It is expected to use variable names so that the data type of the variable can be indicated. For
example, fAverageMarks, iNumberofColors, bAvailability for float, integer, and Boolean
data respectively.

Statement Constructs
There are three statement constructs used for developing an algorithm. The objective is that an algorithm
should be made up of a combination of lesser constructs, say three, as in the following:
1. sequence
2. decision
3. repetition
The use of only these constructs makes an algorithm easy to understand, debug, and modify.

Sequence
An algorithm is a sequence of instructions, which can be a simple instruction (input, output, or assignment)
or either of the other two constructs. Figure 1.3 shows an example of such a sequence construct. Algorithm
1.3 computes the area of a circle.
Sequence construct
do action 1 Algorithm 1.3
Pre None
do action 2 Post None
. Return None
1. Read Radius
2. AreaOfCircle = 2 * 3.142 * Radius * Radius
3. Print AreaOfCircle
4. Stop
do action n
Decision
Some problems cannot be solved with the help of just a sequence of simple instructions. Sometimes, we
need to test the conditions. If the result of the testing is true, we follow a sequence of instructions; if it is
false, we follow a different sequence of instructions. This is called decision or selection construct
If the condition is true,
9

Then,
Series of actions
Else
Series of actions
Example : Compare two numbers to print the maximum among them.
Solution The algorithm for comparing two numbers is listed in Algorithm 1.4 .
Pre None
Post None
Return None
1. Read two numbers Num1 and Num2
2. If Num1 > Num2
Then Print Num1
Else Print Num2
3. Stop

Subalgorithms
We studied three constructs—sequence, decision, and iteration—for developing an algorithm for solvable
problems. A solvable problem is a problem that has a solution that can be described in the form of an
algorithm.

In structured programming, the problem solution is described in the form of smaller modules. This modular
design breaks an algorithm into smaller units called subalgorithms. These units are referred by various
names in programming languages such as functions, subroutines, procedures, methods, and modules. The
goal of modular design in algorithms is to make the complex and lengthy algorithms easy to read, write,
verify, and debug. Each subalgorithm can, in turn, be divided into subalgorithms, and the process of such
subdivision may continue till each step becomes effective.
Example : Algorithms 1.6 computes the number of possible ways of arranging any r of n elements.
Pre None
Post None
Return Result
1. Read n and r
2.Let
(a) A = FACT(n) and
(b) B = FACT(n − r)
3.Result = A / B
4.Print Result
5.Stop
10

Here FACT is the subalgorithm to compute the factorial of a number as


n! = n × (n - 1) × (n - 2) × ... × 1
subalgorithm FACT
1.Read n 2.Let Result = 1 3.while(n not equal to 1) do Result = Result × n
n=n–1 end while 4.Return Result
FlowChartS
A very effective tool to show the logic flow of a program is the flowchart. A flowchart is a pictorial
representation of an algorithm. It hides all the details of an algorithm by giving a picture; it shows how the
algorithm flows from beginning to end. In a programming environment, it can be used to design a complete
program or just a part of the program.
The primary purpose of a flowchart is to show the design of the algorithm. At the same time, it relieves the
programmers from the syntax and details of a programming language while allowing them to concentrate on
the details of the problem to be solved. This is in contrast to another programming design tool, the
pseudocode, which provides a textual design solution.
Both tools have their advantages, but a flowchart has the pictorial power that other tools lack. Figure 1.6 is a
flowchart that describes the process of reading, adding, printing three numbers, and printing the result.
11

You might also like