Cover page images (keyboard)

Abstract Data Types

Sue Evans



Press space bar for next page

Learning Outcomes

Abstract data types

The ADTs we've worked with so far are :

all of which are built-in data types of Python.

ADT - Example

Abstract data types do not have to be large data collections, as the ones are that we've worked with so far.

Whenever we define an ADT, we need to give a definition and a list of operations.

Fractions

A fraction consists of a numerator and denominator and is used when discussing parts of a whole.

Example: The fraction 1/2 means that if an object is divided into two equal parts, then we are working with one of those parts.

The 1 is known as the numerator and the 2 is known as the denominator. Both the numerator and denominator of a fraction are integers. The denominator cannot be 0.

When working with fractions, we are sometimes interested in a whole number as well, as in 5/4 = 1 1/4. Numbers expressed as fractions can have a whole number part as well as a fractional part.

The operations on fractions include :

Since we have just defined a mathematical object (the fraction) and the operations on that object, we can say that a fraction is an abstract data type.

Notice that we haven't discussed the implementation of any of our ADTs

ADTs typically can be implemented in many different ways, and often are implemented differently when using different languages.

Complete the Operations

In order to work with your new type, you will probably have to write functions that will allow you to :

For instance, to print a fraction you would need:

    print '%d %d/%d' % (whole, numerator, denominator)

When writing the code to implement your new type, you should try to give it all of the functionality you can foresee. For instance, if you are writing a program using fractions, and it is only necessary for this program to be able to add and subtract fractions, you should also implement multiplication and division, to produce a fully-usable ADT.

The Queue

Background

A queue is what Americans commonly refer to as a line. The example is that you get into a line at the grocery store. In Great Britain, lines, like the line of customers waiting at the checkout, are called queues. The word queue is more specific. The word line can also mean, a line between two points, the line down the middle of the road, etc.

People have been exposed to the idea of getting in line, or "queueing up", as the Brits say, since their mothers took them to the bank or the grocery store in their strollers. So we all know the rules of a queue.

A queue is a linear collection, where something to be added to the queue must be placed at the end of the queue. Items are removed from the queue from the front. To keep with the analogy, when you walk into the bank you go to the end of the queue. When the teller is ready to help the next person, the person at the beginning of the queue is removed from the queue. Each person then moves one position closer to the beginning of the queue. Eventually, you will be the first person in the queue and the teller will call you up causing you to be removed from the queue.

A queue is refered to as a FIFO data structure: First In, First Out.

The Queue ADT

The definition

A queue is a linear collection of items, where an item to be added to the queue must be placed at the end of the queue and items that are removed from the queue must be removed from the front. The end of the queue is known as the tail and the front of the queue is known as the front. The term enqueue means to add an item to the queue, and the term dequeue means to remove an item from the queue.

The operations

There are actually very few operations on a queue.

A queue is NOT a built-in data type in Python, however you could write these functions that make use of Python's list methods.

The Stack

Background

A stack is a little harder to define than a queue, since there isn't a common term that will relate how a stack works. It is definitely a linear collection of similar items. Stacks have access rules too, but the rules are different than the rules for queues.

You have been exposed to the idea of a stack before. When you go to a cafeteria to get lunch you take a tray from the stack of trays. You must always take the tray that is on the top of the stack. The cafeteria personnel put clean trays on the stack. The clean trays are always added to the top of the stack, so the tray that's on top is the last one that was added.

A stack is refered to as a LIFO data structure: Last In, First Out.

The Stack ADT

The definition

A stack is a linear collection of similar items, where an item to be added to the stack must be placed on top of the stack and items that are removed from the stack must be removed from the top. The top of the stack is known as the top. The term push means to add an item to the stack, and the term pop means to remove an item from the stack.

The operations

There are actually very few operations on a stack.

A stack is NOT a built-in data type in Python, however you could write these functions that make use of python's list methods.

Queues, Stacks, & Priority Queues

Use cases:

What data structures would you use for these programs?

Using a stack

def pop(items):

    size = len(items)
    popped = items[size - 1]
    del(items[size - 1])

    return popped

    
def main():
    cars = []
    cars.append('Audi')
    print 'pushing Audi'
    cars.append('Buick')
    print 'pushing Buick'
    cars.append('Chevy')
    print 'pushing Chevy'
    cars.append('Dodge')
    print 'pushing Dodge'
    print
    
    while len(cars) > 0:
        print 'popping', pop(cars)

main()

Let's run it!

ite207-pc-01.cs.umbc.edu[138] python pop.py
pushing Audi
pushing Buick
pushing Chevy
pushing Dodge

popping Dodge
popping Chevy
popping Buick
popping Audi
ite207-pc-01.cs.umbc.edu[139]