Data Structures and Algorithms: CS3007 CEN2018

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 27

Data Structures and

Algorithms
CS3007
CEN2018
Dr. Mussadiq Abdul Rahim
About the Instructor
• PhD in Computer Science and Technology from Beijing Institute of
Technology, China.
• Assistant Professor, Department of Computer Science at NUTECH.
• For any concern regarding this course you may reach via email or may
visit office during office hours.
• Email: mussadiq@nutech.edu.pk
• Office: CS Faculty Offices, Level-4, Academic Block.
About the Course
• Pre-requisite Course -- CS2005 Object Oriented Programming / CEN
2016 Object Oriented Programming
• We aim to learn to
• Show how data structures are represented in the computer,
• Identify linear and nonlinear data structures,
• Manipulate data structures with basic operations,
• Compare different implementations of the same data structure,
• Perform complexity analysis of algorithms,
• Implement multiple algorithms of different types such as sorting algorithms,
tree-based algorithms, and
About the Course
• We aim to learn
• Different data structures such as stacks, queues, link lists, and other.
• Tree and graph based algorithms.
• Analyze algorithms in terms of complexities.
• The programming language for this course is C++.
• The textbook followed in this course is
• Data Structures and Algorithm Analysis in C++ by Mark Allen Weiss (fourth ed.)
• The additional reference books for this courses are
• Data Structures and Algorithms in C++ by Adam Drozdek
• Data Structures using C by Aaron M. Tenenbaum et al.
About the Course
• Online reference for C++
• C++ Language, http://www.cplusplus.com/doc/tutorial/
• Microsoft C++ Language Reference,
https://docs.microsoft.com/en-us/cpp/cpp/cpp-language-reference?view=vs-
2019
• Online C++ compiler
• Multiple available, use for practice outside class only.
Tips for effective learning
• Keep your phones on silent.
• Keep discussions out of the classroom.
• Be punctual. The class attendance will be called out at any moment
during class.
• Don’t need to ask for leaving the classroom for any urgent matter or
personal call, leave and return quietly without any unnecessary
delays.
• Question freely about the content of lecture. Questions regarding
topic are encouraged.
Rules and guidelines
• There is a 20% deduction for late submission. The deduction becomes
50% if the submission is one-day late. No credit is given after two days.
• Do not recycle code from the Internet. Write your own, if you wish
success in this course.
• Identify yourself–Name, student ID, assignment number, Course Code
and Batch.
• Comment everything–Logic of your program. Purpose of variables, data
structures, functions, and classes.
• Naming style–For example, use lowercase for variables and uppercase
for constants.
Rules and guidelines
• Indentation–4 or 8 spaces of indentation
• Simplicity
• Read after your write, if you can’t read it, discard.
• Avoid multiple nesting of ifs, be smart use conditional operators.
• Functions must be reasonably short, use logic.
COVID-19 and our responsibility

Wear Mask Properly


COVID-19 and our responsibility

Keep Social Distance


COVID-19 and our responsibility

Wash Hands Regularly


COVID-19 and our responsibility

Use Hand Sanitizer


Week # 1: Overview/Revision of
Object Oriented Programming In
C++
Primitive data types
Type Size (Bytes) Data
char 1 Character
short 2 Integer
int 4 Integer
float 4 Real
double 8 Real
long 8 Real
C++ and OOP
• An lvalue is an expression that identifies a non-temporary object.
• An rvalue is an expression that identifies a temporary object or is a
value (such as a literal constant) not associated with any object.
• As a general rule, if you have a name for a variable, it is an lvalue,
regardless of whether it is modifiable.
• In C++11, an lvalue reference is declared by placing an & after some
type.
• string str = "hell";
• string & rstr = str; // rstr is another name for str
• rstr += ’o’; // changes str to "hello"
C++ and OOP
• In C++11, an rvalue reference is declared by placing an && after some
type. An rvalue reference has the same characteristics as an lvalue
reference except that, unlike an lvalue reference, an rvalue reference
can also reference an rvalue (i.e., a temporary).
• string & bad1 = "hello"; // illegal: "hello" is not a modifiable lvalue
string & bad2 = str + ""; // illegal: str+"" is not an lvalue
string & sub = str.substr( 0, 4 ); // illegal: str.substr( 0, 4 ) is not an lvalue

• string && bad1 = "hello"; // Legal


string && bad2 = str + ""; // Legal
string && sub = str.substr( 0, 4 ); // Legal
C++ and OOP
• lvalue references use #1: aliasing complicated names
• auto & whichList = theLists[ myhash( x, theLists.size( ) ) ];
• lvalue references use #2: range for loops
• for( auto & x : arr ) // works
++x;
• lvalue references use #3: avoiding a copy
• auto & x = findMax( arr );
C++ and OOP
• To see the reasons why call-by-value is not sufficient as the only
parameter-passing mechanism in C++, consider the three function
declarations below:
• double average( double a, double b ); // returns average of a and b
void swap( double a, double b ); // wrong parameter types
string randomItem( vector<string> arr ); // inefficent
• void swap( double & a, double & b ); // correct parameter types
• With this signature, a is a synonym for x, and b is a synonym for y.
• Changes to a and b in the implementation of swap are thus changes to x
and y. This form of parameter passing has always been known as call-by-
reference in C++.
C++ and OOP
• A pointer variable is a variable that stores the address where another
object resides. It is the fundamental mechanism used in many data
structures.
• For instance, to store a list of items, we could use a contiguous array,
but insertion into the middle of the contiguous array requires
relocation of many items.
• m = new IntCell( ); // OK
• m = new IntCell{ }; // C++11
• m = new IntCell; // Preferred
C++ and OOP
• Syntax for pointers to declare, initialize, access a member, and delete
the allocated object
• IntCell *m; //declare a pointer
• m = new IntCell{ 0 }; //create an object with a value
• m->write( 5 ); //access a member function
• delete m; //delete the runtime allocated object
• When an object that is allocated by new is no longer referenced, the
delete operation must be applied to the object (through a pointer).
Otherwise, the memory that it consumes is lost (until the program
terminates).
C++ and OOP
• When an object that is allocated by new is no longer referenced, the
delete operation must be applied to the object (through a pointer).
• Otherwise, the memory that it consumes is lost (until the program
terminates). This is known as a memory leak.
• One important rule is to not use new when an automatic variable
can be used instead.
C++ and OOP
• A class in C++ consists of its members.
• These members can be either data or functions.
• The functions are called member functions.
• Each instance of a class is an object. Each object contains the data
components specified in the class.
• A member function is used to act on an object. Often member
functions are called methods.
C++ and OOP
• A member that is public may be accessed by any method in any class.
• A member that is private may only be accessed by methods in its
class.
• Typically, data members are declared private, thus restricting access
to internal details of the class, while methods intended for general
use are made public. This is known as information hiding or
encapsulation.
C++ and OOP
• A constructor is a method that describes how an instance of the class
is constructed.
• If no constructor is explicitly defined, one that initializes the data
members using language defaults is automatically generated.
• Default constructor is used to initialize member data with initial
values. Default parameters can be used in any function, but they are
most commonly used in constructors.
• Parameterized constructor is used to initialize member variables
through parameters or allocate storage.
C++ and OOP
• You should make all one-parameter constructors explicit to avoid
behind-the-scenes type conversions.
• explicit IntCell( int initialValue = 0 ):storedValue{ initialValue } { }
• IntCell obj3 = 37; // Constructor is explicit
• If the constructor is not made explicit then the implicit conversions
will result in hard to find bugs.
C++ and OOP
• Copy constructor is a constructor which creates an object by initializing it with an object
of the same class, which has been created previously. The copy constructor is used to −
• Initialize one object from another of the same type.
• Copy an object to pass it as an argument to a function.
• Copy an object to return it from a function.
• Example
• IntCell( const IntCell & rhs ); // Copy constructor
• A member function that examines but does not change the state of its object is an
accessor or a getter function.
• By default, all member functions are mutators. To make a member function an
accessor, we must add the keyword const after the closing parenthesis that ends the
parameter type list.
Reading Assignment
• Text Book: 1.2, 1.3, 1.4, 1.5

You might also like