131 3/30/98 CSE 143 More Collection ADTs [Sections 4.6-4.8]
-
Upload
phoebe-oneal -
Category
Documents
-
view
220 -
download
1
Transcript of 131 3/30/98 CSE 143 More Collection ADTs [Sections 4.6-4.8]
13/30/98
CSE 143
More Collection ADTs[Sections 4.6-4.8]
23/30/98
More Collection ADTsWe’ll study the following “classic” ADTs
Stack: ordered collection accessible in LIFO mannerQueue: ordered collection accessible in FIFO mannerSet: unordered collection without duplicatesTable: unordered mapping of keys to values (e.g. names -> StudentRecords)
We’ll spend much of the rest of the quarter discussing various implementation techniquesThose techniques can then be used with every more complicated data structures.
33/30/98
Stack ADTTop: Uppermost element of stack,
first to be removedBottom: Lowest element of stack,
last to be removedHeight or Depth: Number of elementsElements are always inserted andremoved from the top (LIFO)
Homogeneous collection
...
top
bottom
aStack:
43/30/98
Stack Operationspush(item): Adds an element to top of stack, increasing stack height by one
pop(): Removes topmost element from stack and returns it, decreasing stack height by one
top(): Returns a copy of topmost element of stack, leaving stack unchanged
No cursorNo iteration via reset(), advance(), etc.
53/30/98
Stack ExampleShow the changes to the stack in the following example:
Stack s;
s.push(5);
s.push(3);
s.push(9); int v1 = s.pop();
int v2 = s.top();
s.push(6);
s.push(4);
63/30/98
Common Uses of Stacks
Implementing function calls Postfix notation
4 7 + 5 * instead of (4 + 7) * 5push operands until operator found, pop 2 values, apply operator, push result
Backtracking, e.g., to explore paths through a maze
73/30/98
Stack Interface
class IntStack {
public:
IntStack();
bool isEmpty(); // is the stack empty?
bool isFull(); // is the stack full?
void push(int item); // add item to top
int pop(); // remove top item
int top(); // show the top item
private:
. . .
}
83/30/98
A Stack Client// Read numbers and print in reverse order
void ReverseNumbers() {IntStack s;int oneNumber;
while ( cin >> oneNumber ) { assert ( !s.IsFull() ); s.push(oneNumber);}
while ( !s.IsEmpty() ) cout << s.pop() << endl;
}
93/30/98
Possible ImplementationsMany possible implementations
Array-basedDynamic memory structure (later)List-based
As implementor, use other ADTs to make job easierDon’t reinvent the wheel for every problemOften simplifies job to reuse pieces when possible
103/30/98
Stack InterfaceWe’ll use a hidden list.
#include “IntList.h”
class IntStack {
public:
Stack();
bool isEmpty(); // is the stack empty?
bool isFull(); // is the stack full?
void push(int item); // add item to top
int pop(); // remove top item
int top(); // show the top item
private:
IntList items;
}
113/30/98
Stack Implementation (2)
IntStack::IntStack() { }
// don’t need to do anything, why?
bool IntStack::isFull() {
return items.isFull();
}
bool IntStack::isEmpty() {
return items.isEmpty();
}
123/30/98
Stack Implementation (3)
void IntStack::push(int item) {
items.insertBefore(item);
}
int IntStack::pop() {
int rval = items.data();
items.deleteItem();
return rval;
}
int IntStack::top() {
return items.data();
}
133/30/98
Queue ADTHomogeneous, ordered collection, accessed only at the front (removal) and rear (insertion)Front: First element in queueRear: Last element of queue FIFO: First In, First Out
front rear
...aQueue:
143/30/98
Queue Operationsenqueue(item) : Adds an element to rear of queuedequeue() : Removes and returns element at the front of queuefront() : Returns a copy of the front element of queueNo cursorNo iteration via reset(), advance(), etc.
153/30/98
Queue ExampleShow the changes to the queue in the following example:
Queue q;
q.enqueue(4);
q.enqueue(7);
q.enqueue(5);
int v1 = q.dequeue();
int v2 = q.front();
q.enqueue(9);
q.enqueue(2);
163/30/98
Common Uses of QueuesManaging a first-come, first-serve line
Processes in an operating systemPrint jobs at the printer
Buffering input/outputWhen printing to screen with cout, characters don’t appear right away: they are buffered and sent out in groups.
Mouse events
SimulationsPeople waiting in line for service
173/30/98
Queue Interface
class IntQueue {
public:
IntQueue();
bool isFull();
bool isEmpty();
void enqueue(int item);
int dequeue();
int front();
private:
IntList items;
// again, we’ll use a hidden list
}
183/30/98
Queue Implementation (1)IntQueue::IntQueue() { }
bool IntQueue::isFull() {
return items.isFull();
}
bool IntQueue::isEmpty() {
return items.isEmpty();
}
void IntQueue::enqueue(int item) {
while (!items.endOfList())
items.advance();
items.insertBefore(item);
}
193/30/98
Queue Implementation (2)
int IntQueue::dequeue() {
items.reset();
int rval = items.data();
items.deleteItem();
return rval;
}
int IntQueue::front() {
items.reset();
return items.data();
}
203/30/98
Set ADTAttributes of set type
Nonlinear collection of elementsVarying number of elementsNo duplicates
Operations on setsElement operations: add, remove, isMemberAggregate operations: union, intersection, difference
Possibly Iteration: reset, advance, end, data
213/30/98
Set TerminologyUnion: Create a new set whose elements are those found in either set
Intersection: Create a new set whose elements are those found in both sets
Difference: Remove elements found in one set from other set
423
97
1 3 10
68
aSet:
223/30/98
Set ExamplesShow the changes to the sets in the following example
Set s1, s2;s1.add(4);s1.add(8);s1.add(14);s2.add(3);s2.add(4);s2.add(5);Set s3 = s1.union(s2);Set s4 = s1.intersection(s2);Set s5 = s1.difference(s2);
233/30/98
Set Interfaceclass IntSet {
IntSet();
// standard size(), isFull(), isEmpty() methods
bool isMember(int item);
void add(int item);
void remove(int item);
Set union(Set other);
Set intersection(Set other);
Set difference(Set other);
// iteration member functions here . . .
private:
IntList items; // again, we’ll use our trusty
} // IntList
243/30/98
Table ADTAttributes of table type
Set of key/value pairsNo duplicate keys
Operations on tableslookup value from keyinsert value at keyremove key and associated value from table
Uses:Phone book, class roster, book index, databases
Sometimes also called a dictionary
253/30/98
Table TerminologyKey: Portion of pair used to look up data, like an index (aka domain value)
Value: Portion of pair that contains data (aka range value)
aTable:“4476542K”“3828122E”“24601JVJ”
23,440
“994802WE”“8675309A”
Key (employee ID) Value (salary)
27,64015,20345,21028,776
263/30/98
Example of Operations
// phone book example:
Table pb;
pb.insert(“Julie”, 5552345);
pb.insert(“Bob”, 3450011);
pb.insert(“Bart”, 6661212);
int bartsNumber = pb.lookup(“Bart”);
pb.delete(“Bob”);
273/30/98
Table Interfaceclass Table {public: Table(); // Create new table
int size(); // Return size of table bool isEmpty(); // Is table empty? bool isFull(); // Is table full?
RangeType lookup(DomainType key); void insert(DomainType key, RangeType value);
RangeType remove(DomainType key);
private: . . .
};
283/30/98
Table ImplementationMany different possibilities
Array of structsList of structsDynamic memory structure
Implementations may make use of future topicsSearching, sortingHash tables
No implementation details at this time...
293/30/98
SummaryStack
List with LIFO structureAccess via push(item), pop(), and top()
QueueList with FIFO structureAccess via enqueue(item), dequeue(), and front()
SetUnordered collection, without duplicates
TableUnordered association of key-value pairsAccess via insert(key, value), delete(key), lookup(key)