Data Structures that aren't vectors Flashcards
What is a list?
Multiple elements of the same type
dynamic in size
add and remove from beginning and end as opposed to vectors which are primarily the end
Not indexed
Create a list of cars and then print them all out
include <list></list>
list<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};</string>
// Print list elements
for (string car : cars) {
cout «_space;car «_space;“\n”;
}
Can you change the list type from string to something else after you’ve declared it?
No
Show the first or last element of a list
cars.front();
cars.back();
Change the first or last element of a list
cars.front() = “Ford”
cars.back() = “Mazda”
Add elements to the front of a list
Add elements to the back of a list
cars.push_front(“Tesla”)
cars.push_back(“vw”)
Remove elements from the front and then the back of a list
cars.pop_front()
cars.pop_back()
Show how many elements a list has
cars.size()
Find out if a list is empty of not
cars.empty()
Loop through a list
list<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};</string>
for (string car : cars) {
cout «_space;car «_space;“\n”;
}
What is a c++ stack
LIFO order
Last in First out
Everything is added and removed from the top. So if you just added something and remove something, it will be what you just added.
Not indexed, you can only grab what’s on top
You can’t add elements to the stack at time of declaration like you can with vectors
Create a stack of strings
include <stack></stack>
stack<string> cars;</string>
cars.push(“chevy”);
Add elements to a stack
cars.push(“Volvo”)
Access a stack element
cars.top() <- you can only look at the top element
Change a stack element
cars.top() = “Tesla”
You can only modify the top
Remove the last added element from the stack
cars.top()
Show the size of the stack
cars.size()
Check if the stack is empty
cars.empty()
What is a queue
FIFO
Like a queue of people, first person in line is first to pay and leave.
Not indexed
You can only access an element at the front or back
Create a queue
include <queue></queue>
queue<string> cars;</string>
data type can’t be changed
You have to wait until after it’s declared to add data
you have to push these as well
Add an element to the end of your queue
cars.push(“Volvo”)
Show the only queue elements you have access to see
cars.front()
cars.back()
Chant the only two elements you can change in a queue
cars.front() = “Tesla”
cars.back() = “Mazda”
How do you remove an element? What element gets removed?
cars.pop();
the oldest element. So the first one you made
Show the size of your queue
cars.size()
Check if the queue is empty
cars.empty()
What is a deque
Double ended queue.
More flexible queue. Elements can be added and removed from both ends.
It’s also indexed
Create a deque
Print all of its elements
include <deque></deque>
deque<string> cars = {"what", "ever"};</string>
for (string car : cars) {
cout «_space;car «_space;“\n”;
}
data type can’t be changed after declared
Show the first and second element of a deque
cars[0]
cars[1]
cars.at(0)
cars.at(1)
Access the first and last element of a deque
cars.front()
cars.back()
Change a deque element
cars[0] = “Opel”
cars.at(0) = “Opel”
Add elements either to the front or the back of a deque and then remove them
cars.push_front(“Tesla”)
cars.push_back(“VW”)
cars.pop_front()
cars.pop_back()
Show deque size
cars.size()
Check if deque is empty
cars.empty()
Loop through a deque
deque<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};</string>
for (int i = 0; i < cars.size(); i++) {
cout «_space;cars[i] «_space;“\n”;
}
OR
deque<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};</string>
for (string car : cars) {
cout «_space;car «_space;“\n”;
}