Data Structures that aren't vectors Flashcards

1
Q

What is a list?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Create a list of cars and then print them all out

A

include <list></list>

list<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};</string>

// Print list elements
for (string car : cars) {
cout &laquo_space;car &laquo_space;“\n”;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Can you change the list type from string to something else after you’ve declared it?

A

No

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Show the first or last element of a list

A

cars.front();
cars.back();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Change the first or last element of a list

A

cars.front() = “Ford”
cars.back() = “Mazda”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Add elements to the front of a list
Add elements to the back of a list

A

cars.push_front(“Tesla”)
cars.push_back(“vw”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Remove elements from the front and then the back of a list

A

cars.pop_front()
cars.pop_back()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Show how many elements a list has

A

cars.size()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Find out if a list is empty of not

A

cars.empty()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Loop through a list

A

list<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};</string>

for (string car : cars) {
cout &laquo_space;car &laquo_space;“\n”;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a c++ stack

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Create a stack of strings

A

include <stack></stack>

stack<string> cars;</string>

cars.push(“chevy”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Add elements to a stack

A

cars.push(“Volvo”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Access a stack element

A

cars.top() <- you can only look at the top element

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Change a stack element

A

cars.top() = “Tesla”

You can only modify the top

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Remove the last added element from the stack

A

cars.top()

17
Q

Show the size of the stack

A

cars.size()

18
Q

Check if the stack is empty

A

cars.empty()

19
Q

What is a queue

A

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

20
Q

Create a queue

A

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

21
Q

Add an element to the end of your queue

A

cars.push(“Volvo”)

22
Q

Show the only queue elements you have access to see

A

cars.front()
cars.back()

23
Q

Chant the only two elements you can change in a queue

A

cars.front() = “Tesla”

cars.back() = “Mazda”

24
Q

How do you remove an element? What element gets removed?

A

cars.pop();

the oldest element. So the first one you made

25
Q

Show the size of your queue

A

cars.size()

26
Q

Check if the queue is empty

A

cars.empty()

27
Q

What is a deque

A

Double ended queue.
More flexible queue. Elements can be added and removed from both ends.
It’s also indexed

28
Q

Create a deque

Print all of its elements

A

include <deque></deque>

deque<string> cars = {"what", "ever"};</string>

for (string car : cars) {
cout &laquo_space;car &laquo_space;“\n”;
}

data type can’t be changed after declared

29
Q

Show the first and second element of a deque

A

cars[0]
cars[1]

cars.at(0)
cars.at(1)

30
Q

Access the first and last element of a deque

A

cars.front()
cars.back()

31
Q

Change a deque element

A

cars[0] = “Opel”

cars.at(0) = “Opel”

32
Q

Add elements either to the front or the back of a deque and then remove them

A

cars.push_front(“Tesla”)
cars.push_back(“VW”)

cars.pop_front()
cars.pop_back()

33
Q

Show deque size

A

cars.size()

34
Q

Check if deque is empty

A

cars.empty()

35
Q

Loop through a deque

A

deque<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};</string>

for (int i = 0; i < cars.size(); i++) {
cout &laquo_space;cars[i] &laquo_space;“\n”;
}

OR

deque<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};</string>

for (string car : cars) {
cout &laquo_space;car &laquo_space;“\n”;
}