70

Lists, Stacks and Queues - Binghamton University · Array-Based List Implementation Code derived from textbook inalist.hh. AList inherits from List . AList provides concrete implementations

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Overview

Abstract collections.

Will look at array and linked-list implementations ofsequences, stacks and queues.

Introduction to asymptotic algorithm analysis.

Introduction to OOP in C++ with discussion of abstractclasses, inheritance and virtual functions.

Lists, Stacks and Queues

Abstract Collections

A collection of objects having the same type.

Usually unbounded in size. Possible to query current size.

Usually supports iterating over objects in the collection.

Note: A tuple is usually not regarded as a collection because:

The types of the contained objects need not be the same.

The size is �xed: pair, n-tuple.

No iteration: access objects using accessor functions likefirst(), second(), nth(), etc.

Lists, Stacks and Queues

Abstract Sequences

A sequence is an ordered collection of elements.

It can contain duplicate elements.

It should be possible to go forward from an element to thenext element.

It may also be possible to easily go backward from an elementto the previous element.

Lists, Stacks and Queues

Abstract Vector or Array

Extends an abstract sequence.

Provides random access: it is possible to go easily to anarbitrary element irrespective of the current element.

Will provide some kind of indexing operation: given azero-based integer index, return corresponding object.

Lists, Stacks and Queues

Abstract Map

Provides a mapping between Key objects to Value objects.

Keys may be restricted to "primitive" types like integers andstrings.

Values can be arbitrary objects.

There must be some notion of equality of keys.

Will provide a lookup operation: given a key, �nd itsassociated value.

Also known as dictionary, associative array.

Usually unordered.

If ordered, may be ordered by insertion order or by someordering on keys.

Lists, Stacks and Queues

Abstract Set

Collection of objects without any ordering.

No duplicates.

Need some notion of equality for stored objects.

Will at least support testing whether some object x belongs toset S , i.e. x ∈ S .

May also support operations like set union S1 ∪ S2, setintersection S1 ∩ S2, set containment S1 ⊂ S2; less commonlyset relative-di�erence S1 − S2.

Lists, Stacks and Queues

Abstract Bag or Multiset

Like a set but allows duplicates.

An element has an associated multiplicity which is a positiveinteger.

Some set operations may be supported, but extended toaccount for multiplicities.

Can always be implemented as a Map mapping elements totheir multiplicity.

Lists, Stacks and Queues

Abstract Tree

Maintains a hierachical parent-child relationship between theobjects stored in the collection.

Usually ordered.

Each stored object is wrapped by a node of a tree.

There is a single node without any parents called the root.

A node without any children is called a leaf.

Typical accessor methods on nodes are parent() ornthChild().

Lists, Stacks and Queues

Exercise

Assume that all Person objects have an id �eld which uniquelyidenti�es a Person. All Person objects also have a firstName andlastName �eld, but multiple Person objects may have the samevalues for those �elds. You should also assume that a Person

object has many other �elds, like address, phoneNumber, etc.How would you organize a collection of Person objects, so that it iseasy to �nd all those objects which satisfy speci�ed values for theid, firstName and lastName �elds?

Lists, Stacks and Queues

Solution to Exercise

One solution would be to have 3 maps:

1 idMap: Map<ID, Person>.

2 �rstNameMap: Map<FirstName, Set<ID>>

3 lastNameMap: Map<LastName, Set<ID>>

To perform a search given a firstName and lastName of Jim andBrown, we would look up firstNameMap by Jim and get the IDs ofall people with �rst name Jim, look up lastNameMap by Brown andget the IDs of all people with last name Brown, intersect the twosets to get the ID's of all people named Jim Brown and then lookup idMap with those IDs.Note that if looking up a person by both firstName and lastName

is a very common operation, then we may want to have anadditional map Map<Pair<FirstName, LastName>, Set<ID>>.

Lists, Stacks and Queues

Abstract Data Types

A data type which hides its implementation behind a set ofoperations is referred to as an Abstract Data Type or ADT.

A list of operations may be referred to as an interface.

Unlike other languages, C++ does not support interfaces.Instead, the list of operations are gathered together as purevirtual functions in an abstract class.

Since there is no implementation for a pure virtual function, itis not possible to create an instance of an abstract class.

Classes which implement the operations inherit from theabstract class. This is an example of interface inheritance.

Code (very similar to textbook code) for a List abstract classwhich corresponds to the abstract sequences discussed earlier.

Lists, Stacks and Queues

Using an ADT

An ADT can be used without knowing anything about how it isimplemented:

/** Return true i� key is in list. */bool

�nd(List<int>& list, int key) {

for (list.moveToStart();

list.currPos() < list.length();

list.next()) {

auto it = list.getValue();

if (key == it) return true; //found}

return false; //not found}

Lists, Stacks and Queues

Array-Based List Implementation

Code derived from textbook in alist.hh.

AList inherits from List.

AList provides concrete implementations of all the purevirtual functions in List.

An AList can be used wherever a List element is expected.AList is referred to as a sub-class or subtype of List.

Strong behavorial subtyping: Liskov substitution principle:If S is a subtype of T, then objects of type T may be replacedwith objects of type S (i.e. an object of type T may besubstituted with any object of a subtype S) without alteringany of the desirable properties of the program (correctness,task performed, etc.)

Lists, Stacks and Queues

Inserting an Element to AList

Lists, Stacks and Queues

Analysis of Array List Operations

Inserting an element using insert() requires shifting allsubsequent elements up by 1 position. Hence it will take timeproportional to the number of subsequent elements, so itdepends on the number of elements in the list.

Getting current element using getValue() simply accesseselement at current index. This takes constant timeindependent of the size of the list.

Destroying ~AList() and clearing ~clear() involves callingdelete[] which may take time dependent on the allocatedsize of the array.

Lists, Stacks and Queues

Exercise

Look at other operations for AList and determine whether theytake constant time or time dependent on the number of elements inthe list.

Lists, Stacks and Queues

Solution to Exercise

Since clear() depends on delete[], it may take timedependent on the allocated size of the array.

remove() will take time dependent on the number of elementsin the list.

All other operations take constant time independent of thenumber of elements in the list.

Lists, Stacks and Queues

AList Log

$ ./main xx

invalid container xx; must be one of AList,

LinkedList, DLinkedList

$ ./main AList

CMD [INT_VALUE [REPEAT_COUNT]]

CMD is one of help|moveToPos|moveToEnd|moveToStart|

remove|append|next|prev|insert|clear

>> insert 1

>> [ 1 ]

0 millis

>> insert 2

>> [ 2, 1 ]

0 millis

>> append 3

>> [ 2, 1, 3 ]

0 millis

Lists, Stacks and Queues

AList Log Continued

>> remove

>> [ 1, 3 ]

0 millis

>> insert 99 3000

>> [ 99 * 3000, 1, 3 ]

33 millis

>> append 88 3000

>> [ 99 * 3000, 1, 3, 88 * 3000 ]

0 millis

Lists, Stacks and Queues

AList Log Continued

>> moveToPos 99

>> [ 99 * 3000, 1, 3, 88 * 3000 ]

0 millis

>> remove

>> [ 99 * 2999, 1, 3, 88 * 3000 ]

0 millis

>> moveToPos 2999

>> [ 99 * 2999, 1, 3, 88 * 3000 ]

0 millis

>> remove

>> [ 99 * 2999, 3, 88 * 3000 ]

0 millis

>>

Lists, Stacks and Queues

Linked List Implementation

Each data item is wrapped within a Link node which containsa pointer pointing to the next item in the list.

Can traverse the list in the forward direction easily.

Di�cult to traverse the list in the backward direction.

Code in linkedlist.hh.

Lists, Stacks and Queues

Initializing Linked List

Lists, Stacks and Queues

Linked List Insert

Lists, Stacks and Queues

Linked List Remove

Lists, Stacks and Queues

Linked List Log

>> insert 1

>> [ 1 ]

0 millis

>> insert 2

>> [ 2, 1 ]

0 millis

>> append 3

>> [ 2, 1, 3 ]

0 millis

>> insert 99 3000

>> [ 99 * 3000, 2, 1, 3 ]

1 millis #e�cient to insert at head>> append 88 3000

>> [ 99 * 3000, 2, 1, 3, 88 * 3000 ]

0 millis #e�cient to insert at tail#or at any other position

>> Lists, Stacks and Queues

Comparison of Array List vs Linked List Implementations

Array lists requires preallocation of some �xed number ofelements.

Array lists cannot grow beyond predetermined size.

Linked lists have no limits on their size beyond availablememory.

Array lists are more e�cient than linked lists for getting to theprevious element. In fact, they facilitate random access whichcan be critical when the list needs to be sorted.

Linked lists require memory overhead of storing an extrapointer along with every stored element.

Array list is less e�cient in use of memory when it has fewelements; more e�cient when it is getting close to full.

Lists, Stacks and Queues

Determining Cuto� Between Array List vs Linked ListImplementations

Assume that element size is E bytes, P is the size of a pointerand n is the number of elements currently stored in the list.

If an array list has a preallocated size of D, then it will alwaysuse D × E bytes, irrespective of n.

A linked list node will occupy (P + E ) bytes. Hence with nelements stored in a linked list, it will use n × (P + E ) bytes.

The array list is more space e�cient than a linked list if thespace for the linked list is greater; i.e. n× (P + E ) > D × E orn > D × E/(P + E ).

If P = E , break-even point is at D/2.

Lists, Stacks and Queues

Exercise

Assume that size of a pointer is 8 bytes and that of a data elementis 56 bytes. Also assume that you normally expect to store around100 elements, but would like to give yourself a cushion of 20%.How many elements would you need to store before an array listimplementation became more space e�cient than a linked listimplementation?

Lists, Stacks and Queues

Solution to Exercise

We have P = 8 and E = 56. With a 20% cushion we haveD = 120.

Hence the array list will be more space e�cient than a linkedlist when the number of elements n > D × E/(P + E ), i.e.n > 120× 56/(56+ 8) which is n > 120× 7/8, i.e. n > 105.

Since 105 is pretty close to the expected number of elementsof 100, an array list seems like a reasonably e�cient choice inthis situation.

Lists, Stacks and Queues

Doubly Linked Lists

Store both a forward and reverse link in each node.

Allows easy traversal in both forward and reverse directions.

Use two header nodes to refer to the head and tail.

Code uses invariant that curr always points just before thereal current element. This is for consistency with singly-linkedlist implementation.

Code in dlinkedlist.hh.

Lists, Stacks and Queues

Example of a Doubly Linked List

Lists, Stacks and Queues

Doubly Linked List Insert

Lists, Stacks and Queues

Doubly Linked List Remove

Lists, Stacks and Queues

Exercise

Repeat previous exercise, but for a doubly linked list.Assume that size of a pointer is 8 bytes and that of a data elementis 56 bytes. Also assume that you normally expect to store around100 elements, but would like to give yourself a cushion of 20%.How many elements would you need to store before an array listimplementation became more space e�cient than a doubly linkedlist implementation?

Lists, Stacks and Queues

Solution to Exercise

With 2 pointer �elds per node, the array list will be morespace e�cient than a linked list when the number of elementsn > D × E/(E + 2× P),

We have P = 8 and E = 56. With a 20% cushion we haveD = 120.

Hence the array list will be more space e�cient than a linkedlist when the number of elements n > 120× 56/(56+ 2× 8)which is n > 120× 7/9, i.e. n > 93.3.

Lists, Stacks and Queues

Aside: Exchange without Temporary

//normal xchg idiomvoid swap(int& x, int& y) {

int t = x; //temporaryx = y;

y = t;

}

//swap without temporaryvoid swap(int& x, int& y) {

//x = x0, y = x0x = x^y; //^ is xor, x=x0^y0, y=y0y = x^y; //x=x0^y0, y = x0x = x^y; //x = y0

}

Lists, Stacks and Queues

Doubly Linked List with Only a Single Pointer Field

Can use a similar idea to implement a doubly linked list whichuses only a single pointer �eld.

When traversing forward, will need address of previous listnode in order to extract address of next node.

When traversing backward, will need address of next list nodein order to extract address of previous node.

Will explore further in next project.

Lists, Stacks and Queues

Lists Perspective

Lists as described in textbook essentially implementfunctionality of an abstract sequence.

May not be implemented explicitly, but implicitly as part ofother data structures. For example, some data structure mayhave multiple lists threaded through it to facilitate varioustraversals.

Linked implementations preferred unless sorting needed.

Lists, Stacks and Queues

Stacks

List with all action occurring only at one-end, referred to astop.

AKA Last-In, First-Out LIFO lists.

Insert operation is called push.

Remove operation is called pop.

Interface code in stack.hh. Poor semantics for clear()operation.

Lists, Stacks and Queues

Array-Based Stack

Since insertion at end of array is more e�cient (no shifting ofelements required), we maintain top-of-stack as end-of-array.

Have a �eld called top which indexes top-of-stack. Need tode�ne this more clearly.

Could have top index element actually on top-of-stack. In thatcase, an empty stack would have top initialized to -1.

Could have top index next free location in stack. In that case,an empty stack would have top initialized to 0.

Code in astack.hh uses second alternative.

Lists, Stacks and Queues

Linked List Based Stack

Straight-forward linked list implementation with all actionoccurring at start of list.

Trivial code in lstack.hh.

Lists, Stacks and Queues

Comparison of Array vs Linked List Stacks

push() and pop() are constant time for both.

Analysis of space requirements for array lists vs linked lists alsocarries over to stacks.

Array-based stacks can support two stacks, with one stack atone end and the other at the other end. Best if the two stackshave opposite growth patterns where as one stack grows theother shrinks.

Lists, Stacks and Queues

Use of Stacks for Implementing Function Calls

Modern programming languages use stacks for implementingfunction calls.

When a function calls another function, the callee needs toknow which address it should return to in the caller.

When the callee returns, it also needs to restore theenvironment, i.e. the variable values (aka bindings) in thecaller.

Achieved by pushing activation records onto a stack, with anactivation record for a callee containing the return address ofthe caller and the environment of the current callee.

Makes it possible to support recursive functions as illustratedby a trace of a recursive factorial on the next slide.

Lists, Stacks and Queues

Trace of Recursive Factorial for fact(4)

Lists, Stacks and Queues

Using an Explicit Stack instead of Recursion

long fact(int n) {

return (n > 1) ? n*fact(n - 1) : 1L;

}

//use explicit stacklong stackFact(int n, Stack<int>& S) {

// To �t n! in a long variable, require n <= 12assert((n >= 0) && (n <= 12) &&

"Input out of range");

while (n > 1) S.push(n--); // Load up the stacklong result = 1; // Holds �nal resultwhile(S.length() > 0) {

result *= S.pop(); // Compute}

return result;

}

Text also discusses a less trivial replacement for Towers of Hanoiproblem.

Lists, Stacks and Queues

Queues

Queues support a �rst-in, �rst-out FIFO discipline (unlikestack's LIFO discipline).

Adding an element to the rear of a queue is referred to as anenqueue operation.

Removing an element from the front of a queue is referred toas an dequeue operation.

Queue interface code in queue.hh.

Lists, Stacks and Queues

Queue Implementations

Linked list based queues are trivial.

Array-based queues are non-trivial. If we try to use an arraylist for a queue:

If array element 0 is regarded as the front of the queue, thenenqueue() becomes a simple constant-time append()

operation, but dequeue() becomes an expensive remove()

operation, with time proportional to the number of queuedelements.If array element 0 is regarded as the rear of the queue, thendequeue() becomes a simple constant-time remove()

operation, but enqueue() becomes an expensive insert()

operation, with time proportional to the number of queuedelements.

Lists, Stacks and Queues

Array-Based Queue Implementation

Instead of �xing front/rear of queue to array index 0, implementarray based queue by having front and rear indexes driftingthrough the array. So both dequeue() and enqueue() becomeconstant time operations.

Lists, Stacks and Queues

Circular Array Queue Implementation

Treat array implementing the queue as a circular array with indexincrement done modulo N, where N is the size of the array.

Lists, Stacks and Queues

Circular Array Queue: Empty or Full?

Assume that front indexes �rst element of queue, and rear

indexes last element in queue.

If front == rear, then queue has size 1 and both front andrear index same element. Looks good.

But since front indexes �rst element of queue, how do werepresent an empty queue using only front and rear indexes?

Lists, Stacks and Queues

Circular Array Queue: Empty or Full Continued

Assuming that all the N slots in the array can be occupied byelements, queue can contain 0, 1, 2, . . . ,N elements, i.e. queuehas N + 1 states.

For a �xed front, rear can only have N distinct values0, 1, 2, . . . , (N − 1).

Cannot distinguish N + 1 states using only N distinct values ofrear.

Pigeonhole principle: If we put M objects (pigeons) into Nslots (pigeonholes) with M > N, then at least one slot willcontain multiple objects.

So we cannot distinguish the N + 1 states using a rear

pointer having only N values.

Lists, Stacks and Queues

Circular Array Queue: Empty or Full Continued

One alternative is to maintain a count of number of elementsin the queue.

Another alternative is to maintain a boolean variable isEmptywhich is true i� the queue is empty (when true the array slotsindexed by front and rear are empty). Will need special codeto handle insertion into an empty queue. A full queue will haveisEmpty false and front == (rear + 1) % N.A third alternative is to ensure that the array always has anempty slot, guaranteeing N + 1 slots for N + 1 states.Assuming that the occupied queue elements are at indexesfront (inclusive) to rear exclusive modulo N clockwise andthat we always have an empty slot between rear and front:

Queue is empty when front == rear.Queue is full when front == (rear + 1)%(N + 1).

[Note that the assumption that rear is exclusive is di�erentfrom the textbook which treats rear as inclusive.]

Code in aqueue.hh

Lists, Stacks and Queues

Exercise

Assume that we want a queue to have up to 4 elements. Assumean array implementation without using any auxiliary variables otherthan indexes front and rear, where front is inclusive and rear isexclusive:

1 How many elements should the array have?

2 What is the condition for the queue being empty?

3 What is the condition for the queue being full?

4 How do you initialize front and rear when creating a newqueue?

Lists, Stacks and Queues

Solution to Exercise

1 Underlying array should have 5 elements.

2 front == rear.

3 (rear + 1) % 5 == front.

4 Set both front and rear to the same value in [0, 4], typically0.

Lists, Stacks and Queues

Searching an Integer Array

Code in main.cc.

A successful search could �nd a match for the key with the�rst array value, the second array value, .., the last array value.

On average, assuming that successfully looked up keys areuniformly distributed across the stored values, a successfulsearch will need to look at half of the array values before�nding a match.

Unsuccessful searches will always need to scan the entire arraybefore reporting failure.

Appending each integer to the array is a constant-timeoperation.

Lists, Stacks and Queues

Searching an Ordered Integer Array

Code in main.cc.

Di�erence from unordered search is that unsuccessful searchescan report failure without scanning the entire array.

Disadvantage is that inserting an element into the sorted arrayis no longer a constant time operation.

Can do better.

First, let's look at what "ordered" means?

Lists, Stacks and Queues

Sets

A set S is a collection of distinct entities, no concept ofordering.

The empty set is denoted as ∅.Fundamental operation on sets is membership. Given a set S ,we say a ∈ S if a belongs to S and a 6∈ S if a does not belongto S . For all a, a 6∈ ∅.The size of set S referred to as its cardinality is denoted as|S |.If every element a ∈ S1 also belongs to set S2, we say S1 is asubset of S2, denoted as S1 ⊆ S2. If S1 6= S2, we say S1 is aproper subset of S2, denoted as S1 ⊂ S2.

The powerset of a set S denoted as P(S) or 2S is the set ofall its subsets. We have |P(S)| = 2|S| explaining the alternate2S notation for denoting a powerset.

Lists, Stacks and Queues

Set Operations

Given two sets S1 and S2, we have the usual set operations ofunion S1 ∪ S2, intersection S1 ∩ S2, relative di�erence

S1 − S2.

The cartesian product S1 × S2 of two sets S1 and S2 is theset of pairs 〈a, b〉 where a ∈ S1 and b ∈ S2. Can be extendedto cartesian product of n-sets S1, S2, . . . , Sn.

Lists, Stacks and Queues

Relations

Given sets S1, S2, . . . , Sn, any set R ⊆ (S1 × S2 × . . .× Sn) is saidto be an n-ary relation on S1, S2, . . . , Sn.

If n = 2, then R is said to be a binary relation. For example,if Cars = { car_a, car_b, car_c } and Colors = {silver, gray, red },

{ <car_a, red>, <car_b, silver>, <car_c, red> } is arelation on Cars × Colors,{ <red, car_a>, <silver, car_b>, <red, car_c> } is arelation on Colors × Cars,

If 〈a, b〉 ∈ R often written in in�x notation as aRb.

If n = 1, then R is said to be a predicate. For example, ifColors is a set of colors like { red, blue, green,

magenta, black, white, yellow }, then a predicate forthe standard additive primary colors is { <red>, <blue>,

<green> }.

Lists, Stacks and Queues

Binary Relation Properties

A binary relation R ⊆ S × S is:

Re�exive if aRa for all a ∈ S .

Symmetric if whenever aRb, then bRa, for all a, b ∈ S .

Antisymmetric if whenever aRb and bRa, then a = b, for alla, b ∈ S .

Transitive if whenever aRb and bRc , then aRc for alla, b, c ∈ S .

For natural numbers N , < is antisymmetric (can't have bothn1 < n2 and n2 < n1), ≤ is re�exive, antisymmetric and transitive,= is re�exive, symmetric, antisymmetric and transitive.

Lists, Stacks and Queues

Equivalence Relations and Partitions

A binary relation R is a equivalence relation if it is re�exive,symmetric and transitive.

If 〈a, b〉 belongs to equivalence relation R , we say a ≡ b.

An equivalence relation induces a partition of its underlyingset. Speci�cally, a partition of a set S is a collection ofsubsets or blocks B1,B2, . . .Bn of S such that:

They cover the entire set, i.e. B1 ∪ B2 ∪ . . . ∪ Bn = S .They are pairwise disjoint, i.e. Bi ∩ Bj = ∅ for i 6= j .

For integers, = is an equivalence relation where each integerhas its own block in the corresponding partition.

For integers Z, any integer n and relation iRj wheneveri mod n = j mod n, R is an equivalence relation and thecorresponding partition of Z will have n blocks.

Lists, Stacks and Queues

Ordering

A binary relation is a partial order if it is antisymmetric andtransitive.

Elements a and b are comparable in a relation R , if eitheraRb or bRa.

If every pair of distinct elements in a partial order arecomparable, then the order is said to be a total order orlinear order. All total orders are also partial orders.

Lists, Stacks and Queues

Ordering Examples

For integers, < and ≤ are total orders.

⊆ is a partial order but not a total order. For set S = {a, b, c},{a} ⊆ {a, b}, but {a, b} is not comparable with {a, c}.Given set S = {a, b, c}, then both:

[∅, {a}, {b}, {c}, {a, b}, {a, c}, {b, c}, {a, b, c}]

[∅, {a}, {b}, {c}, {a, c}, {a, b}, {b, c}, {a, b, c}]

are partial orders of power set of S under ⊆.

Lists, Stacks and Queues

Exercise

Assume set Z of integers and relation aRb is true i� a − b is even.Is R an equivalence relation?Hints:

To show R is an equivalence relation on Z, you need to showthat it is re�exive, symmetric and transitive.

To show that R is not an equivalence relation, you simply needto provide a counter-example which violates any one of there�exive, symmetric and transitive properties.

0 is regarded as an even integer.

Lists, Stacks and Queues

Solution to Exercise

Assume a, b, c ∈ Z:

Since a − a = 0 which is even, the relation is re�exive.

If a − b is even, we must have b − a even. Hence the relationis symmetric.

If a − b is even and b − c is even, then we must havea = 2×m+ b and −c = 2× n− b for some integers m and n.Hence (a − c) = (2×m + b) + (2× n − b) = 2× (m + n)which is clearly even. Hence the relation is transitive.

Since R is re�exive, symmetric and transitive, it is an equivalencerelation.

Lists, Stacks and Queues

Exercise

Assume set Z of integers and relation aRb is true i� a × b is even.Is R an equivalence relation?

Lists, Stacks and Queues

Solution to Exercise

We have 1 ∈ Z, but 1× 1 is not even. In fact, for any odd n ∈ Z,n × n is odd. Hence the relation cannot be re�exive and hence it isnot an equivalence relation.

Lists, Stacks and Queues

Binary Search

Code in main.cc.

The search range where the key may occur is halved on eachiteration of the loop.

If array size is 16, the search ranges will have size 16, 8, 4, 2, 1.

If array size is n, the maximum number of times through theloop will be around dlg ne, where lg n denotes the base-2logarithm of n.

Inserting an element into the array remains the same: not aconstant time operation, but the search is more e�cient thana sequential scan.

Worth paying the cost of building up the sorted array if thereare many subsequent searches.

Lists, Stacks and Queues

Motivation for a Dictionary

Often have complex structures like a Payroll or Employeehaving many �elds.

Need to �nd structures which meet some criteria. Examples:

Find the employee having a particular employee ID. Use list ofemployees sorted by employee ID.Find all employees who make more than 100k / year. Use listof employees sorted by salary.

If we use separate lists for each criteria, we are duplicating allthe �elds of the complex structure.

Lists, Stacks and Queues

Dictionary

Solution is to keep a single copy of each complex structure.

Have separate data structures for each search criteria.

Each data structure consists of <Key, Value> pairs, whereKey are those �elds of the complex structure involved in thesearch criteria and Value is a pointer or reference to the entirecomplex structure.

Such data structures which maintain a mapping from Keys'sto Value's are referred to as a dictionary, map orassociative array.

Essential functionality: given a key, return corresponding valueor values.

Lists, Stacks and Queues