Lecture 20 Zoom Q&A


Q: around what time will the assignment be opened tomorrow?

A1: lunch time, yum!


Q: so will the regular extension be valid until Saturday ?

A1: Yes


Q: I just submitted my assignment with a linkedlist pqueue implementation, with a linkedlist built from scratch! I hope I get some extra credits for that, I didn't know we were going to build the class today!

A1: live answered


Q: structs can have constructors? i thought structs themselves were like constructors?

A1: The constructor is the function that initializes the fields


Q: Do we wanna implement a size function?

A1: live answered


Q: what does the friend ostream do?

A1: friend declares this function to be a privileged insider, that can assess private fields of this class


Q: when will we be able to schedule meetings with our section leaders to go over the midterm diagnostic?

A1: They’ll send you a notice when they schedule the slots – hopefully in the next day or so


Q: should we add a function that returns the value of the linked list node as we're removing it (like what dequeue() and pop() does?)

A1: You could choose to do something while implementing this, but not needed

A2: We could certainly do that if we were building a class that we wanted to be more funcitional for users but this class is mostly just to get practice with linked lists manipulations


Q: why in the add functions is it adding an int instead of a ListNode?

A1: You create a listnode in the add function that contains that value

A2: If you think of the way data structures are implemented, the public interface should only specify the data type of the data structure (which in this case is an int) and not reveal details of the internal implementastion to the client

A3: Internally we are using ListNodes to store the int values, but the client doesn’t know that, the interface is described in terms of a list of integer values as an abstraction


Q: what is ListNode again?

A1: The little struct that has fields for int and next pointer


Q: is it more labor intensive to be asking for a new node each time where the memory only needs sections of one space reserved? or more intensive to expand and request large blocks of memory?

A1: It depends — take CS107 and you’ll learn all those gory details :-)


Q: why do we need the new keyword here?

A1: live answered

A2: We need to dynamically create a new list nocde in memory to store our data that’s getting added to the linked list.


Q: Is the new node’s next by default the nullptr?

A1: correct


Q: Could we also have a ListNode constructor that sets data and next in one fell swoop?

A1: Yup, totally possible. Would just need to define new constructor that takes 2 params


Q: What does next do again?

A1: next returns the next node in the list


Q: whats the diff between newNode.next and newNode->next?

A1: the arrow dereferences and does the ‘.’ so newNode->next = *(newNode).next

A2: The former applies to a variable of type ListNode (struct), the latter is for a variable of type ListNode * (pointer)


Q: when create a ListNode, its next will be nulptr by default?

A1: yes

A2: Yes, because the constructor set the field to nullptr as a default value


Q: Don’t we still want a last node pointing to nullptr if we addBack?

A1: The constructor for ListNode sets the next field to nullptr as a default value


Q: can this be done recursively?

A1: live answered

A2: Yes, most linked list operations can be done either iteratively or recursively, some are more suited to one or the other


Q: can we say while(curr->next) also?

A1: yes


Q: Why don’t we keep a private ptr to the back of the LinkedList? Wouldn’t that simplify addBack() ?

A1: It would but the goal here is for us to show you what a traversal + insertion looks like :)


Q: If newNode is created using the “new” operator, why does it disappear at the end of addFront or addBack? Is it on the stack or the heap?

A1: The memory created by new lives on the heap and will not “disappear” until we explicitly call delete on it

A2: The newNode pointer itself goes away afetr the funciton goes out of scope, but the memory it points to persists. Since we have rewired the linked list to point to that new memory, we still have the ability to access the coentns of the memory after the function exits.


Q: why not have a back node and then u do not need to traverse

A1: Educational exercise to show what its like to traverse a list + insert :)

A2: Not all linked lists you deal with will be able to have a back ptr, so its good to get practice with all LL operations assuming you only have pointer to the front


Q: Can you explain the arrow next a little bit? Does it mean it get value of a address?

A1: live answered

A2: The -> is a combination of dereference () and field access (.), The expression (ptr).next is more easily written as ptr->next


Q: Should this function return an int?

A1: it could, but we’ve decided not to

A2: Not necesarily, that’s an implementation decision


Q: Why can’t front be dealt like garbage?

A1: live answered

A2: With dynamically allocated memory, you as the programmer are responsible for freeing it when you’re done with it


Q: why do we have to give back memory after every node removal here but we don’t have to do that in the priority queue?

A1: The array memory was allocated/deallocated in big chunks, not per-element

A2: Good question! in hte priority queue, we were dealing with contiguously allocated arrays of memory and so it didnt make sense to give back small pieces of memory right away because we could easily reuse it ourselves later on. With linked lists since each element is dynamically allocated on its own right, we cant just leave old unused nodes hanging around


Q: how is _front = _front-> next dealt with equality? Isn't it a class on the left and the field on the right?

A1: live answered


Q: so delete really just frees memory pointed to by a given pointer

A1: correct


Q: Does delete not need []

A1: No, that is only when you’re freeing an entire arrays worth of memory

A2: it only needs that if you are using a dynamic array


Q: is delete [] used ony when you allocated more than one memory slot?

A1: correct

A2: It’s used when you are freeing memory to a dynamic array


Q: if we use the [] then that would delete an array?

A1: correct

A2: yes, y ou sould use delet for isngle elements and delete[] for arrays


Q: How would we just delete the pointer, temp? Instead of what it is pointing to?

A1: live answered

A2: There’s not really a concept of deleting a pointer – you delete what it points to


Q: if you didn’t create a temp pointer and then delete temp, would the first element just be inaccessible but still exist and the linkedlist would start from the 2nd element?

A1: live answered


Q: why are there no brackets after delete?

A1: delete[] is used to free an entire array of elements, delete is used to free a single dynamically allocated element

A2: you only do that when deleting a dynamically allocated array


Q: can you explain again why we cant just say front = front -> next; ?

A1: live answered

A2: If you update front without first saving a pointer to the old front, you will lose the ability to to reference the old front of the list in order to free it


Q: if i didn’t just want to keep integer data, could i define the ListNode data as void *data instead of int *data?

A1: That would be the old-school C strategy, in C++ you would more likely use a template


Q: what’s the different between delete pte and delete[] ptr?

A1: delete[] is used to free an entire array of elements, delete is used to free a single dynamically allocated element


Q: So, does the C++ know when a pointer is pointing to an array as opposed to when a pointer is pointing to a single element? Or is that our job as the programmer to tell what memory to delete?

A1: this is your job as the programmer

A2: It “knows” in the sense that if you tried to call delete[] on a single element (that is not an array) it will crash. But obviously that’s nto a satisfactory result, so its your job as programmer to call the right one.


Q: will this code be uploaded to the website later?

A1: yes


Q: can you use the same pointer names in dif functions?

A1: yup


Q: What does curr->data do?

A1: Gets the data value of the current node that curr is pointing to


Q: how do you "unpoint," as in delete a connection between 2 nodes

A1: coming right up! (involves overwirting -> next fields)


Q: To check if _front == nullptr, could you also say:

if(this->isEmpty()) { return false; }

If yes, which way is prefered?</i>

A1: yup that also works (you don’t explicitly need the this->). I would say that stylistically this alternate way of doing it is pretty nice


Q: what happens if the list doesn’t contain the value and isn’t empty?

A1: Remove should not change the list in that case


Q: Could you skip the temp declaration and say prev->next = (prev->next)->next?

A1: live answered

A2: You could but you would lost track of the elemnt to delete


Q: For this current implementation, wouldn’t it always make a list of size 1 empty? Don’t we need to check that curr’s value is equal to the value you are looking for?

A1: Yeah there is probably some extra edge case handling there we would want to implement


Q: What does “break” do?

A1: It jumps out of the current loop iteration, starts executing the first statement after the loop


Q: could we just do while (_front != nullptr) {removeFront()}

A1: I think that should do it, yes!


Q: So is it possible the system will change the content of _front between deleting and calling it again if you tried to do it that way?

A1: live answered


Q: can we call the function deleteFront in the while loop?

A1: Yes, that should also work


Q: if we just deleted _front, I get that we can't delete the rest of the nodes, but where does that memory on the heap go? It stays assigned forever? And we lose track?

A1: live answered


Q: I’m still confused about how the remove function would handle the case if the list didn’t contain the value

A1: live answered


Q: When the “this” term is used, why is the arrow used (this->someFunction)? Isn’t “this” an object, so I would think it would be “this.someFunction”?

A1: Good question – this is actually a pointer to the current object, which is why we have ot use the -> operator


Q: Could you go over how the removeAfter function works again?

A1: live answered


Q: can you go over the prev == nullptr part?

A1: live answered


Q: for the removevalue, would we want to put the "removeAfter(prev)" in an else condition? Because if prev == nullptr, wouldn't removeafter try to deference a null pointer?

A1: Yes, exactly, we don’t want to pass in a null previous pointer to the removeafter function


Q: why we need a constructor in the ListNode Struct but not in the Tower example from last class

A1: We didn’t explicitly need a constructor – we just added one for convenience