Three Data Structures
- The outline indicates that we are to discuss three data structures.
- All three are containers
- The default behavior is to hold a collection of homogeneous data.
- The stack and queue are time based
- The list is not.
- There are several ways to implement each.
- static implementations are usually based on an array.
- These implementations have limited memory.
- Thus they can become full.
- dynamic implementations are based on dynamic memory.
- These implementations can grow to the size of memory.
- Queue ADT
- This is a line (remember in Harry Potter they "queued up").
- The first one in the line is the first one out (FIFO)
- It is useful for situations where you need to handle things in the order they arrive.
- Like a line, a queue has a front and a rear
- New items are placed at the rear of the queue.
- Items are removed from the front of the queue.
- Queue Methods
- Enqueue(element) - add an element to the rear of the queue
- Dequeue(void) - remove the front element from the queue.
- Front(void) - return the front item of a queue.
- Size() - return the number of elements in the queue
- IsEmpty() - return true if the queue is empty.
- There can be other specialized methods.
- When possible, all operations on a queue should be O(1).
- Because of the nature of the queue, sorting is usually not permitted.
- Searching is not a general operation of a queue.
- Stack ADT
- A collection of data where the last item added is the first item removed (LIFO)
- Think stack of books.
- Stacks are useful when you are dealing with a situation where a task might be interrupted and you need to come back to it.
- A stack has a top.
- Items are put on the top of the stack.
- Items are removed from the top of the stack.
- Stack Methods
- Push(element) - add an element to the top of the stack.
- Pop(void) - remove and return an element from the top of the stack.
- Top(void) - return, but do not remove an element from the top of the stack.
- Size(void) - return the size of the stack.
- IsEmpty(void) - return true if the stack is empty.
- There can be other specialized methods.
- All operations should be O(1).
- It is not normal to search or sort a stack.
- The List ADT
- A collection of elements.
- Lists are akin to arrays.
- Many more variations than a stack or queue.
- Lists may be ordered or unordered
- Lists are linear structures.
- There is no guarantee that items next to each other in the list are next to each other in memory.
- Each item contains information on how to find the next item.
- Think train cars.
- Some methods
- Insert(element) - add an element to the list
- Delete(element) - remove an element from the list
- Find(element) - return true if the element is in the list.
- Iterate through the list returning each element.
- Size(void) - return the size of the list.
- IsEmpty(void) - return true if the list is empty.
- Performance
- Find is O(n);
- Delete is O(1), assuming we have found the item.
- Insert is O(1), assuming we have located where we want to store the item.
- The big difference between arrays and lists, to insert when full
- To insert when full:
- in an array you need to reallocate it and copy over all elements
- In a linked list you just insert.
- To delete an item
- In an array you need to move items to fill the hole.
- In a list you just remove the element.
- To Search
- Sequential search is the same.
- Binary search works on an ordered array.
- Binary search does not work on a list