Lists, Stacks, Queues, Trees, Hash Tables Basic Data Structures.
1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types,...
-
Upload
kylee-turnham -
Category
Documents
-
view
236 -
download
3
Transcript of 1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types,...
![Page 1: 1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.](https://reader035.fdocuments.net/reader035/viewer/2022081514/5518d03f55034638098b501c/html5/thumbnails/1.jpg)
1
Linked lists
Sections 3.2, 3.3, 3.5
Chapter 3 Lists, Stacks, and QueuesAbstract Data Types, Vectors
![Page 2: 1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.](https://reader035.fdocuments.net/reader035/viewer/2022081514/5518d03f55034638098b501c/html5/thumbnails/2.jpg)
2
List Wish List• Efficiently insert an element • Efficiently remove an element
– What are the time complexities for inserting, removing and searching for elements in a vector or sorted vector?
• Remove all items • Assignment operator • Comparison operators • Constructors/destructors
• Generic class• Convenient way to iterate through the list
![Page 3: 1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.](https://reader035.fdocuments.net/reader035/viewer/2022081514/5518d03f55034638098b501c/html5/thumbnails/3.jpg)
3
Singly Linked List
ListElement
next next next next
prev
front back?
null
null
Time complexities for push_front, push_back, pop_front, pop_back?
![Page 4: 1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.](https://reader035.fdocuments.net/reader035/viewer/2022081514/5518d03f55034638098b501c/html5/thumbnails/4.jpg)
4
Abstract view of List and Iterator
ListElement
next
prev
next next next
prev prev prev
I1front back
null
null
Iterator
• Doubly-linked list
![Page 5: 1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.](https://reader035.fdocuments.net/reader035/viewer/2022081514/5518d03f55034638098b501c/html5/thumbnails/5.jpg)
5
List Public Interface
• Constructors and big-three– Copy constructor, destructor, assignment operator
List();
List(const List &rhs);
~List();
const List & operator=(const List &rhs);
• Read-only accessor functions int size() const;
bool empty() const;
![Page 6: 1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.](https://reader035.fdocuments.net/reader035/viewer/2022081514/5518d03f55034638098b501c/html5/thumbnails/6.jpg)
6
List Public Interface (cont’d)
• Accessing values on the listObject & front();
Object & back();
and their constant versions.
• Locating places on the list using iterators– Both regular and constant versions
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
![Page 7: 1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.](https://reader035.fdocuments.net/reader035/viewer/2022081514/5518d03f55034638098b501c/html5/thumbnails/7.jpg)
7
List Public Interface (cont’d)
• List manipulation functions
int push_front(const Object & x);
int push_back(const Object & x);
int pop_front();
int pop_back();
iterator insert(iterator & itr, const Object & x);
iterator erase( iterator itr);
iterator erase( iterator start, iterator end );
void clear();
![Page 8: 1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.](https://reader035.fdocuments.net/reader035/viewer/2022081514/5518d03f55034638098b501c/html5/thumbnails/8.jpg)
8
List Complexity Requirements
• O(1) Runtime complexity
– Default constructor
– push_front(t), push_back(t), insert(I, t)– pop_front(), pop_back(), erase(I)– begin(), end();– front(), back();– empty();
![Page 9: 1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.](https://reader035.fdocuments.net/reader035/viewer/2022081514/5518d03f55034638098b501c/html5/thumbnails/9.jpg)
9
List Complexity Requirements (2)
• O(N) Runtime complexity
– Copy Constructor
– Destructor
– clear()
– erase(SI,EI)
![Page 10: 1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.](https://reader035.fdocuments.net/reader035/viewer/2022081514/5518d03f55034638098b501c/html5/thumbnails/10.jpg)
10
List Iterator Public Interface
• Read-only operatorsint operator== (const iterator & rhs) const;int operator!= (const iterator & rhs) const;Object & operator* ( ) const; // return a reference to current value
• Write operatorsiterator & operator++ ( ); // prefixiterator operator++ ( int ); // postfixiterator& operator-- ( ); // prefixiterator operator-- ( int ); // postfix
• O(1) requirement for space and time
![Page 11: 1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.](https://reader035.fdocuments.net/reader035/viewer/2022081514/5518d03f55034638098b501c/html5/thumbnails/11.jpg)
11
Using List
List<String> Cities;Cities.push_front(“Tallahassee”);
– “Tallahassee”Cities.push_back(“Gainesville”);
– “Tallahassee”, “Gainesville”Cities.push_front(“Jacksonville”);
– “Jacksonville”, “Tallahassee”, “Gainesville”Cities.push_back(“Miami”);
– “Jacksonville”, “Tallahassee”, “Gainesville”, “Miami”
List<String>::iterator I;
for (I = Cities.begin(); I != Cities.end(); ++I) {// print list with <<
}
![Page 12: 1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.](https://reader035.fdocuments.net/reader035/viewer/2022081514/5518d03f55034638098b501c/html5/thumbnails/12.jpg)
12
List Insertion
• Insert “Orlando” before “Miami”
// sequential searchfor (I = Cities.begin(); I != Cities.end(); ++I) {
if (“Miami” == *I) {break;
}}
// insert the new stringCities.insert(I, “Orlando”);
– “Jacksonville”, “Tallahassee”, “Gainesville”, “Orlando”, “Miami”
// what happens if “Miami” is not on the list?
![Page 13: 1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.](https://reader035.fdocuments.net/reader035/viewer/2022081514/5518d03f55034638098b501c/html5/thumbnails/13.jpg)
13
Remove all copies of an item from List
• Remove all elements with value “Orlando”
List<String>::iterator I = Cities.begin();
while( I != Cities.end()) {if (“Orlando” == *I) {
I = Cities.erase(I);} else {
I++;}
}
![Page 14: 1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.](https://reader035.fdocuments.net/reader035/viewer/2022081514/5518d03f55034638098b501c/html5/thumbnails/14.jpg)
14
List and List Iterator
– Conceptual relationship
Iterator I1
begin current end
List: A, B, C, D, E, F
begin current end
Iterator I2
![Page 15: 1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.](https://reader035.fdocuments.net/reader035/viewer/2022081514/5518d03f55034638098b501c/html5/thumbnails/15.jpg)
15
List Implementation
A Doubly Linked List With Header and Tail Nodes as Markers
An Empty List
![Page 16: 1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.](https://reader035.fdocuments.net/reader035/viewer/2022081514/5518d03f55034638098b501c/html5/thumbnails/16.jpg)
16
Nodes in a list
• Node– Data Value– Pointers to the previous and next element
• Defined within the List class, with limited scope
data
prev
next
data
prev
next
data
prev
next
No need for contiguous memory allocation
![Page 17: 1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.](https://reader035.fdocuments.net/reader035/viewer/2022081514/5518d03f55034638098b501c/html5/thumbnails/17.jpg)
17
Outline of List Class (Part 1)
![Page 18: 1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.](https://reader035.fdocuments.net/reader035/viewer/2022081514/5518d03f55034638098b501c/html5/thumbnails/18.jpg)
18
Outline of List Class (Part 2)
![Page 19: 1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.](https://reader035.fdocuments.net/reader035/viewer/2022081514/5518d03f55034638098b501c/html5/thumbnails/19.jpg)
19
Outline of List Class (Part 3)
![Page 20: 1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.](https://reader035.fdocuments.net/reader035/viewer/2022081514/5518d03f55034638098b501c/html5/thumbnails/20.jpg)
20
Outline of List Class (Part 4)
![Page 21: 1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.](https://reader035.fdocuments.net/reader035/viewer/2022081514/5518d03f55034638098b501c/html5/thumbnails/21.jpg)
21
const_iterator for List
Prefix
Postfix
![Page 22: 1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.](https://reader035.fdocuments.net/reader035/viewer/2022081514/5518d03f55034638098b501c/html5/thumbnails/22.jpg)
22
const_iterator class for List (contd.)
![Page 23: 1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.](https://reader035.fdocuments.net/reader035/viewer/2022081514/5518d03f55034638098b501c/html5/thumbnails/23.jpg)
23
List Initialization Routines
![Page 24: 1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.](https://reader035.fdocuments.net/reader035/viewer/2022081514/5518d03f55034638098b501c/html5/thumbnails/24.jpg)
24
List Insert Routine
![Page 25: 1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.](https://reader035.fdocuments.net/reader035/viewer/2022081514/5518d03f55034638098b501c/html5/thumbnails/25.jpg)
25
List Erase Routine