Data Structure &
Algorithms
in C++
Lecture No. 01
1
Data:
The basic and essential part of the any organization is data
Data is the only source that is collected, stored and processed
A data item refers to a single unit of values.
Data items can be divided into two groups
1)Group item 2)Elementary item
Group item: Data items that are divided in to sub items are
called grouped items e.g. Name is a group item it is divided
into first name , middle name and last name.
Elementary item: Those that are not are called elementary
items e.g. a passport number or religion which one follows
are not subdivided and are elementary items
2
Information:
The term ‘Information’ is sometimes used for data with given
attributes
In other words meaningful or processed form of data is called
information
Information can be used for decision making while data can not
be
3
Introduction:
Data Structure
Data Structure is the systematic way of organizing and accessing
data.
In other words, a data structure is a way to store and organize
data in order to facilitate access and modifications.
Data Structure can be classified as:
1. Primitive
2. Non-Primitive
4
Types of Data Structures:
Primitive Data Structure: Basic data types such as integers,
characters and Boolean are known as primitive data structures.
They are known to be simple data types as they consists of those
characters that can not be further divided.
Non-Primitive Data Structure: The simplest example of the
non-primitive data structure is the processing of complex
numbers. The other examples are arrays, linked-lists, stacks,
queues, trees and graphs.
5
Data Structures:
Array
Linked List
Queue
Tree Stack
6
Data Structures:
Array: Fixed-size
Linked-list: Variable-size
Stack: Add to top and remove from top
Queue: Add to back and remove from front
7
Data Structure Selection:
No single data structure works well for all purposes, and so it is
important to know the strengths and limitations of several of
them
A structure should be simple enough that one can effectively
process the data when necessary.
Choosing a wrong data structure can cause serious damage like:
Program run-time
Utilization of resources
8
Operations on Data Structure:
A particular data structure that one chooses for a given
situation depends largely on the frequency with which specific
operations are performed.
The following four operations play a major role:
1. Insertion
2. Deletion
3. Traversing
4. Searching
9
Operations on Data Structure:
1.Insertion: Adding new record to the structure and putting the
data
2.Deletion: Removing a record from the structure
3.Traversing: Traversing means passing over or tracking.
Accessing each record exactly once so that certain
items in the record may be processed
4.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
10
Algorithm:
Algorithm is a step-by-step procedure/process for performing
some task in a finite amount of time
Algorithms can be achieved by
Human Language: Natural language
Pseudo-code: Natural language + computer programming
language mix
Graphical Representation: Flowcharts and activity
diagrams
11
Algorithm expressed in Human Language:
For example:
Create a program that will determine the number of times a
name occurs in a list.
1. Get the list of names [namelist]
2. Acquire the name you want to look for [keyname]
3. Now compare the acquire name by searching against every
name in the list
4. If a match occurs[keyname]&[namelist], start your count
[1, 2, 3 and so on]
5. If all names are searched and compared, display the count as
a result
12
Algorithm expressed in Pseudo-code:
For example:
Create a program that will determine the number of times a
name occurs in a list.
1. Let nameList=List of the different names
2. Let keyname=The name which we have to look for
3. Let Count=0
4. For each name in namelist do the following
1. If name==keyname
2. Count=count+1
5. Display Count
13
Graphical representation of Algorithm:
14
Data Types:
While doing programming in any programming language, you
need to use various variables to store various information.
Variables are nothing but reserved memory locations to store
values. This means that when you create a variable you reserve
some space in memory.
Data types
long double
double
float
unsigned long int (synonymous with unsigned long)
long int (synonymous with long)
unsigned int (synonymous with unsigned)
int
unsigned short int (synonymous with unsigned short)
short int (synonymous with short)
unsigned char
char
bool (false becomes 0, true becomes 1)
Fig. 3.5 Promotion hierarchy for built-in data types. 15
Data Types:
16
Data Types:
17
Data Types:
Adding two numbers
18
Data Types:
Variable names
Correspond to actual locations in computer's memory
Every variable has name, type, size and value
When new value placed into variable, overwrites previous
value
integer1 45
std::cin >> integer1;
Assume user entered 45
integer1 45
std::cin >> integer2; integer2 72
Assume user entered 72
integer1 45
sum = integer1 + integer2; integer2 72
sum 117
19
Basics of Typical C++ Environment:
Phases of C++ Program Editor
Program is created in
Disk the editor and stored
1.Edit on disk.
Preprocessor Preprocessor program
2.Preprocess Disk
processes the code.
Compiler creates
3.Compile Compiler Disk object code and stores
it on disk.
4.Link Linker links the object
Linker Disk code with the libraries,
5.Load creates an executable
Primary file and stores it on disk
6.Execute Loader
Memory
Primary
Memory
Loader puts program
CPU in memory.
CPU takes each Disk ..
instruction and ..
..
executes it, possibly
storing new data
.. Primary
.. values as the program Memory
..
executes.
20
Array:
Array is:
Consecutive group of memory locations
Same name and type (int, char, etc.)
Array
To refer to an element
0 1 2 3 4
Specify array name and position number (index) in square
brackets
Format: arrayname[ position number ]
First element at position 0
For Example:
c[ 0 ], c[ 1 ] … c[ n - 1 ]
Nth element as position N-1
21
Array:
Assignment operator, printing for an integer array c
c[ 0 ] = 3;
cout << c[ 0 ];
You can perform operations inside subscript
c[ 5 – 2 ] same as c[ 3 ]
22
Declaring Array:
When declaring arrays, specify
Name
Type of array
Any data type
Number of elements
Type arrayName[ arraySize ];
int c[ 10 ]; // array of 10 integers
float d[ 3284 ]; // array of 3284 floats
Declaring multiple arrays of same type
Use comma separated list, like regular variables
int b[ 100 ], x[ 27 ];
23
Declaring Array:
Array of Strings
Arrays of characters
All strings end with null ('\0')
Examples
char string1 [ ] = "hello";
Null character implicitly added
string1 has 6 elements
char string1 [ ] = { ‘h’, ‘e’ , ‘l’ , ‘l’ , ‘o’ , ‘\0’ };
Subscripting is the same
String1[ 0 ] is 'h'
string1[ 4 ] is ‘o'
24
Questions / Answers
Session
25