0% found this document useful (0 votes)
25 views32 pages

Unit 1

Uploaded by

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

Unit 1

Uploaded by

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

DATA STRUCTURE USING C++

MODULE 1

 Introduction to data structures: Introduction, Basic terminology;


Elementary Data organization, Structures, Data Structure Operations
 Introduction to Algorithms: Introduction, Algorithmic notations,
Control structure.
 Recursion: Definition; Recursion Technique Examples –Factorial,
Fibonacci sequence, Towers of Hanoi.
Definition of data structure
• A data structure is a method of storing data in a computer,
so that it can be used efficiently.
• Data may be organized in many different ways; the logical
or mathematical model of a particular organization of data
is called a data structure.
Data structures mainly deals with
• The study of how the data is organized in the memory.
• How efficiently, data can be stored in the memory.
• How efficiently, the data can be retrieved and manipulated.
Advantages
1. Data structures allow information storage on hard disks.
2. Provides means for management of large dataset such as databases.
3. Are necessary for design of efficient algorithms.
4. Allows safe storage of information on a computer. The information is
then available for later use and can be used by multiple programs.
5. Allows the data use and processing on a software system.
6. Using internet, we can access the data anytime from any connected
machine (computer, laptop, tablet, phone ,etc.).
Disadvantages
1. Only advanced users can make changes to data structures.
2. Any problem involving data structure will need an expert's
help, i.e. basic users cannot help themselves.

Basic terminology: elementary data organization


Data - Data are simply values or sets of values.
Data item - Refers to a single unit of values.
Group items - Data items are divided into sub items called
group items; those that are not are called elementary items.

items- first name, middle name, and last name.


But the phone number would be treated as a single item.
Classification of data structures
Data structures are generally classified into primitive and non-
primitive data structures.
Primitive data structures
• Primitive data structures are the data structure that can be
manipulated directly by the machine instructions.
• For example, Basic data types such as integer, real, character
and Boolean are known as primitive data structures.

Non-primitive data structures


• Non-primitive data structures are the data structure that cannot
be manipulated directly by machine instructions.
• For example : Linked lists, stacks, queues, trees and graphs are
examples of non-primitive data structures.
Linear Data Structure
• A data structure is said to be linear if its elements form
a sequence or a linear list.
• Arrays, linked lists, stacks and queues are examples of
linear data structures.

Non Linear Data Structure


• Similarly, a data structure is said to be non-linear if the
data is not arranged in sequence.
• Trees and graphs are examples
Arrays
The array stores a fixed-size sequential collection of elements of
the same type.
A is an array, then the elements of A is denoted by A(1),A(2),
……..A(n) or A[1],A[2],…..A[n] or a1,a2,…..an.
Linear arrays are called one dimensional arrays.
Linked list:
A linked list is a non-sequential collection of data items. For every data item in the linked list,
there is an associated pointer that gives the memory location of the next data item in the linked list.
Single linked list: Singly linked lists contain two “buckets” in one node; one bucket holds the data
and the other bucket holds the address of the next node of the list. Traversals can be done in one
direction only as there is only a single link between two nodes of the same list.

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

Steps, control, exit


The steps of the algorithm are executed one after another, beginning with step 1.
Control may be transferred to step n of the algorithm by the statement “go to step n”.
The algorithm is completed when the statement Exit is encountered.
Comments
Each step may contain a comment in brackets which indicates the main purpose
of the step. The comment will usually appear at the beginning or the end of the
step.
Variable names
Variable names will use capital names as MAX.

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.

The conditional structures fall into three types. They are


• Single alternative
• Double alternative
• Multiple alternatives
Single alternative

If condition, then:
[Module A]
[End of if structure]

Here, the if condition holds, then module A which consists of one or


more statements is executed; otherwise Module A is skipped and control
transfers to the next step of the algorithm
Double alternative

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.

A recursive procedure must have the following 2 properties.


1. There must be certain criteria, called base criteria, for which the
procedure does not call itself.
2. Each time a procedure calls itself, it must be closer to the base criteria.

A recursive procedure with these two properties is said to be well defined.


Examples:
• Factorial function
• Fibonacci function
• Towers of Hanoi
Factorial function:
The product of positive integers from 1 to n, is called” n factorial” and is usually
denoted by n!.
N! =1 2 3 .......(N-1) N

Accordingly, the factorial function may also be defined as follows:


a) If n=0, then n! =1
b) If n>0, then n! =n.(n-1)!

Procedure Factorial (Fact, n)


This procedure calculates n! and returns the value in the variable Fact.
1. If n=0 then
2. Set Fact: =1 and return
3. Call Factorial (Fact, n-1)
4. Set Fact: =n*Fact(n-1)
5. Return
Fibonacci sequence:
The Fibonacci sequence is as follows 0,1,1,2,3,5,8,13

a) If n=0 or n=1, then Fn=n


b) If n>1, then Fn=Fn-2+Fn-1

Procedure FIBONACCI (Fib, n)


1. If n=0 or n=1 then
2. Set Fib:=n and return
3. Call Fibonacci(Fib a, n-2)
4. Call Fibonacci(Fib b, n-1)
5. Set Fib:=Fib a+Fib b
6. Return
Towers of Hanoi:

The Tower of Hanoi is a mathematical puzzle invented by the French mathematician


Edouard Lucas in 1883.
There are three pegs, source(A), Auxiliary (B) and Destination(C).
Peg A contains a set of disks stacked to resemble a tower, with the largest disk at the
bottom and the smallest disk at the top.

figure 1 Illustrate the initial configuration of the pegs for 3 disks.


The objective is to transfer the entire tower of disks in peg A to peg C maintaining the same
order of the disks.
Rules:
1. Only one disk can be transfer at a time.
2. Each move consists of taking the upper disk from one of the peg and placing it on the
top of another peg i.e. a disk can only be moved if it is the uppermost disk of the peg.
3. Never a larger disk is placed on a smaller disk during the transfer.

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

You might also like