CS Week 7 - Linked Lists Flashcards
pointer
a variable that contains a memory address
Dynamically allocated array
an array whose size can change during runtime
when a vector is created
–The class internally dynamically allocates an array with an initial size
–If the number of elements added to the vector exceeds the capacity, class with dynamically allocate a new array with an increased size
–Contents of the array are copied into the new larger array
–Each time the internal array is dynamically allocated, array’s location in memory will change
–Vector class uses a pointer to store the memory location of the internal array
Vector insert/erase performance problem
For a vector with thousands of elements, a single call to insert() or erase() can require thousands of instructions and cause the program to run very slowly
Linked list
consists of items that contain both data and a pointer - a link- to the next list item
this pointer
when a class member function is called on an object, a pointer to the object is automatically passed to the member function as an implicit parameter
- Enables access to an object’s data members within the object’s class member functions
-A data member can be accessed using this and the member access operator for a pointer ->
-This pointer clearly indicated that an objects data member is being accessed, which is needed if a member function’s parameter has the same variable name as the data member
this-> dataMember
Singly-linked list
a data structure for implementing a list ADT, where each node has data and a pointer to the next node
head
singly-linked list’s first node
tail
singly-linked list’s last node
positional list
a list where elements contain pointers to the next and/or previous elements in the list
null
a special value indicating a pointer points to nothing
Append
inserts the new node after the list’s tail node
Appending to empty list: if list’s head pointer is null(empty), algorithm points the list’s head and tail pointers to the new node
Append to non-empty list: if the list’s head pointer is not null (not empty), algorithm points the tail node’s next pointer and the list’s tail pointer to the new node
Prepend
inserts the new node before the list’s head node
Prepend to empty list-if the list’s head pointer is null (empty), algorithm points the list’s head and tail pointers to the new node
Prepend to non-empty list- if the list’s head pointer is not null (not empty), algorithm points the new node’s next pointer to the head node, and then points the list’s head pointer to the new node
InsertAfter
inserts the new node after a provided existing list node
curNode is a pointer to an existing list node, but can be null when inserting into an empty list
Insert as list’s first node- if the list’s head pointer is null, algorithm points the list’s head and tail pointers to the new node
Insert after the list’s tail node- if the list’s head pointer is not null and curNode points to the list’s tail node, algorithm points the tail node’s next pointer and the list’s tail pointer to the new node
Insert in middle of list- if the list’s head pointer is not null and curNode does not point to the list’s tail node, algorithm points the new node’s next pointer to curNode’s next node, and then points curNode’s next pointer to the new node
RemoveAfter
removes the node after the specified list node
Exiting node must be specified because each node in a singly-linked list only maintains a pointer to the next node
curNode parameter - existing node
Remove list’s head node- (special case) if curNode is null, algorithm points sucNode to the head node’s next node, and points the list’s head pointer to sucNode. If sucNode is null, the only list node was removed, so the list’s tail pointer is pointed to null (indicating list is now empty)
Remove node after curNode- if curNode’s next pointer is not null (a node after curNode exists), algorithm points sucNode to the node after curNode’s next node. Then curNode’s next pointer is pointed to sucNode. If sucNode is null, the list’s tail node was removed, so algorithm points the list’s tail pointer to curNode (the new tail node)