Data Structures and Algorithms Chapter 3
the head and tail pointers are null
What are the list's head and tail pointers when it is created?
List
a common ADT for holding ordered data with operations like append, remove, search, and print. Users do not need internal implementation of the list ADT.
list traversal
an algorithm that visits all nodes once and preforms an operation on each
Append to a non-empty singly-linked list
if the list's head pointer is not null, point the current list tail node's next pointer to the new node, then the list's tail pointer to the new making the new node the new list's tail node
Prepend to a non-empty singly-linked list
if the list's head pointer is not null, point the new node's next pointer to the current head node and the list's head pointer to the new node making it the list's new head node
Append or Prepend to empty singly-linked list
if the list's head pointer is null, point the current list's head and tail pointers to the new node
dummy node
in linked list implementation, a node with an unused data member that will always reside at the head of the node. can be called header node
List insertAfter Operation
inserts an element after a specified value
Yes insertion sort for lists is similar to insertion sort for arrays but the list must be doubly-linked because both the next and previous pointers are used
is the list insertion sort like an insertion sort for an array
the append, prepend, insertAfter, and remove operations will become simpler and easier to code
what operations for a linked list are effected when a dummy node is present
shell sort, quicksort, and heap sort
what sorting algorithms cannot be easily adapted to linked lists
only a doubly-linked list can be reverse traversed
which kind of list can be reverse traversed
newNode->prev = list->tail
which line of code points the new node's previous pointer to the tail node
curNode->next = newNode
which line of code will point the curNode's next pointer to the new node so that it will be the node after the curNode?
newNode->next = curNode->next
which line of code will point the new node to the curNode's next node?
list->head->prev = newNode
which line of code will set the head node's previous pointer to the new node
challenge - indexed access is required to find the child nodes in constant time, indexed locations are not allowed in linked lists
why does heap sort not easily adapt to linked lists
challenge - partition requires backward traversal through the right portion of the array which singly-linked lists do not support
why does quicksort not easily adapt to linked lists
challenge - jumping the gap between element cannot be done because the elements between two elements must be traversed
why does shell sort not easily adapt to linked lists
ListFindInsertionPosition
and algorithm that searches for the insertion position and returns it's node so that the desired node can be inserted after it. will start with two pointers one set to null and the other to the head of the list and use a loop to compare the one starting at the head to the data passed in. will return the pointer that was originally set to null
Head Node
Head of List, front, top
insertion sort can only insert a node after an existing one, so it will use the ListFindInsertionPosition to get the position that the given node should be inserted after in a singly-linked list. if the position returned is null then the node will be inserted at the head.
How does an insertion sort work with singly-linked lists
Append(list, x)
How is the list's append operation written?
GetLength(list)
How is the list's getLength operation written?
InsertAfter(list, w, x)
How is the list's insertAfter operation written?
IsEmpty(list)
How is the list's isEmpty operation written?
Prepend(list, x)
How is the list's prepend operation written?
Print(list)
How is the list's print operation written?
PrintReverse(list)
How is the list's printReverse operation written?
Remove(list, x)
How is the list's remove operation written?
Search(list, x)
How is the list's search operation written?
Sort(list)
How is the list's sort operation written?
for a doubly-linked list, inserts the new node after a specified existing list node. curNode points to the specified node and sucNode points to curNode's next node
InsertAfter operation for doubly-linked list
for singly-linked lists, inserts a new node after a specified, existing node. curNode variable is used as a pointer to the specified existing node
InsertAfter operation of singly-linked list
List Append Operation
Inserts an element at the end of the list
for a singly-linked list, inserts the new node at the head of the list so that the new node becomes the list's new head node
Prepend operation for singly-linked list
for a doubly-linked list, inserts the new node before the list's head node and points the list's head pointer to the new node
Prepend operation of doubly-linked list
for a doubly-linked list, removes a specified, existing node. curNode is the pointer to the node being removed, sucNode is the curNode's next node, and predNode is the curNode's previous node
Remove operation for a doubly-linked list
for a singly-linked list, removes the node after a specified existing node. curNode points to the specified node and sucNode is the node after the node that the curNode's next pointer is pointing to
RemoveAfter operation of singly-linked list
the pointer is not pointing to any node
What does a null value for a pointer mean?
to add after, generally at the end of something like a list
What does append mean?
to add before, generally at the beginning of something like a list
What does prepend mean?
That the list's data and implementation are not known
What is the point of an ADT?
list->head = newNode
What line of code will point the list's head pointer to a new node?
newNode->next = list->head
What line of code will point the new node's next pointer to the current head node?
Singly-Linked List
a data structure for implementing a list ADT where each element(node) has a data value and a pointer to the next node.
doubly-linked list
a data structure for implementing a list ADT, where each node contains data(element), a pointer to the next node, and a pointer to the previous node
Positional List
a list where elements contain pointers to the next and/or previous element in the list.
if the successor node exists, if the predecessor node exist, if the list head is being removed, and if the list tail is being removed, for doubly-linked list
what are the four pointers checked when a doubly-linked list removes a node
insertion sort and merge sort
what are the most efficient and easily adapted sorting algorithms for linked list
list->tail->next = newNode
what line of code will point the list's tail node's next pointer to a new node
list->tail = newNode
what line of code will point the list's tail pointer to a new node?
sucNode = curNode->next->next
what line of code will sucNode be if a node from the middle of the singly-linked list is being removed
sucNode = list->head->next
what line of code will sucNode be if the singly-linked list's head node is being removed
curNode = curNode->next
what line will set curNode to it's next node
List Prepend Operation
Inserts an element at the beginning of the list
Tail Node
Tail of List, end, bottom
List Sort Operation
sorts the list's elements into ascending order
List Print Operation
prints the list's elements in order
to traverse a list set the curNode as the pointer to the list's had node, test curNode to determine if it is null or not, if curNode is null the entire list has been traversed, if curNode is not null then the desired operation is executed and the curNode is reset to point the next node. repeat the last three steps as many times as needed
what are the steps of a list traversal
the steps are: set the curNode to point to the list's tail node, check if the curNode is null or not, if curNode is null then the list has been gone through, if the curNode is not null then the desired operation is preformed, then the curNode is reset so that it points to it's previous node. the last three steps will repeat until the list has been gone through
what are the steps of a revers traversal
the doubly-linked list will include a previous pointer to null in the dummy node
what does a doubly-linked list with a dummy node have that a singly-linked list with a dummy node does not
when provided with a key, will return the first node whose data matches that key or null if the key was not found in the list
what does a search algorithm do
the singly-linked list's head node is removed
what happens if the curNode is null when using the singly-linked list removeAfter operaiton
the doubly-linked list's head pointer is reset to the sucNode
what happens if the head node is to be removed from a doubly-linked list
the predNode's next pointer is set to the sucNode, doubly-linked list
what happens if the predecessor node exists when removing a node from a doubly-linked list
the sucNode's previous pointer is set to the predNode, doubly-linked list
what happens if the successor node exists when removing a node from a doubly-linked list
the doubly-linked list's tail pointer is reset to the predNode
what happens if the tail node is to be removed from a doubly-linked list
the linked list will essentially preform a traversal whose operation will attempt to match they key to a node and return the curNode that points to the matching node or null if the key is not found
what is a search of a linked list
the dummy nodes purpose is so that a list will always have a node for it's head and tail pointers to point at. this allows any special cases for when the list is empty in the list operations to be omitted.
what is the purpose of a dummy node
the average- and wort-case runtime for insertion sort is O(N squared) while the best-case runtime is O(N)
what is the runtime for insertion sort
curNode->data
what line of code is used to get node data
for a singly-linked list, inserts the new node after the current tail node of the list so that the now node is now the list's new tail node
Append Operation of a singly-linked list
for a doubly-linked list, inserts a new node after the current tail node to become the list's new tail node
Append Operation of double-linked list
reverse traversal
an algorithm that traverses a list, starting with the tail node instead of the head node, and preforms an operation on each node. only supported with doubly-linked lists
Node
from the it's own class, contains the memory location where the data value of a list is stored and contains pointers(next/previous) to the node in front(previous) and/or the node in back(next)
for singly-linked lists, the dummy node will always be at the head of the list with the list's head pointer set to the dummy node at all times and when the list is empty the list's tail pointer will also point to the dummy node. this will prevent exception being thrown because the list is empty
how are dummy nodes implemented with singly-linked lists
adaptation - operates similar on doubly-linked list, requires searching from the list head for insertion position for singly-linked lists
how does insertion sort adapt to linked lists
adaption - finding the middle of the list requires searching linearly from the list head and can merge lists without additional storage
how does merge sort adapt to linked lists
arrays allow for access to indexed locations while LinkedLists do allow indexes to be used for access
how is data access different for arrays and linked lists
insertion sort's inner loop will execute N/2 times
how many times will the inner loop in the insertion sort execute
insertion sort's outer loop will execute N-1 times
how many times will the outer loop in the insertion sort execute
insertion after the tail node in a singly-linked list
if a singly-linked list's head pointer is not null and the curNode is pointing to the tail node, the point the list's tail node's next pointer to the new node, and then point the list's tail pointer to the new node, making it the new tail node
insertion into empty singly-linked list
if a singly-linked list's head pointer is null, then the new node is the first node to be added and the list's head and tail pointers are set to the new node
Insertion in the middle of singly-linked list
if a singly-linked lists head pointer is not null and the curNode is not pointing to the tail node, point the new node's next pointer to the curNode's next node and point the curNode's next pointer to the new node so that it is now the node after curNode
removing node after curNode in singly-linked list
if curNode is not null, the sucNode is pointed to the node after the node being removed(curNode's next node) and curNode's next pointer is set to the sucNode. if the sucNode is null then the list's tail pointer is pointed to the curNode
Removing singly-linked list's head node, special case
if the curNode is null, which points to the list's head node, the sucNode is pointed to the list's head node's next node, and the list's head pointer is pointed to the sucNode but if sucNode is null, then the only node was removed and the list's head and tail pointer snow point to null
Prepending to empty doubly-linked list
if the doubly-linked list's had pointer is null, the list's head and tail nodes are pointed to the new node being added in front
inserting in the middle of doubly-linked list
if the doubly-linked list's head pointer is not null and curNode is not pointing to the tail node, the sucNode is the curNode's current next node, the new node's next node pointer is set to the sucNode, the new node's previous pointer is set to the curNode, the curNode's next pointer is reset to the new node, and the sucNode's previous pointer is reset to the new node.
inserting after the tail node of doubly-linked list
if the doubly-linked list's head pointer is not null and the curNode is pointing to the tail node, the tail node's next pointer is set to the new node, the new node's previous pointer is set to the list's current tail node, and the list's tail pointer is reset to the new node making it the list's new tail node.
Prepending to a non-empty doubly-linked list
if the doubly-linked list's head pointer is not null, the new node's next pointer is set to the current list head node, the current head node's previous pointer is set to the new node, and the list's head pointer is reset to the new node making it the new list head node
Appending to non-empty doubly-linked list
if the doubly-linked list's head pointer is not null, the now node's previous pointer is se to the current tail node, the current tail node's next pointer is set to the new node, then the list's tail pointer is reset to the new node making it the list's new tail node
inserting as first node in doubly-linked list
if the doubly-linked list's head pointer is null the list's head and tail pointers are set to the new node, insertAfter
Appending to empty doubly-linked list
if the doubly-linked list's head pointer is null, then the list's head and tail pointer are set to the new node to add after
List PrintReverse Operation
prints the list's elements in reverse order (back to front)
List Remove Operation
removes the specified element(item/value, not index)
List isEmpty Operation
returns True if the list has no elements
List Search Operation
returns an element if it is found or null if it is not
List GetLength Operation
returns the number of elements in the list
the remove a node from a doubly-linked list the first thing to happen is that the sucNode is set to point to the curNode's next node, and the predNode is set to point to the curNode's previous node
what are the first two steps in removing a node from a doubly-linked list