Topic: 4.1.3 Abstract Data Types (ADT) : Chapter: 4.1 Computational Thinking and Problem-Solving
Topic: 4.1.3 Abstract Data Types (ADT) : Chapter: 4.1 Computational Thinking and Problem-Solving
In the last section, you were introduced to several kinds of ADTs and the functions that can be performed
on them. In general, ADTs are usually constructed using a built in data type. The most commonly reused
data type is the ARRAY which can be used for stacks, queues, linked list, trees, etc…
Stack:
U
A stack is an ADT that might involve a dynamic or static implementation. A stack is a last-in-first-out
(LIFO) or first-in-last-out (FILO) ADT. Implementations should include two operations, pushing and
popping, and a pointer to the top of the stack.
A real life example is a stack of books you might have on your desk:
In this example we keep adding (pushing) books to the stack. If we want to get at the bottom book about
Cars, we must first remove (pop) all the books above it. Hence First In Last Out (FILO)
1. Starting from the head pointer, compare the item with the item you want.
2. If the items match, print “Match found at index (N)” and stop the searching.
3. Else if the items do not match, move the head pointer one index down then check that index.
4. Repeat steps 1 to 3 until you reach the end of the stack or until you find the data.
5. If you reach the end of the stack with no match found, print “No match found”
Page 1 of 6
Computer Science 9608 (Notes)
Chapter: 4.1 Computational thinking and
problem-solving
Queue:
U
A queue is a first-in first-out (FIFO) abstract data type that is heavily used in computing. Uses for queues
involve anything where you want things to happen in the order that they were called, but where the
computer can't keep up to speed. For example:
Printer Queue - you want print jobs to complete in the order you sent them, i.e. page 1, page 2, page 3,
page 4 etc. When you are sharing a printer several people may send a print job to the printer and the
printer can't print things instantly, so you have to wait a little while, but the items output will be in the
order you sent them
Disadvantage: Uses up large amounts of memory, might overwrite important memory locations.
Starting from the head pointer, compare the item at that index with the item you want.
1. If the items match, print “Match found at index (N)” and stop the searching.
2. Else if the items do not match, move the head pointer one index down and check the data at that
index.
3. Repeat steps 1 to 3 until you reach the end of the queue or until you find the data.
4. (The end of the queue will actually be when you keep decrementing the head pointer until it
reaches the same level as the tail pointer!!!)
5. If you reach the end of the queue (head pointer = tail pointer) with no match found, print “No match
found” and end the searching.
For more details about the queue, and for algorithms to insert and delete an item in a queue, refer to
chapter 4.1.2.
Page 2 of 6
Computer Science 9608 (Notes)
Chapter: 4.1 Computational thinking and
problem-solving
It is a dynamic abstract data type that uses pointers to vary memory used at run time.
A common question asked is why do we use linked lists when we can just use arrays?
There are a number of benefits and drawbacks of using a linked list over an array.
1. The memory used by a linked list can vary at run time, meaning that memory isn’t a fixed size
(unlike the array)
2. In an array, memory spaces are continuous (even on the hard drive). So when you want to initialize
an array, there has to be enough continuous free spaces on your hard-drive, and if not, then you
will have to reduce the size of your array. Linked lists help tackle this problem, one part of the list
can be on one end of the hard-drive and another part of the list on the other end of the hard-drive,
the linked list will simply link both lists and display it as if the data were continuous.
3. Linked lists, can keep a track of non-continuous data, but this will slow down the access to the
data. Arrays have very fast access to data.
For more details about the linked list, and for algorithms to insert, delete, and search for items in a linked
list, refer to chapter 4.1.2 pages 8, 9, and 10.
Binary Tree:
Dictionary:
A dictionary is an abstract data type composed of a collection of (key, value) pairs, such that each
possible key appears just once in the collection.
A standard solution to the dictionary problem is a hash table. In some cases, it is also possible to solve
the problem using binary search trees.
Dictionaries have many applications including many programming patterns such as memorization and the
decorator pattern.
Page 3 of 6
Computer Science 9608 (Notes)
Chapter: 4.1 Computational thinking and
problem-solving
Suppose that the set of loans made by a library is to be represented in a data structure. Each book in a
library may be checked out only by a single library patron at a time. However, a single patron may be able
to check out multiple books. Therefore, the information about which books are checked out to which
patrons may be represented by a dictionary in which the books are the keys and the patrons are the
values.
For instance, in Python programming language, the current checkouts may be represented by a dictionary.
A lookup operation with the key “Great Expectations” in this array would return the name of the person
who checked out that book, John. If John returns his book, that would cause a deletion operation in that
dictionary, and if Pat checks out another book, which would cause an insertion operation, leading to a
different state:
In this new state, the same lookup as before, with the key “Great Expectation”, would raise an exception
because this key is no longer present in the array.
find(k): if the dictionary has an entry with the key k, return it, else, return null.
findAll(k): returns an iterator of all entries with key k.
insert(k, o): inserts and returns the entry (k, O).
remove(e): remove the entry e from the dictionary.
size(): return the size of the dictionary
isEmpty(): if the dictionary is empty, then this Boolean data type will return true value.
Page 4 of 6
Computer Science 9608 (Notes)
Chapter: 4.1 Computational thinking and
problem-solving
Page 5 of 6
Computer Science 9608 (Notes)
Chapter: 4.1 Computational thinking and
problem-solving
Page 6 of 6