Unit 1
Unit 1
MODULE 1
Double linked list:Doubly Linked Lists contain three “buckets” in one node; one bucket holds the
data and the other buckets hold the addresses of the previous and next nodes in the list. The list is
traversed twice as the nodes in the list are connected to each other from both sides.
Circular linked lists can exist in both singly linked list and doubly linked list. Since the last node
and the first node of the circular linked list are connected, the traversal in this linked list will go on
forever until it is broken.
Stack
A stack, also called as last-in first-out (LIFO) system, is a linear list in which
insertions and deletions can take place only at one end, called top.
Queue: A queue also called as first in first out system is a linear list in which
deletions can take place only at one end of the list, the front of the list, and the
insertions can take place only at the other end of the list, the rear of the list.
Trees:
• A tree is a non-terminal data structure in which items are arranged in a
sorted sequence.
• It consists of nodes (where the data is stored) that are connected via links.
The tree data structure stems from a single node called a root node and has
subtrees connected to the root.
Graphs:
Data sometimes contain a relationship between pairs of elements which is not necessarily
hierarchical in nature. This is called graph.
Data structure operations:
There are total 6 data structures
1. Traversing – accessing each record exactly once so that certain items in
the record may be processed.
2. Searching - finding the location of the record with a given key value or
finding the locations of all records which satisfy one or more conditions.
3. Insertions - adding a new record to a structure.
4. Deleting - removing a record from a structure.
5. Sorting - arranging the records in the some logical order.
6. Merging - combining the records in two different sorted files into a
single sorted file.
UNIT 1
CHAPTER 2
INTRODUCTION TO ALGORITHMS
What is an Algorithm?
In computer programming terms, an algorithm is a set of well-defined instructions
to solve a particular problem. It takes a set of input(s) and produces the desired
output.
Algorithmic notation
An algorithm is a finite step-by-step list of well-defined instructions for
solving a particular problem
Step 1: [initialize]
Set K:=1, LOC:=1 and MAX:=A[1]
Step 2: [Increment counter]
Set K:=K+1
Step 3: [Test counter]
If K>N then:
Write: LOC, MAX and EXIT.
Step 4: [Compare and update]
If MAX < DATA[K],
then: Set LOC:=K and MAX:=A[K]
Step 5: [Repeat loop] Go to step 2
Following are some algorithmic notations
1. Steps, control, exit
2. Comments
3. Variable names
4. Assignment statement
5. Input and output
6. Procedures
Assignment statement
Our assignment statements will use the : = notation.
Input and output
Data may be input and assigned to variables by means of a Read statement
with the following form: Read (variable names).
Write statement in the following form Write (message and/or variable
names.)
Procedures
Procedure is an independent algorithmic module which
solves a particular problem.
Control structures
There are three types of logic or flow of control.
1. Sequential logic or sequential flow
2. Selection logic or conditional flow
3. Iteration logic or repetitive flow
Sequential logic
Instructions are executed in the sequential order. The sequence may be
presented explicitly by means of numbered steps or implicitly, by the
order in which modules are written.
Selection logic
Selection logic employs a number of conditions which lead to a
selection of one out of several alternative modules.
The structures which implement this logic are called conditional
structures or if structures.
If condition, then:
[Module A]
[End of if structure]
If condition, then:
[Module A]
Else
[Module B]
[End of if structure]
The logic of this structure is, if the condition holds, then module A is
executed; otherwise Module B is executed
Multiple alternatives:
If condition(1), then:
[Module A1]
Else if condition(2), then:
[Module A2]
.
.
.
Else if condition (M), then:
[Module AM]
Else: [Module B]
[End of if structure]
The logic of this structure allows only one of the modules to be executed. Specifically,
either the module which follows the first condition which holds is executed, or the module
which follows the final Else statement is executed. In practice, there will rarely be more
than three alternatives.
Iterative logic (repetitive flow)
Each type begins with a Repeat statement and is followed by a module, called the
body of the loop. We will indicate the end of the structure by the statement [End of
loop] or some equivalent.
The repeat-for loop uses an index variable, such as K to control the loop. The loop
will usually have the form:
Repeat for k=R to S by T:
[Module]
[End of loop]
Here R is the initial value. S is the end value or test value, and T is the increment.
The repeat-while loop uses a condition to control the loop. The loop will usually
have the form
Repeat while condition:
[module]
[end of loop]
The looping continues until the condition is false.
Local and global variables
Each program module contains its own list of variables called local variables which are accessed
only by the given program module.
Variables that can be accessed by all program modules are called global variables
SUB ALGORITHMS
• A sub algorithm is a complete and independently defined algorithmic module which is used by
some main algorithm or by some other sub algorithm.
• A sub algorithm receives values called arguments from a calling algorithm; performs
computations and then sends back the result to the calling algorithm.
• Sub algorithm will have a return statement rather than an Exit statement.
• Sub algorithms fall into 2 categories:
1. function sub algorithms
2. procedure sub algorithms.
• The major difference between the two is that the function sub algorithm returns only a single
value to the calling algorithm whereas the procedure sub algorithm may send back more than one
value.
UNIT 1
CHAPTER 3
RECURSION
Recursion is the name given to the technique of defining a set or a process
in terms of itself. A recursive procedure can be called from within or
outside itself.
Algorithm:
TOH( n, Sour, Aux , Des)
If(n=1)
Write ("Move Disk “, n ," from ", Sour ," to ",Des)
Else
TOH(n-1,Sour,Des,Aux);
Write ("Move Disk “, n ," from ", Sour ," to ",Des)
TOH(n-1,Aux,Sour,Des);
END
Move Disk 1 from A to C
Move Disk 2 from A to B
Move Disk 1 from C to B
Move Disk 3 from A to C
Move Disk 1 from B to A
Move Disk 2 from B to C
Move Disk 1 from A to C