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

Lecture 1 - Introduction PDF

This document outlines the expectations and topics for a data structures course taught by Professor Shaker El-Sappagh of Galala University. The course will cover common data structures like lists, stacks, queues, trees, and graphs implemented using arrays and linked lists in C++. Students will learn algorithm analysis and sorting/searching algorithms. The main textbook is "Data Structures Via C++" and assessments include a midterm, final exam, and assignments.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

Lecture 1 - Introduction PDF

This document outlines the expectations and topics for a data structures course taught by Professor Shaker El-Sappagh of Galala University. The course will cover common data structures like lists, stacks, queues, trees, and graphs implemented using arrays and linked lists in C++. Students will learn algorithm analysis and sorting/searching algorithms. The main textbook is "Data Structures Via C++" and assessments include a midterm, final exam, and assignments.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

Data Structures via C++

What is data structure?


Professor: Shaker El-Sappagh
Galala University
[email protected]

1 Shaker El-Sappagh Data structures 10/20/2021


Outline
• Course expectations
• Data structures definitions.

2 Shaker El-Sappagh Data structures 10/20/2021


Course expectations

3 Shaker El-Sappagh Data structures 10/20/2021


1. Covered topics
• Definition of data structures
• Quick review of C++ and object-oriented programming
• Algorithm complexity analysis
• Sorting algorithms and searching algorithms
• Recursion
• Lists (array and linked list)
• Stack using array and linked list
• Queue using array and linked list
• Hashing using array and linked list
• Tree using linked lists
• Graph using linked lists

4 Shaker El-Sappagh Data structures 10/20/2021


2. The Book
The official book that I will use for this course is:
"Data Structures Via C++: Objects by Evolution", A. Michael Berman, , Oxford Univ. Press (2007)

However, I will use several materials and exercises and labs problems will be collected from several books.

5 Shaker El-Sappagh Data structures 10/20/2021


3. C++ IDE
C++ Development Environment: Since this is a programming course, you will also require
a development environment to compile and run your programs. Students are assumed to be
able to install one of the following IDEs. Note that the list is ordered from the highest
priority to the lowest.
Name Link
Eclipse https://www.eclipse.org/ide/
C++Builder https://www.embarcadero.com/products/
Visual Studio Code https://code.visualstudio.com/
Netbeans https://netbeans.org/downloads/8.0.1/
Kite https://www.kite.com/get-kite/
Codelite https://codelite.org/
Atom https://atom.io/
CLion https://www.jetbrains.com/clion/
Codeblocks http://www.codeblocks.org/

6 Shaker El-Sappagh Data structures 10/20/2021


4. Grading

Category % Description
Midterm exam 25% Midterm exam
Final Exam 60% Final exam
Collection 15% Assignments, practical exam, attendance, class
participation, quizzes, research, and seminar.

7 Shaker El-Sappagh Data structures 10/20/2021


5. Other points
1. Homework
2. Office hours
3. Communication
4. Collaboration and academic honesty
5. Teaching assistant
6. Exams

8 Shaker El-Sappagh Data structures 10/20/2021


Data structures definitions

9 Shaker El-Sappagh Data structures 10/20/2021


1. Data structure definitions
Definition 1: A data structure is the organization of information, usually in
memory, for better algorithm efficiency (storage and speed) by facilitating
operations while keeping the size of the data.
Definition 2: A data structure consists of a base storage method (e.g., array)
and one or more algorithms that are used to access or modify that data.
Data structure specifies: (1) Organization of data, (2) Accessing methods, (3)
Degree of associativity, (4) Processing alternatives for information.
A data structure is a way of storing data so that it can be used efficiently. An
appropriate data structure allows a variety of important operations to be
performed using resources (memory space and execution time) efficiently.
Types of data structures: build in and user defined.
10 Shaker El-Sappagh Data structures 10/20/2021
1. Data structures definition (Cont’d)
Types of Data Structure:

• In linear data structures: values are arranged in linear fashion.

• In Non-linear data structures: valuesare not arranged in order.

Operations Performed in Data Structure: Traversing, Insertion, Deletion, Merging,


Sorting, Searching

11 Shaker El-Sappagh Data structures 10/20/2021


1. Data structures definition (Cont’d)
Algorithm + Data Structure = Program
• Algorithm: The step-by-step procedure to solve a problem. It is a well-
organized, pre-arranged, and defined computational module that
receives some values or set of values as input and provides a single or
set of values as output. These well-defined computational steps are
arranged in sequence, which processes the given input into output.
• The efficiency of an algorithm depends on the time complexity and
space complexity.
• Complexity is a function of input size.
• Suppose space is fixed for one algorithm then only run time will be
considered for obtaining the complexity of algorithm, these are
1. Best case
2. Worst case
3. Average case
12 Shaker El-Sappagh Data structures 10/20/2021
1. Data structures definition (Cont’d)
• Efficiency of an algorithm: can be determined by measuring the
time, space, and amount of resources it uses for executing the
program.
• The amount of time taken by an algorithm can be calculated by
finding the number of steps the algorithm executes.
• The space refers to the number of units it requires for memory
storage.

13 Shaker El-Sappagh Data structures 10/20/2021


2. Memory: Where everything lives?
We have two memories for a C++ application:
1. Static memory that is not growing while the program
is executing: Compilation time allocation (OS-
dependent).
a. Code instructions,
b. Static and global variables,
c. Stack for local variables and function calls. Heap

2. Dynamic memory that can be grow while the program Stack


is executing: Execution time allocation. Static and global
variables
a. Heap memory: where the objects are living. It has
garbage collection mechanism. Code

14 Shaker El-Sappagh Data structures 10/20/2021


2. Memory (Cont’d)
Example: Stack (1MB from OS)
#include <iostream> int main()
int total; D()
{
int Square(int x) int a = 4, b = 8; C()
{ total = SquareOfSum (a, b); B()
return x*x; cout<<“output = “<< total; A()
} }
int SquareOfSum(int x, int y)
{ Global
int z = Square(x+y);
return z;
}
Code
15 Shaker El-Sappagh Data structures 10/20/2021
2. Memory (Cont’d)
Example: Stack (1MB from OS)
#include <iostream>
int main()
{
Heap
int a;
int *p1, *p2;
p1 = new int;
*p1 = 5;
Heap
p2 = new int[10];
*p2= 100 // as p[0] = 100
Global
*(p2+1) = 200; // as p[1] = 200
p2 = new int [5];
delete p1;
delete[] p2;
Code
} 16 Shaker El-Sappagh Data structures 10/20/2021
2. Memory (Cont’d)
Limitations of the stack memory:
• Stack can go in stack overflow as is memory can not be changed in
run time and its allocation depends on the OS and resources.
• For array allocation, you must know at the compilation time the size
of the array. Static array. Either allocating very large space or be in
risk for stack overflow.

17 Shaker El-Sappagh Data structures 10/20/2021


2. Memory (Cont’d)
Limitations of the heap memory (dynamic memory allocation):
• Heap is a free pool of memory but…
• Heap memory can extend (dynamic memory allocation) while
program execution.
• We can go out of memory of heap if the system memory is low.
• It is the responsibility of the programmer to clean the not needed
memory.

18 Shaker El-Sappagh Data structures 10/20/2021


3. Software development lifecycle: Waterfall methodology

Initiation In a software project:


Develop a high-quality
Analysis
SW, in time, at cost.
Who performs: Design
manager.
Deliverables: Who performs:
request for system analyst.
development. Deliverables: Implementation
functional Who performs:
Steps: feasibility
study, examination specification as a system designer.
part of the legal Deliverables: Who performs: Testing
of alternatives, programmer.
agreement Design, technical,
cost benefit Deliverables:
or coding Maintenance
analysis, make or documented code Who performs:
specification.
buy study and system tester.
documentation. Deliverables: test Who performs:
pan and report. maintenance progr.
For: bugs, adaptations,
enhancements

19 Shaker El-Sappagh Data structures 10/20/2021


3. Software development lifecycle (Cont’d)
Characteristics of a good software system
• Usable: easy to use.
• Reliable: no crash and correct results.
• Maintainable: software problems can be solved easily, and
software updated easily.
• Reusable: software built with reusable components to
reduce cost and improve reliability and should generate
new reusable components that will be used in future.
Good design helps to achieve these goals.

20 Shaker El-Sappagh Data structures 10/20/2021


4. Design methodologies
• Top-down design (TDD): it decomposes a large problem
into smaller ones, each of which can be solved separately.
Coupling and cohesion.
• Object-oriented design (OOD): System is broken down
into objects. Class, object, encapsulation, polymorphism,
overloading, messaging, inheritance, interface, hiding, etc.
• What methodology we will use?!

21 Shaker El-Sappagh Data structures 10/20/2021


Specification: Video Rental System
• Build a software system to support the operation of a video rental store.
The system should automate the process of renting tapes and receiving
returned tapes, including calculating and printing patron bills, which may or
may not be done at the same time the tape is returned. The system must
also give the clerk access to information about tapes, such as the number of
copies on the shelf of any given video owned by the store. The system must
be able to add new customers and tapes to and remove them from the
database. Each patron and each copy of each tape are to be associated with
a unique bar-coded label.

22 10/20/2021
Using TDD method: Structure chart for initial
decomposition of video rental system
Video
Rental
System

Process Process Process


Transactions Queries Modifications

23 10/20/2021
Using TDD method: Structure chart for “process
transactions” Process
Transactions

Rent Return Pay


Tape Tape Bill

Query Validate Update Update Update


Tape Patron Tape Patron Patron

Query Validate Compute Update Update


Tape Patron Bill Tape Patron

24 10/20/2021
Using TDD method: : Final decomposition of video
rental system Video
Rental
System

Process Process Process


Transactions Queries Modifications

Rent Return Pay Tape Patron


Tape Tape Bill Queries Queries

Query Validate Update Update Update Add Delete


Tape Patron Tape Patron Patron Patron Patron

Query Validate Compute Update Update Add Delete


Tape Patron Bill Tape Patron Tape Tape

25 10/20/2021
Using OOD method: Responsibility-driven design
process
• Find the classes

• Determine the responsibilities of each class.

• Determine who collaborates with each class.

26 10/20/2021
Using OOD method: Classes for video rental system
• Tape
• Patron
• Console
• Scanner
• Printer

27 10/20/2021
Using OOD method: Tape class
Class Responsibilities
Tape Keep track of tape identification data
Check self out
Check self in
Answer queries about location of self

10/20/2021 28
Using OOD method: Patron class
Class Responsibilities
Patron Keep track of patron identification data
Update patron identification data
Update list of rented tapes
Update billing information

10/20/2021 29
Using OOD method: Console class
Class Responsibilities
Console Check tapes in and out
Find the location of tapes
Find out about patrons
Add and remove tapes
Add, update, and remove patrons
Update patron billing information

10/20/2021 30
Using OOD method: Scanner class
Class Responsibilities
Scanner Read a bar code and return either a tape
or a patron

31 10/20/2021
Using OOD method: Printer class
Class Responsibilities
Printer Print bills
Print receipts

32 10/20/2021
Using OOD method: Video rental system: Classes,
responsibilities, and collaborators
Class Responsibilities Collaborators
Tape Keep track of tape identification data
Check self in
Check self out
Answer queries about location of self
Patron Keep track of patron identification data Tape
Update patron identification data
Update list of rented tapes
Update billing information
Console Check tapes in and out Patron
Find the location of tapes Tape
Find out about patrons Scanner
Add and remove tapes Printer
Add, update, and remove patrons
Update patron billing information
10/20/2021 33
Using OOD method: Video rental system: Classes,
responsibilities, and collaborator
Class Responsibilities Collaborators
Scanner Read a bar code and return either a tape or a Tape
patron. (This suggests that a database that links Patron
bar codes to tapes and patrons is maintained
by the Scanner class.)
Printer Print bills Tape
Print receipts Patron

34 10/20/2021
4. Design methodologies (Cont’d)
• Abstract data type (ADT): is a well-specified collection of data and a
group of operations that can be performed upon the data.
• ADT’s specification describes what data can be stored (the characteristics
of the ADT) and how it can be used (the Operations), but not how it is
implemented.
• ADT can be defined formally using a programming language or can be
described informally using English.

35 Shaker El-Sappagh Data structures 10/20/2021


5. Software reliability
• Importance of data and risks (costs) of faulty software.
• Importance of SW testing. Testing is the search for SW errors.
• Taxonomy of errors:
syntax errors: violating the formal rules of programming
language.
validity (logical) errors: when a program runs but gives wrong
answers. Results from misunderstanding b/w phases.
verification errors: errors in the requirements analysis by
overlooking important factors (the moon example).
run-time errors: failure to implement design. Cause program
termination or crash as misuse of memory, divide by zero, etc.
Maintenance errors: any of the above errors introduced during
software maintenance.
36 Shaker El-Sappagh Data structures 10/20/2021
Exercise
• The following function contains a validity error of a sort that’s quite common.
int sum(int a[], int n)
{
// precondition: a is an array subscripted // from 0 to n-1
int i, total(0);
for (i = 0; i <= n; i++)
total += a[i];
return total;
}
• Find the bug and fix it.

37 10/20/2021
6. Testing approaches

• Black-Box testing: test the correspondence between system outputs


and expected ones.

Input set according to Corresponding output


the system’s set according to the
requirements system’s requirements

• Glass-Box testing: test as many possible paths through the code as


feasible.

Input set according to Corresponding output


the system’s set according to the
requirements system’s requirements

38 Shaker El-Sappagh Data structures 10/20/2021


Guidelines for Creating Test Plans
1. Test typical cases
2. Test extreme cases
3. Test invalid inputs

39 10/20/2021
6. Testing approaches (cont’d)

• Unit testing: unit may be function, group of functions (module),


or class. It depends on the used design methodology.

• System testing: testing the components interface, system


functional requirements, beta testing.
• How to select the testing samples? Test typical cases, test
extreme cases, test invalid inputs.

40 Shaker El-Sappagh Data structures 10/20/2021


6. Testing approaches (cont’d)
Unit testing techniques: Unit testing is required because a single
system has many developers.
• Drivers: when you write a unit that is independent of other
units. Driver is a separate program to only test a unit (ex. Max
function).
• Stubs: When a unit contains calls to one or more functions that
are not yet available. A stub is a placeholder for the function
that has not yet been written. It can print “I am here” message
or return a value.
41 Shaker El-Sappagh Data structures 10/20/2021
Function max
int max (int a[], int n)
{
// (precondition) assertion 1: a is an array with subscripts ranging from 0 to
n-1
int max_val(a[0]), i;
for (i = 1; i < n; i++)
// (Loop invariant) assertion 2: (max_val >= a[k] for 0 <= k < i) and
// (max_val = a[j] for some j, 0 <= j < i)
if (max_val < a[i])
max_val = a[i];
// (Postcondition) assertion 3: (max_val >= a[k] for 0 <= k < n) and
// (max_val == a[j] for some j, 0 <= j < n)
// i.e., max_val is equal the value of largest int in array a
return max_val;
}
42 10/20/2021
Code Example 3-1: Test driver for max function (Part 1 of 2)
#include <iostream>
int max(int a[], int n);
int main()
{
int a[100], i;
cout << "Max driver\n";
cout << "Enter each input to max terminated by -9999\n";
cout << "Length of input must be <= 100\n";

43 10/20/2021
Test driver for max function (Part 2 of 2)
for (i = 0; i < 100; i++) {
int val;
cin >> val;
if (val == -9999) // termination sentinel
break;
else
a[i] = val;
}
cout << "\nMax is " << max(a, i) << '\n';
cout << "\n\n";
return 0;
}

44 10/20/2021
Assignment
- Write a function int min(int a[],
int n) that finds the smallest item in array
a.
- Write a function int maxpos(int a[],
int n) that returns the position of the
largest item in the array.

45 Shaker El-Sappagh Data structures 10/20/2021


Thank you

46 Shaker El-Sappagh Data structures 10/20/2021

You might also like