SlideShare a Scribd company logo
1 of 28
ECE 250 Algorithms and Data Structures
Douglas Wilhelm Harder, M.Math. LEL
Department of Electrical and Computer Engineering
University of Waterloo
Waterloo, Ontario, Canada
ece.uwaterloo.ca
dwharder@alumni.uwaterloo.ca
© 2006-2013 by Douglas Wilhelm Harder. Some rights reserved.
Lists
2
Lists
Outline
We will now look at our first abstract data structure
– Relation: explicit linear ordering
– Operations
– Implementations of an abstract list with:
• Linked lists
• Arrays
– Memory requirements
– Strings as a special case
– The STL vector class
3
Lists
Definition
An Abstract List (or List ADT) is linearly ordered data where the
programmer explicitly defines the ordering
We will look at the most common operations that are usually
– The most obvious implementation is to use either an array or linked list
– These are, however, not always the most optimal
3.1
4
Lists
Operations
Operations at the kth entry of the list include:
Access to the object Erasing an object
Insertion of a new object Replacement of the object
3.1.1
5
Lists
Operations
Given access to the kth object, gain access to either the previous or
next object
Given two abstract lists, we may want to
– Concatenate the two lists
– Determine if one is a sub-list of the other
3.1.1
6
Lists
Locations and run times
The most obvious data structures for implementing an abstract list
are arrays and linked lists
– We will review the run time operations on these structures
We will consider the amount of time required to perform actions
such as finding, inserting new entries before or after, or erasing
entries at
– the first location (the front)
– an arbitrary (kth) location
– the last location (the back or nth)
The run times will be Q(1), O(n) or Q(n)
3.1.2
7
Lists
Linked lists
We will consider these for
– Singly linked lists
– Doubly linked lists
3.1.3
8
Lists
Singly linked list
3.1.3.1
Front/1st node kth node Back/nth node
Find Q(1) O(n)* Q(1)
Insert Before Q(1) O(n)* Q(n)
Insert After Q(1) Q(1)* Q(1)
Replace Q(1) Q(1)* Q(1)
Erase Q(1) O(n)* Q(n)
Next Q(1) Q(1)* n/a
Previous n/a O(n)* Q(n)
* These assume we have already accessed the kth entry—an O(n) operation
9
Lists
Singly linked list
Front/1st node kth node Back/nth node
Find Q(1) O(n)* Q(1)
Insert Before Q(1) Q(1)* Q(1)
Insert After Q(1) Q(1)* Q(1)
Replace Q(1) Q(1)* Q(1)
Erase Q(1) Q(1)* Q(n)
Next Q(1) Q(1)* n/a
Previous n/a O(n)* Q(n)
3.1.3.1
By replacing the value in the node in question, we can speed things up
– useful for interviews
10
Lists
Doubly linked lists
3.1.3.2
Front/1st node kth node Back/nth node
Find Q(1) O(n)* Q(1)
Insert Before Q(1) Q(1)* Q(1)
Insert After Q(1) Q(1)* Q(1)
Replace Q(1) Q(1)* Q(1)
Erase Q(1) Q(1)* Q(1)
Next Q(1) Q(1)* n/a
Previous n/a Q(1)* Q(1)
* These assume we have already accessed the kth entry—an O(n) operation
11
Lists
Doubly linked lists
3.1.3.2
kth node
Insert Before Q(1)
Insert After Q(1)
Replace Q(1)
Erase Q(1)
Next Q(1)
Previous Q(1)
Accessing the kth entry is O(n)
12
Lists
Other operations on linked lists
3.1.3.3
Other operations on linked lists include:
– Allocation and deallocating the memory requires Q(n) time
– Concatenating two linked lists can be done in Q(1)
• This requires a tail pointer
13
Lists
Arrays
3.1.4
We will consider these operations for arrays, including:
– Standard or one-ended arrays
– Two-ended arrays
14
Lists
Standard arrays
3.1.4
We will consider these operations for arrays, including:
– Standard or one-ended arrays
– Two-ended arrays
15
Lists
Run times
Accessing
the kth entry
Insert or erase at the
Front kth entry Back
Singly linked lists
O(n) Q(1) Q(1)*
Q(1) or Q(n)
Doubly linked lists Q(1)
Arrays
Q(1)
Q(n)
O(n) Q(1)
Two-ended arrays Q(1)
* Assume we have a pointer to this node
16
Lists
Data Structures
In general, we will only use these basic data structures if we can
restrict ourselves to operations that execute in Q(1) time, as the only
alternative is O(n) or Q(n)
Interview question: in a singly linked list, can you speed up the two
O(n) operations of
– Inserting before an arbitrary node?
– Erasing any node that is not the last node?
If you can replace the contents of a node, the answer is “yes”
– Replace the contents of the current node with the new entry and insert
after the current node
– Copy the contents of the next node into the current node and erase the
next node
17
Lists
Memory usage versus run times
All of these data structures require Q(n) memory
– Using a two-ended array requires one more member variable, Q(1), in
order to significantly speed up certain operations
– Using a doubly linked list, however, required Q(n) additional memory to
speed up other operations
18
Lists
Memory usage versus run times
As well as determining run times, we are also interested in memory
usage
In general, there is an interesting relationship between memory and
time efficiency
For a data structure/algorithm:
– Improving the run time usually
requires more memory
– Reducing the required memory
usually requires more run time
19
Lists
Memory usage versus run times
Warning: programmers often mistake this to suggest that given any
solution to a problem, any solution which may be faster must require
more memory
This guideline not true in general: there may be different data
structures and/or algorithms which are both faster and require less
memory
– This requires thought and research
20
Lists
The sizeof Operator
In order to determine memory usage, we must know the memory
usage of the various built-in data types and classes
– The sizeof operator in C++ returns the number of bytes occupied by a
data type
– This value is determined at compile time
• It is not a function
21
Lists
The sizeof Operator
#include <iostream>
using namespace std;
int main() {
cout << "bool " << sizeof( bool ) << endl;
cout << "char " << sizeof( char ) << endl;
cout << "short " << sizeof( short ) << endl;
cout << "int " << sizeof( int ) << endl;
cout << "char * " << sizeof( char * ) << endl;
cout << "int * " << sizeof( int * ) << endl;
cout << "double " << sizeof( double ) << endl;
cout << "int[10] " << sizeof( int[10] ) << endl;
return 0;
}
{eceunix:1} ./a.out # output
bool 1
char 1
short 2
int 4
char * 4
int * 4
double 8
int[10] 40
{eceunix:2}
22
Lists
Abstract Strings
A specialization of an Abstract List is an Abstract String:
– The entries are restricted to characters from a finite alphabet
– This includes regular strings “Hello world!”
The restriction using an alphabet emphasizes specific operations
that would seldom be used otherwise
– Substrings, matching substrings, string concatenations
It also allows more efficient implementations
– String searching/matching algorithms
– Regular expressions
23
Lists
Abstract Strings
Strings also include DNA
– The alphabet has 4 characters: A, C, G, and T
– These are the nucleobases:
adenine, cytosine, guanine, and thymine
Bioinformatics today uses many of the
algorithms traditionally restricted to
computer science:
– Dan Gusfield, Algorithms on Strings, Trees and Sequences: Computer
Science and Computational Biology, Cambridge, 1997
http://books.google.ca/books?id=STGlsyqtjYMC
– References:
http://en.wikipedia.org/wiki/DNA
http://en.wikipedia.org/wiki/Bioinformatics
24
Lists
Standard Template Library
In this course, you must understand each data structure and their
associated algorithms
– In industry, you will use other implementations of these structures
The C++ Standard Template Library (STL) has an implementation of
the vector data structure
– Excellent reference:
http://www.cplusplus.com/reference/stl/vector/
25
Lists
Standard Template Library
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v( 10, 0 );
cout << "Is the vector empty? " << v.empty() << endl;
cout << "Size of vector: " << v.size() << endl;
v[0] = 42;
v[9] = 91;
for ( int k = 0; k < 10; ++k ) {
cout << "v[" << k << "] = " << v[k] << endl;
}
return 0;
}
$ g++ vec.cpp
$ ./a.out
Is the vector empty? 0
Size of vector: 10
v[0] = 42
v[1] = 0
v[2] = 0
v[3] = 0
v[4] = 0
v[5] = 0
v[6] = 0
v[7] = 0
v[8] = 0
v[9] = 91
$
26
Lists
Summary
In this topic, we have introduced Abstract Lists
– Explicit linear orderings
– Implementable with arrays or linked lists
• Each has their limitations
• Introduced modifications to reduce run times down to Q(1)
– Discussed memory usage and the sizeof operator
– Looked at the String ADT
– Looked at the vector class in the STL
27
Lists
References
[1] Donald E. Knuth, The Art of Computer Programming, Volume 1:
Fundamental Algorithms, 3rd Ed., Addison Wesley, 1997, §2.2.1, p.238.
[2] Weiss, Data Structures and Algorithm Analysis in C++, 3rd Ed., Addison
Wesley, §3.3.1, p.75.
28
Lists
Usage Notes
• These slides are made publicly available on the web for anyone to
use
• If you choose to use them, or a part thereof, for a course at another
institution, I ask only three things:
– that you inform me that you are using the slides,
– that you acknowledge my work, and
– that you alert me of any mistakes which I made or changes which you
make, and allow me the option of incorporating such changes (with an
acknowledgment) in my set of slides
Sincerely,
Douglas Wilhelm Harder, MMath
dwharder@alumni.uwaterloo.ca

More Related Content

Similar to 3.01.Lists.pptx

Constructing Distributed Doubly Linked Lists without Distributed Locking
Constructing Distributed Doubly Linked Lists without Distributed LockingConstructing Distributed Doubly Linked Lists without Distributed Locking
Constructing Distributed Doubly Linked Lists without Distributed LockingKota Abe
 
A unique sorting algorithm with linear time &amp; space complexity
A unique sorting algorithm with linear time &amp; space complexityA unique sorting algorithm with linear time &amp; space complexity
A unique sorting algorithm with linear time &amp; space complexityeSAT Journals
 
Re-engineering Eclipse MDT/OCL for Xtext
Re-engineering Eclipse MDT/OCL for XtextRe-engineering Eclipse MDT/OCL for Xtext
Re-engineering Eclipse MDT/OCL for XtextEdward Willink
 
cs201-list-stack-queue.ppt
cs201-list-stack-queue.pptcs201-list-stack-queue.ppt
cs201-list-stack-queue.pptRahulYadav738822
 
EdSketch: Execution-Driven Sketching for Java
EdSketch: Execution-Driven Sketching for JavaEdSketch: Execution-Driven Sketching for Java
EdSketch: Execution-Driven Sketching for JavaLisa Hua
 
Algorithms devised for a google interview
Algorithms devised for a google interviewAlgorithms devised for a google interview
Algorithms devised for a google interviewRussell Childs
 
Biomedical Control systems-Block Diagram Reduction Techniques.pptx
Biomedical Control systems-Block Diagram Reduction Techniques.pptxBiomedical Control systems-Block Diagram Reduction Techniques.pptx
Biomedical Control systems-Block Diagram Reduction Techniques.pptxAmnaMuneer9
 
19. algorithms and-complexity
19. algorithms and-complexity19. algorithms and-complexity
19. algorithms and-complexityshowkat27
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxAbhishek Tirkey
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxGauravPandey43518
 
Oracle Join Methods and 12c Adaptive Plans
Oracle Join Methods and 12c Adaptive PlansOracle Join Methods and 12c Adaptive Plans
Oracle Join Methods and 12c Adaptive PlansFranck Pachot
 
Histograms in 12c era
Histograms in 12c eraHistograms in 12c era
Histograms in 12c eraMauro Pagano
 
Как приготовить тестовые данные для Big Data проекта. Пример из практики
Как приготовить тестовые данные для Big Data проекта. Пример из практикиКак приготовить тестовые данные для Big Data проекта. Пример из практики
Как приготовить тестовые данные для Big Data проекта. Пример из практикиSQALab
 
Hailey_Database_Performance_Made_Easy_through_Graphics.pdf
Hailey_Database_Performance_Made_Easy_through_Graphics.pdfHailey_Database_Performance_Made_Easy_through_Graphics.pdf
Hailey_Database_Performance_Made_Easy_through_Graphics.pdfcookie1969
 
lect- 3&4.ppt
lect- 3&4.pptlect- 3&4.ppt
lect- 3&4.pptmrizwan38
 
Insertion sort and shell sort
Insertion sort and shell sortInsertion sort and shell sort
Insertion sort and shell sortPraveen Kumar
 
Profiling and optimization
Profiling and optimizationProfiling and optimization
Profiling and optimizationg3_nittala
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++NUST Stuff
 

Similar to 3.01.Lists.pptx (20)

Constructing Distributed Doubly Linked Lists without Distributed Locking
Constructing Distributed Doubly Linked Lists without Distributed LockingConstructing Distributed Doubly Linked Lists without Distributed Locking
Constructing Distributed Doubly Linked Lists without Distributed Locking
 
A unique sorting algorithm with linear time &amp; space complexity
A unique sorting algorithm with linear time &amp; space complexityA unique sorting algorithm with linear time &amp; space complexity
A unique sorting algorithm with linear time &amp; space complexity
 
Re-engineering Eclipse MDT/OCL for Xtext
Re-engineering Eclipse MDT/OCL for XtextRe-engineering Eclipse MDT/OCL for Xtext
Re-engineering Eclipse MDT/OCL for Xtext
 
Normalization.ppt
Normalization.pptNormalization.ppt
Normalization.ppt
 
cs201-list-stack-queue.ppt
cs201-list-stack-queue.pptcs201-list-stack-queue.ppt
cs201-list-stack-queue.ppt
 
EdSketch: Execution-Driven Sketching for Java
EdSketch: Execution-Driven Sketching for JavaEdSketch: Execution-Driven Sketching for Java
EdSketch: Execution-Driven Sketching for Java
 
Algorithms devised for a google interview
Algorithms devised for a google interviewAlgorithms devised for a google interview
Algorithms devised for a google interview
 
Biomedical Control systems-Block Diagram Reduction Techniques.pptx
Biomedical Control systems-Block Diagram Reduction Techniques.pptxBiomedical Control systems-Block Diagram Reduction Techniques.pptx
Biomedical Control systems-Block Diagram Reduction Techniques.pptx
 
Data structure-question-bank
Data structure-question-bankData structure-question-bank
Data structure-question-bank
 
19. algorithms and-complexity
19. algorithms and-complexity19. algorithms and-complexity
19. algorithms and-complexity
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
 
Oracle Join Methods and 12c Adaptive Plans
Oracle Join Methods and 12c Adaptive PlansOracle Join Methods and 12c Adaptive Plans
Oracle Join Methods and 12c Adaptive Plans
 
Histograms in 12c era
Histograms in 12c eraHistograms in 12c era
Histograms in 12c era
 
Как приготовить тестовые данные для Big Data проекта. Пример из практики
Как приготовить тестовые данные для Big Data проекта. Пример из практикиКак приготовить тестовые данные для Big Data проекта. Пример из практики
Как приготовить тестовые данные для Big Data проекта. Пример из практики
 
Hailey_Database_Performance_Made_Easy_through_Graphics.pdf
Hailey_Database_Performance_Made_Easy_through_Graphics.pdfHailey_Database_Performance_Made_Easy_through_Graphics.pdf
Hailey_Database_Performance_Made_Easy_through_Graphics.pdf
 
lect- 3&4.ppt
lect- 3&4.pptlect- 3&4.ppt
lect- 3&4.ppt
 
Insertion sort and shell sort
Insertion sort and shell sortInsertion sort and shell sort
Insertion sort and shell sort
 
Profiling and optimization
Profiling and optimizationProfiling and optimization
Profiling and optimization
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++
 

Recently uploaded

Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...amitlee9823
 
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...amitlee9823
 
Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdfAccredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdfadriantubila
 
Smarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptxSmarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptxolyaivanovalion
 
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...SUHANI PANDEY
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxolyaivanovalion
 
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...amitlee9823
 
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...amitlee9823
 
BabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxBabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxolyaivanovalion
 
Carero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxCarero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxolyaivanovalion
 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxolyaivanovalion
 
ELKO dropshipping via API with DroFx.pptx
ELKO dropshipping via API with DroFx.pptxELKO dropshipping via API with DroFx.pptx
ELKO dropshipping via API with DroFx.pptxolyaivanovalion
 
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% SecureCall me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% SecurePooja Nehwal
 
Capstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics ProgramCapstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics ProgramMoniSankarHazra
 
BigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxBigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxolyaivanovalion
 
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...amitlee9823
 

Recently uploaded (20)

Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
 
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
 
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
 
Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdfAccredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
 
Smarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptxSmarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptx
 
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptx
 
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
 
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
 
BabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxBabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptx
 
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get CytotecAbortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
 
Carero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxCarero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptx
 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptx
 
ELKO dropshipping via API with DroFx.pptx
ELKO dropshipping via API with DroFx.pptxELKO dropshipping via API with DroFx.pptx
ELKO dropshipping via API with DroFx.pptx
 
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% SecureCall me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
 
Capstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics ProgramCapstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics Program
 
BigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxBigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptx
 
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts ServiceCall Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
 
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
 

3.01.Lists.pptx

  • 1. ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca dwharder@alumni.uwaterloo.ca © 2006-2013 by Douglas Wilhelm Harder. Some rights reserved. Lists
  • 2. 2 Lists Outline We will now look at our first abstract data structure – Relation: explicit linear ordering – Operations – Implementations of an abstract list with: • Linked lists • Arrays – Memory requirements – Strings as a special case – The STL vector class
  • 3. 3 Lists Definition An Abstract List (or List ADT) is linearly ordered data where the programmer explicitly defines the ordering We will look at the most common operations that are usually – The most obvious implementation is to use either an array or linked list – These are, however, not always the most optimal 3.1
  • 4. 4 Lists Operations Operations at the kth entry of the list include: Access to the object Erasing an object Insertion of a new object Replacement of the object 3.1.1
  • 5. 5 Lists Operations Given access to the kth object, gain access to either the previous or next object Given two abstract lists, we may want to – Concatenate the two lists – Determine if one is a sub-list of the other 3.1.1
  • 6. 6 Lists Locations and run times The most obvious data structures for implementing an abstract list are arrays and linked lists – We will review the run time operations on these structures We will consider the amount of time required to perform actions such as finding, inserting new entries before or after, or erasing entries at – the first location (the front) – an arbitrary (kth) location – the last location (the back or nth) The run times will be Q(1), O(n) or Q(n) 3.1.2
  • 7. 7 Lists Linked lists We will consider these for – Singly linked lists – Doubly linked lists 3.1.3
  • 8. 8 Lists Singly linked list 3.1.3.1 Front/1st node kth node Back/nth node Find Q(1) O(n)* Q(1) Insert Before Q(1) O(n)* Q(n) Insert After Q(1) Q(1)* Q(1) Replace Q(1) Q(1)* Q(1) Erase Q(1) O(n)* Q(n) Next Q(1) Q(1)* n/a Previous n/a O(n)* Q(n) * These assume we have already accessed the kth entry—an O(n) operation
  • 9. 9 Lists Singly linked list Front/1st node kth node Back/nth node Find Q(1) O(n)* Q(1) Insert Before Q(1) Q(1)* Q(1) Insert After Q(1) Q(1)* Q(1) Replace Q(1) Q(1)* Q(1) Erase Q(1) Q(1)* Q(n) Next Q(1) Q(1)* n/a Previous n/a O(n)* Q(n) 3.1.3.1 By replacing the value in the node in question, we can speed things up – useful for interviews
  • 10. 10 Lists Doubly linked lists 3.1.3.2 Front/1st node kth node Back/nth node Find Q(1) O(n)* Q(1) Insert Before Q(1) Q(1)* Q(1) Insert After Q(1) Q(1)* Q(1) Replace Q(1) Q(1)* Q(1) Erase Q(1) Q(1)* Q(1) Next Q(1) Q(1)* n/a Previous n/a Q(1)* Q(1) * These assume we have already accessed the kth entry—an O(n) operation
  • 11. 11 Lists Doubly linked lists 3.1.3.2 kth node Insert Before Q(1) Insert After Q(1) Replace Q(1) Erase Q(1) Next Q(1) Previous Q(1) Accessing the kth entry is O(n)
  • 12. 12 Lists Other operations on linked lists 3.1.3.3 Other operations on linked lists include: – Allocation and deallocating the memory requires Q(n) time – Concatenating two linked lists can be done in Q(1) • This requires a tail pointer
  • 13. 13 Lists Arrays 3.1.4 We will consider these operations for arrays, including: – Standard or one-ended arrays – Two-ended arrays
  • 14. 14 Lists Standard arrays 3.1.4 We will consider these operations for arrays, including: – Standard or one-ended arrays – Two-ended arrays
  • 15. 15 Lists Run times Accessing the kth entry Insert or erase at the Front kth entry Back Singly linked lists O(n) Q(1) Q(1)* Q(1) or Q(n) Doubly linked lists Q(1) Arrays Q(1) Q(n) O(n) Q(1) Two-ended arrays Q(1) * Assume we have a pointer to this node
  • 16. 16 Lists Data Structures In general, we will only use these basic data structures if we can restrict ourselves to operations that execute in Q(1) time, as the only alternative is O(n) or Q(n) Interview question: in a singly linked list, can you speed up the two O(n) operations of – Inserting before an arbitrary node? – Erasing any node that is not the last node? If you can replace the contents of a node, the answer is “yes” – Replace the contents of the current node with the new entry and insert after the current node – Copy the contents of the next node into the current node and erase the next node
  • 17. 17 Lists Memory usage versus run times All of these data structures require Q(n) memory – Using a two-ended array requires one more member variable, Q(1), in order to significantly speed up certain operations – Using a doubly linked list, however, required Q(n) additional memory to speed up other operations
  • 18. 18 Lists Memory usage versus run times As well as determining run times, we are also interested in memory usage In general, there is an interesting relationship between memory and time efficiency For a data structure/algorithm: – Improving the run time usually requires more memory – Reducing the required memory usually requires more run time
  • 19. 19 Lists Memory usage versus run times Warning: programmers often mistake this to suggest that given any solution to a problem, any solution which may be faster must require more memory This guideline not true in general: there may be different data structures and/or algorithms which are both faster and require less memory – This requires thought and research
  • 20. 20 Lists The sizeof Operator In order to determine memory usage, we must know the memory usage of the various built-in data types and classes – The sizeof operator in C++ returns the number of bytes occupied by a data type – This value is determined at compile time • It is not a function
  • 21. 21 Lists The sizeof Operator #include <iostream> using namespace std; int main() { cout << "bool " << sizeof( bool ) << endl; cout << "char " << sizeof( char ) << endl; cout << "short " << sizeof( short ) << endl; cout << "int " << sizeof( int ) << endl; cout << "char * " << sizeof( char * ) << endl; cout << "int * " << sizeof( int * ) << endl; cout << "double " << sizeof( double ) << endl; cout << "int[10] " << sizeof( int[10] ) << endl; return 0; } {eceunix:1} ./a.out # output bool 1 char 1 short 2 int 4 char * 4 int * 4 double 8 int[10] 40 {eceunix:2}
  • 22. 22 Lists Abstract Strings A specialization of an Abstract List is an Abstract String: – The entries are restricted to characters from a finite alphabet – This includes regular strings “Hello world!” The restriction using an alphabet emphasizes specific operations that would seldom be used otherwise – Substrings, matching substrings, string concatenations It also allows more efficient implementations – String searching/matching algorithms – Regular expressions
  • 23. 23 Lists Abstract Strings Strings also include DNA – The alphabet has 4 characters: A, C, G, and T – These are the nucleobases: adenine, cytosine, guanine, and thymine Bioinformatics today uses many of the algorithms traditionally restricted to computer science: – Dan Gusfield, Algorithms on Strings, Trees and Sequences: Computer Science and Computational Biology, Cambridge, 1997 http://books.google.ca/books?id=STGlsyqtjYMC – References: http://en.wikipedia.org/wiki/DNA http://en.wikipedia.org/wiki/Bioinformatics
  • 24. 24 Lists Standard Template Library In this course, you must understand each data structure and their associated algorithms – In industry, you will use other implementations of these structures The C++ Standard Template Library (STL) has an implementation of the vector data structure – Excellent reference: http://www.cplusplus.com/reference/stl/vector/
  • 25. 25 Lists Standard Template Library #include <iostream> #include <vector> using namespace std; int main() { vector<int> v( 10, 0 ); cout << "Is the vector empty? " << v.empty() << endl; cout << "Size of vector: " << v.size() << endl; v[0] = 42; v[9] = 91; for ( int k = 0; k < 10; ++k ) { cout << "v[" << k << "] = " << v[k] << endl; } return 0; } $ g++ vec.cpp $ ./a.out Is the vector empty? 0 Size of vector: 10 v[0] = 42 v[1] = 0 v[2] = 0 v[3] = 0 v[4] = 0 v[5] = 0 v[6] = 0 v[7] = 0 v[8] = 0 v[9] = 91 $
  • 26. 26 Lists Summary In this topic, we have introduced Abstract Lists – Explicit linear orderings – Implementable with arrays or linked lists • Each has their limitations • Introduced modifications to reduce run times down to Q(1) – Discussed memory usage and the sizeof operator – Looked at the String ADT – Looked at the vector class in the STL
  • 27. 27 Lists References [1] Donald E. Knuth, The Art of Computer Programming, Volume 1: Fundamental Algorithms, 3rd Ed., Addison Wesley, 1997, §2.2.1, p.238. [2] Weiss, Data Structures and Algorithm Analysis in C++, 3rd Ed., Addison Wesley, §3.3.1, p.75.
  • 28. 28 Lists Usage Notes • These slides are made publicly available on the web for anyone to use • If you choose to use them, or a part thereof, for a course at another institution, I ask only three things: – that you inform me that you are using the slides, – that you acknowledge my work, and – that you alert me of any mistakes which I made or changes which you make, and allow me the option of incorporating such changes (with an acknowledgment) in my set of slides Sincerely, Douglas Wilhelm Harder, MMath dwharder@alumni.uwaterloo.ca