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

2. Formal Intro to Data Structures and Algorithms

The document introduces the importance of data structures and algorithms in organizing data for efficient program execution. It discusses abstract data types (ADTs), their composition, specification, and implementation, as well as the efficiency of solutions based on resource constraints. Additionally, it covers the use of arrays and dynamic arrays, emphasizing the need for careful selection of data structures based on specific problem requirements.

Uploaded by

thezsk45
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

2. Formal Intro to Data Structures and Algorithms

The document introduces the importance of data structures and algorithms in organizing data for efficient program execution. It discusses abstract data types (ADTs), their composition, specification, and implementation, as well as the efficiency of solutions based on resource constraints. Additionally, it covers the use of arrays and dynamic arrays, emphasizing the need for careful selection of data structures based on specific problem requirements.

Uploaded by

thezsk45
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Data Structures and

Algorithms
Topic 2: Formal intro to course
Need for Data Structure
• Data structures organize the data => more efficient programs.

• More powerful computers => More complex applications

• More complex applications demand more calculations

2
Organizing Data
• Any organization for a collection of records that can be searched,
processed in any order, or modified.

• The choice of data structure and algorithm can make the difference
between a program running in a few seconds or many days.

3
Abstract Data Types
• Collection
• Operations on data
Data abstraction
Composition, specification &
implementation
• Composition of ADT

• Specification of ADT

• Implementation of ADT
wall of ADT operations
The ADT List
• What is a list?

• ADT List operations


Class Activity
• Questions to ask when designing an ADT

• Questions to ask when implementing an ADT


Efficiency
A solution is said to be efficient if it solves the problem within its
resource constraints.
Space
Time

The cost of a solution is the amount of resources that the solution


consumes.

10
Selecting a Data Structure
Select a data structure as follows:

• Analyze the problem to determine the resource constraints a solution


must meet.
• Determine the basic operations that must be supported. Quantify the
resource constraints for each operation.
• Select the data structure that best meets these requirements.

11
Some Questions
• Are all data inserted into the data structure at the beginning, or are
insertions interspersed with other operations?

• Can data be deleted?

• Are all data processed in some well-defined order, or is random access


allowed?

12
Data Structure Philosophy
 Each data structure has costs and benefits.

 Rarely is one data structure better than another in all situations.

 A data structure requires:


• space for each data item it stores,
• time to perform each basic operation,
• programming effort.

13
Arrays

Elementary data structure that exists as built-in in most programming languages.


 Array declaration: int arr[MAX_LIST];
int main( )  An array is collection of cells of the same type.
 The collection has the name ‘arr’.
{  The cells are numbered with consecutive integers.
int arr[MAX_LIST];  To access a cell, use the array name and an index:
arr[0], arr[1], arr[2], arr[3], arr[4], arr[5]
int j;  Access can be for read/access e.g cout<<arr[3]
for(j=0; j < MAX_LIST; j++)  Access can be for write/mutate/set e.g arr[5]=100
arr[j] = 2*j;
}
14
Dynamic Arrays
 You would like to use an array data structure but you do not know the
size of the array at compile time.
 You find out when the program executes that you need an integer array
of size n=20.
 Allocate an array using the new operator:

int* y = new int[20]; // or int* y = new int[n]


y[0] = 10;
y[1] = 15; // use is the same

‘y’ is a lvalue; it is a pointer that holds the address of 20 consecutive cells in


memory.
15
Dynamic Arrays
 It can be assigned a value. The new operator returns as address that
is stored in y. We can write:

y = &x[0]; // x can appear on the right, y gets the address


y = x; of the first cell of the x array
 We must free the memory we got using the new operator once we
are done with the y array.
delete[ ] y;
 We would not do this to the x array because we did not use new to
create it.

16
An Array-Based ADT List
Separation of Files
• any serious project is going to need splitting up into several files in order to be
manageable.
• Advantages of separation of files
1. speed up compilation
2. Increase organization
3. Facilitate code reuse
4. Share code between projects
5. Split coding responsibilities among programmers
• Separate File List has some basic guidelines
• Put things together which are used together
• Create classes for domain objects (eg files, collections etc)

• Header file (s)e.g ListA.h


• Source file (s) e.g ListA.cpp
• A single Driver class main.cpp
An Array-Based ADT List
• Header files allow you to declare a class or function and
then use it in several different source files.
• header files enable you to separate the declaration from the
implementation.

Save as ListA.h
Each new module/package/class will have its separate header file
An Array-Based ADT List
• ListA.cpp
An Array-Based ADT List
• main.cpp

• include all header files only and use objects as always.


Quiz
• Write a one page summary on ADTs

You might also like