Chapter 3 Lists, Stacks, Queues
Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations that can be performed on the items – Nothing about HOW to do the operations For example add to the end Length of list
C++ Classes and ADT The C++ class facility makes it easy to implement ADTs. – Hides the details from the user – Easy to change the details (would not have to change programs that use the class) – The interface (argument sequence) should never have to change (if it does, then it was a poor design).
List (not linked list) A basic ADT is a List. A list holds a collection of items – Class roll – Shopping list Each item has a position Various operations – Print, makeEmpty, insert, remove, find – Must determine error conditions Remove from empty list, find value not there…
Implementations – Array Very easy to use an array to implement the List ADT. Fixed size Easy to add and remove at the end Easy to find – Very easy to find if list is ordered Easy to go to a specified position (findKth) “Hard” to add and remove from anywhere else – Must move items up or down (think of big lists) NOTE: Easy and hard are computer time, not programmer effort.
Implementation – Linked Lists Easy to add to beginning Can be easy to add to end (if keep an end pointer) Easy to add after/before any position (just change pointers after you get to that position) findKth not as easy as array based Can be done with dynamic memory (no fixed size).
Doubly Linked Lists If we ever need to “back up” in a linked list, it will be very difficult. – Have to start at beginning, keep a previous pointer and go until get to “current” node. Instead, have a node keep a previous (as well as next) link. The first node’s previous will be NULL. More pointers to change when inserting or deleting, but still just a fixed amount of code to execute – O(1). Also makes insertInOrder easier (no need to keep a previous pointer.
Stack Last-in-First-out model. The only real rule is that when we remove from a stack, we get the last thing added (that has not already been removed) – How that is implemented is up to the designer Operations – Push, pop (mandatory) – Nice: top, size, isempty….
Stack Implementation – Array Keep an array and a variable to tell where the top of the stack is. Push will put something into the array and change the top pointer Pop will return a value and change the top pointer Top will return a value and NOT change the top pointer
Stack Implementation – Linked List Use a linked list with just a few methods: Push == insert at beginning Pop == remove from beginning Top == return value of first node
Stack Uses Find parentheses pairs (or any pairs {} []…) Evaluating reverse polish (postfix) expressions Infix to postfix conversion Function call returns (allows recursion)
Queues Easy with Linked List (add to end, remove from the beginning). Need to use a circular array when implementing with an array
Header Node Have a header node at the beginning of a linked list (node created in the constructor). This way the Linked List always has a “NODE” (but it is not counted in the size). – Always skipped; has no data Now, never have to check for empty list. – Never have to change the head pointer. Most other methods have to be changed to account for the header node.