Lecture 4 DS
Lecture 4 DS
and Algorithm
21COMP04C
2023
Lecture4
1
Labs and and tutorial
• Labs rearrangement
• Tutorial will be converted to labs till class test
• Tutorial converted labs will take place in Lab 128
• We need one tutorial
• will be arranged to be one hour online session in week 5 (After Monday ) or
in Week 6 (included in class test)
• Lab3 submission
• Labs are ungraded
• Lab 3 submission is next Sunday
2
Our Course
Lecture Lecture Lecture Lecture
Lecture 4
1,2,3 5,6,7,8 9,10 11,12
Array Linked
OO Searching Trees
based lists lists
Algorithm
Recursion Graphics
analysis
Lecture Resources
Data-Structure Using C++ by D.S. Malik (ch 2,5)
Operations
Searching (find the location)
Inserting (add new element)
Deleting
Sorting
Source: https://www.hellocodeclub.com/when-to-use-which-data-structure-top-6-data-structures
12
Operations on data structures
• Traversing (access one element or more)
• Searching (find the location)
• Inserting (add new element)
• Deleting
• Sorting
• Merging
13
Abstract Data Type (ADT)
• An abstract data type (ADT) is the way we look at a data structure, focusing on what it does and ignoring
how it does its job.
• For example, stacks and queues are perfect examples of an ADT. We can implement both these ADTs using
an array or a linked list. This demonstrates the ‘abstract’ nature of stacks and queues.
• In ADT we separate the use of a data structure from the details of its implementation. This is the principle
underlying the use of abstract data types.
You can implement
Stacks and Queues
using an array or a
linked list
Source: https://www.geeksforgeeks.org/abstract-data-types/
Source: https://techvidvan.com/tutorials/java-abstract-data-type/
14
Lists
Array-based
18
Array-based list
ADT
implementation as
template
Default parameters in page 32
22
Array-Based Lists (cont’d.)
• Definitions of functions isEmpty, isFull, listSize and maxListSize
O(1)
O(1)
28
Array-Based Lists (cont’d.)
• Inserting an element
29
Array-Based Lists (cont’d.)
• Removing an element
30
For classes with pointer member variables,
three things are normally done:
1. Include the destructor in the class.
2. Overload the assignment operator for the
class.
3. Include the copy constructor.
Copy constructor
• The copy constructor automatically executes in three situations
• when object passed as a (value) parameter to a function
• when object declared and initialized using the value of another object of the
same type
• When the return value of a function is an object
• Only needed when an object owns pointers or non-shareable
reference. But also you need to do the following two
1. Include the destructor in the class.
2. Overload the assignment operator for the class.
32
Array-Based Lists
• Copy constructor
• Definition
O(n)
33
Array-Based Lists (cont’d.)
• Overloading the assignment operator
• Definition of the function template
34
Array-Based Lists (cont’d.)
TABLE 3-1 Time complexity of list operations
void main(){
{
listType<int> intList;
listType<string> stringList;
intList.print();
}