0% found this document useful (0 votes)
6 views

Lecture 4 DS

The document outlines the structure and content of a course on Data Structures and Algorithms, including lab arrangements, tutorial sessions, and submission deadlines. It covers various topics such as array-based lists, linked lists, templates, and abstract data types, along with their operations and implementations in C++. Additionally, it highlights the importance of function and class templates in C++ programming.

Uploaded by

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

Lecture 4 DS

The document outlines the structure and content of a course on Data Structures and Algorithms, including lab arrangements, tutorial sessions, and submission deadlines. It covers various topics such as array-based lists, linked lists, templates, and abstract data types, along with their operations and implementations in C++. Additionally, it highlights the importance of function and class templates in C++ programming.

Uploaded by

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

Introduction to Data Structures

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

Pointers Template Stacks Sorting Queues

Algorithm
Recursion Graphics
analysis

Functions to Other data


Basic information First Data structure Main focus of the
process data structure
Very important course
structure
3
The Pointer this Class test
• A member function of a class {
can (directly) access the data
members of that class for a …………
given object. Sometimes it is test test::funcOne()
necessary for a function
member to refer to the object { ...
as a whole, rather than the
object’s individual data return *this;
members
• Every object of a class }
maintains a (hidden) pointer to };
itself, and the name of this
pointer is this.
• In C++, this is a reserved word. Void main()
• The pointer this is available for
you to use {
test x,y;
y = x.funcOne();
}
Lecture 4
Templates
Array-based list

Lecture Resources
Data-Structure Using C++ by D.S. Malik (ch 2,5)

These slides are adapted for the text book's slides 5


Templates
Function Overloading
int largerInt(int x, int y);
char largerChar(char first, char second);
double largerDouble(double u, double v);
string largerString(string first, string second);

int larger(int x, int y);


char larger(char first, char second);
double larger(double u, double v); larger(5,3)
string larger(string first, string second); larger('A', '9')
Templates
• Templates are very powerful features of C++.
• Templates cab be
• Function template (used in function overloading)
• Class template
• write a single code segment for a set of related functions, called a
function template, and for related classes, called a class template.
Function Templates
int larger(int x, int y);
char larger(char first, char second);
double larger(double u, double v);
string larger(string first, string second);

template <class T>


T larger(T x, T y)
{ larger(5,3)
if (x >= y) larger('A', '9')
return x;
else
return y;
}
template <class e>
void two_numbers<e>::print() const
Class Templates {
cout << x<<y<<endl;
}
template <class t> template <class element>
class two_numbers element two_numbers<element>::sum() const
{ {
private : element z;
t x,y; z=x+y;
public : return z;
two_numbers(t, t); }
t sum() const; void main(){
void print() const; {
two_numbers<int> A(5,3);
}; two_numbers<float> B(2.5,5.0001);
A.print(); //will print 53
cout<<B.sum(); //will print 7.5001
}
Templates
Array-based list
Lecture Resources
Data-Structure Using C++ by D.S. Malik (ch 2,5)

These slides are adapted for the text book's slides


Data structures Examples

You can implement Stacks


and Queues using an array
or a linked list

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

These slides are adapted for the text book's slides


List Array based list ADT
• Collection of elements of same type • Data
• Length of a list • Array to store the elements
• Number of elements in the list • int length
• Int Maxsize
• Many operations may be performed on a list
1. Create the list. The list is initialized to an empty • Operations
state.( no elements is stored) 1. Create the list. The list is initialized to an empty
2. Determine whether the list is empty. state.( no elements is stored) constructor
3. Determine whether the list is full. 2. Determine whether the list is empty.
4. Find the size of the list. 3. Determine whether the list is full.
5. Destroy, or clear, the list. 4. Find the size of the list.
6. Determine whether an item is the same as a 5. Destroy, or clear, the list.
given list element. (compare to element) 6. Determine whether an item is the same as a given
7. Insert an item in the list at the specified list element. (compare to element)
location. (insert) 7. Insert an item in the list at the specified location.
8. Remove an item from the list at the specified (insert)
location. (remove or delete) 8. Remove an item from the list at the specified
9. Replace an item at the specified location with location. (remove or delete)
another item. (replace) 9. Replace an item at the specified location with
10. Retrieve an item from the list from the another item. (replace)
specified location. (Retrieve) 10. Retrieve an item from the list from the specified
11. Search the list for a given item. (search) location. (Retrieve)
11. Search the list for a given item. (search)
16
Array-based Lists (sequential Lists)
• Because all the elements of a list are of the same type, an effective and
convenient way to process a list is to store it in an array.
• Size of the array is the max number of elements that can be stored in the
list.
• Three variables needed to maintain and process a list in an array
• The array holding the list elements
• A variable to store the length of the list (length)
• Number of list elements currently in the array
• A variable to store array size (max size)
• Maximum number of elements that can be stored in the array
• Desirable to develop generic code
• Used to implement any type of list in a program
• Make use of templates (class template)

Data Structures Using C++ 2E 17


Array-Based Lists
• Define class implementing list as an abstract data type (ADT)
• This list can be list of:
• int
• double
• Students
• (student is a class)

18
Array-based list
ADT
implementation as
template
Default parameters in page 32

Copy Constructor in page 159

• in public inheritance: Protected members of base will be protected to derived class


• In private inheritance: Protected members of base will be private to derived class
• But private members of base class are hidden to derived class in both public or private inheritance
Array-Based Lists
• Definition of the constructor and the destructor

22
Array-Based Lists (cont’d.)
• Definitions of functions isEmpty, isFull, listSize and maxListSize

O(1)

Data Structures Using C++ 2E 23


Array-Based Lists (cont’d.)
• Template print (outputs the elements of the list) and template O(n)
isItemAtEqual

O(1)

Data Structures Using C++ 2E 24


Array-Based Lists (cont’d.)
• Template insertAt

Data Structures Using C++ 2E 25


Array-Based Lists (cont’d.)
• Template insertEnd and template removeAt

Data Structures Using C++ 2E 26


Array-Based Lists (cont’d.)
• Template replaceAt and template clearList

Data Structures Using C++ 2E 27


Array-Based Lists (cont’d.) FIGURE 3-32 List of seven elements
• Searching for an element
• Linear search example: determining if 27 is in the list
• Definition of the function template

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

Data Structures Using C++ 2E 35


New instructions

• cerr is the same as cout but used for error messages

• assert terminate the program in case of major error


• Need to include <assert.h> for assert function to work
Another Example template <class elemType>
Class Templates void listType<elemType>::print() const
{
for (int i = 0; i < length; i++)
cout << list[i] << " ";
cout << endl;
}

void main(){
{
listType<int> intList;
listType<string> stringList;
intList.print();
}

You might also like