$\require{cancel}$
Introduction to Linked Lists.
- What is an array?
- Name the primary characteristics of an array.
- Homogeneous mass storage
- Constant access time.
- Arrays are good as generic containers.
- I would like to read data in from a file, sort it once and search it many times.
- Where is is inexpensive to insert into an array and where is it expensive?
- Why does the vector have a push_back function and not a push_front?
- But under some circumstances we have more of a challenge.
- What if I have dynamic data that I want to keep sorted.
- Data arrives or departs at any given time.
- Think inventory in a store.
- We will sort by item number.
- In this case
- An Item arrives and we insert it into the proper place in the array.
- We sell the last item so we delete it from the array.
- What does it take to do each of these operations?
- Trace inserting/deleting something from the list
- This becomes especially bad when it is expensive to move data.
- One common complaint here is that extending an array is expensive
- Allocate memory for the larger array.
- Move the old array over and add the item
- Delete the memory for the old array.
- When we are not careful, this makes insert O(n), where n is the number of items in the array.
- A more careful algorithm can make this O(1), but requires essentially double the space
- Keep both a size, the number of elements and a capacity, the allocated space.
- When size == capacity, reallocate, doubling capacity.
- Eventually this will take O(1) for each insert on average.
- But There are other data structures.
- A Linked List
- The basic component is a node.
- This is probably a structure
- The data for that node.
- A link to the next node, normally called next.
- The head of the list points to either nullptr or the first item in the list.
- The tail of the list (optional) points to nullptr or the last element in the list.
- Sorry, I am at home, no drawing software.
- To insert an element
- Find one before where it goes
- Allocate a node
- Store the data in the node
- Make the node's next pointer point to the place past the insert.
- Make the previous node's next pointer point to the new node.
- Special cases for first and last.
- To delete a node
- Find the node before the one we wish to delete
- Make a temp pointer to the node we wish to delete.
- Make the previous node's next pointer point to the think past the node we wish to delete.
- Delete the node.
- How would we traverse a linked list (visit all nodes)?
- How would we search a linked list?
- Can we do binary search?
- Effectively?
- Can we index into elements, effectively?
- We can make multiple different versions of this.
- A circular linked list has pointers from the tail to the head.
- This is of somewhat limited value, but if you need it, it is great.
- A doubly linked list has pointers to each of its neighboring nodes.
- This is very useful, but there is twice as much memory devoted to book keeping.
- In general
- Arrays are good for static data.
- Read in, sort once search many.
- Especially if we can do binary search.
- Linked lists are good for dynamic data
- Where the size keeps changing
- Frequent deletes and inserts.
- But linear search.