Download presentation
Presentation is loading. Please wait.
1
COMPSCI 107 Computer Science Fundamentals
Lecture 13 – ADTs and Stacks
2
Lab 4 this week PeerWise questions Remember to keep in mind:
Your question must be very clearly worded You should provide a useful, detailed explanation You may also improve explanations that are missing or inadequate Avoid “trick” questions!
3
Test Thursday 13th April Content Format During normal lecture time
All material covered up until end of lectures this week Labs 01 – 05 Format Short answer questions Code writing Code reading Order analysis
4
Test Structure Assigned seats in groups of 4
Used for discussion phase and bonus calculation Test1: Determines marks for the test 20 minutes Discussion phase in assigned groups 10 minutes Test2: Determines possible bonus marks Possible bonus marks: (group average for Test1) – (group average for Test2)
5
Abstraction A fundamental idea
6
Abstraction A fundamental idea
7
Abstraction Where have you seen “abstraction” so far?
8
count = count + 1 Abstraction
Where have you seen “abstraction” so far? count = count + 1
9
Abstraction Where have you seen “abstraction” so far? CPU instructions
Process of addition mov eax 0x33fa2c mov ebx 1 add eax ebx mov 0x33fa2c eax Move data from memory address to register ‘eax’ Move value 1 to register ‘ebx’ Add values in registers and store result in ‘eax’ Store the result of the addition back to memory The main memory (the "RAM") in personal computers is dynamic RAM (DRAM). It is the RAM in desktops, laptops and workstation computers as well as some of the RAM of video game consoles.
10
(dynamic random access memory)
Abstraction Where have you seen “abstraction” so far? Addressable data (bytes made up of bits) 0x33fa2c The main memory (the "RAM") in personal computers is dynamic RAM (DRAM). It is the RAM in desktops, laptops and workstation computers as well as some of the RAM of video game consoles. “main memory” RAM chip (dynamic random access memory)
11
Abstraction Where have you seen “abstraction” so far? A memory cell
(dynamic random access memory) transistor (controls access) A transistor and a capacitor are paired to create a memory cell, which represents a single bit of data. The capacitor holds the bit of information -- a 0 or a 1 (see How Bits and Bytes Work for information on bits). The transistor acts as a switch that lets the control circuitry on the memory chip read the capacitor or change its state. capacitor (stores charge) Source:
12
count = count + 1 Abstraction
Where have you seen “abstraction” so far? relevant, simpler ideas count = count + 1
13
count = count + 1 Abstraction
Where have you seen “abstraction” so far? relevant, simpler ideas count = count + 1 Abstraction complex details
14
Abstraction Other kinds of abstraction that you are familiar with
Procedural abstraction
15
a function to test if n is prime
Abstraction Other kinds of abstraction that you are familiar with Procedural abstraction A function definition encapsulates a set of program statements. The function name then becomes an abstraction that we can use to help simplify the logic of our program. Abstraction a function to test if n is prime
16
Abstraction Other kinds of abstraction that you are familiar with
Data abstraction We can encapsulate related data items into a single type of object
17
a Person object has a name and an age
Abstraction Other kinds of abstraction that you are familiar with Data abstraction A class definition encapsulates a set of data values along with a set of methods that operate on those values. The class name then becomes an abstraction that allows us to work with objects that simplify the logic of our program. Abstraction a Person object has a name and an age
18
Abstract data types An abstract data type (ADT) is a specification of:
a set of data and the set of operations that can be performed on the data. Such a data type is abstract in the sense that it is independent of various concrete implementations
19
Abstract data types An abstract data type (ADT) is a specification of:
a set of data and the set of operations that can be performed on the data. Such a data type is abstract in the sense that it is independent of various concrete implementations Request to perform ADT operation Program Result of ADT operation ADT operations
20
Abstract data types An abstract data type (ADT) is a specification of:
a set of data and the set of operations that can be performed on the data. Such a data type is abstract in the sense that it is independent of various concrete implementations Request to perform ADT operation Data structure implementing the ADT interface Program Result of ADT operation ADT operations
21
Abstract data types An abstract data type (ADT) is a specification of:
a set of data and the set of operations that can be performed on the data. Such a data type is abstract in the sense that it is independent of various concrete implementations Data structure implementing the ADT interface Request to perform ADT operation Data structure implementing the ADT interface Program Result of ADT operation A KEY IDEA: We can easily replace one implementation with another (better) one, if the program only accesses the agreed ADT operations ADT operations
22
An example – balanced brackets
Consider the following pairs of brackets: () <> [] {}
23
An example – balanced brackets
Consider the following pairs of brackets We can have expressions (consisting of arbitrary letters and brackets) where the brackets are perfectly balanced, like this: ([x{y}z<>]) brackets are balanced when read from left to right
24
An example – balanced brackets
Consider the following pairs of brackets We can have expressions (consisting of arbitrary letters and brackets) where the brackets are perfectly balanced However we can also have expressions where the brackets are not balanced, like this: ([x{y<z}>])
25
An example – balanced brackets
Are the brackets in this expression balanced or not? <<j{g{h{{[h[a{[j](a{q(<<[(){t}[[](j<p<a><s{(t{d<j[[c({x<y>({<<y<>{({}){f}<>(j)}{}[b<k>]>[x]>}){}})]]>})}>>)]]>>)})}]]}}}}>> These are balanced
26
What is a “stack”? We are going to cover:
The “Stack” Abstract Data Type Two different implementations of the Stack ADT We will then use a stack to solve our balanced brackets problem
27
What is a “stack”? A stack is an ordered collection of items where the addition of new items and the removal of existing items always takes place at the same end, referred to as the top of the stack. i.e. add new elements to the top, remove existing elements from the top Last-in, first-out (LIFO) property The last item placed on the stack will be the first item removed Example: A stack of dishes in a cafeteria
28
Last In - First Out (LIFO)
What is a “stack”? We add only to the top of a stack (called a “push”) We remove only from the top of the stack (called a “pop”) Add a new element push “103” Remove top element pop Remove top element pop 34 57 12 34 57 12 103 34 57 12 34 57 Last In - First Out (LIFO)
29
The Stack ADT The “Stack” ADT Data: Operations:
an ordered collection of elements Operations: create a new empty stack (Stack()) determine whether a stack is empty (is_empty()) add a new element to the stack (push) remove the most recently added element from the stack (pop) look at (but don’t remove) the most recently added element (peek) determine the size of a stack (how many elements) (size)
30
The Stack ADT An example s = Stack() print(s.is_empty()) s.push(5)
s.push('cat') print(s.peek()) s.push(True) print(s.size()) s.push(8.4) print(s.pop())
31
Exercise Show the contents of the stack, and the output produced.
s = Stack() print(s.is_empty()) s.push(5) s.push('cat') print(s.peek()) s.push(True) print(s.size()) s.push(8.4) print(s.pop())
32
The Stack ADT An example True cat 3 False 8.4 2 s = Stack()
print(s.is_empty()) s.push(5) s.push('cat') print(s.peek()) s.push(True) print(s.size()) s.push(8.4) print(s.pop()) True cat 3 False 8.4 2
33
Exercise OK, so how can we actually implement the Stack ADT?
What data structure would be appropriate?
34
Stack Implementation OK, so how can we actually implement the Stack ADT? What data structure would be appropriate? We will use a Python list Remember: the addition of new items and the removal of existing items always takes place at the same end, referred to as the top of the stack But which end of the Python list is better for our Stack implementation? Python List
35
Stack Implementation Version 1: the beginning of the list will hold the top element of the stack push(…) pop() class Stack: ... def push(self, item): self.items.insert(0,item) def pop(self): return self.items.pop(0) What is the Big-O of the push()/pop() here?
36
Stack Implementation Version 2: the end of the list will hold the top element of the stack class Stack: ... def push(self, item): self.items.append(item) def pop(self): return self.items.pop() push(…) pop() What is the Big-O of the push()/pop() here?
37
Balanced brackets Back to our example of balanced brackets
Ensure that pairs of brackets are properly matched: e.g. Possible errors: {a,(b+f[4])*3,d+f[5]} (..)..) // too many closing brackets (..(..) // too many open brackets [..(..]..) // mismatched brackets
38
Balanced brackets initialise the stack to empty for every char read
Algorithm: initialise the stack to empty for every char read if it is an open bracket then push onto stack if it is a close bracket, then if the stack is empty, return ERROR pop from the stack if they don’t match then return ERROR if it is a non-bracket, skip the character if the stack is NON-EMPTY, ERROR
39
Balanced brackets Example: {a,(b+f[4])*3,d+f[5]} top { top ( { top [ (
40
[ { ( ) ] Balanced brackets Example: pop: ] and { Not matched!! top [
41
Exercise Implement the Stack ADT using a Python list, such that all operations are O(1). class Stack: def __init__(self): def is_empty(self): def push(self, item): def pop(self): def peek(self): def size(self):
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.