Teach a Level Computing 1 Data Structures 2018
Teach a Level Computing 1 Data Structures 2018
@MrEliotWilliams
Course Outline
1 Representations of data structures:
Arrays, tuples, Stacks, Queues,Lists
2 Recursive Algorithms
Data structures can be made from multiple data types and other data
structures…
Abstract vs Built in
• Abstract Data type
• A model of how a data is stored and what operations can be carried
out on the data
• Built in Type
• A predefined implementation of a data type in a programing language
Simple/Primitive data types
• Boolean
• Int
• Float
• Char (python doesn’t really have chars)
• mylist.remove("tin of beans")
• print(mylist)
Lists
• Lists are a complex and powerful data structure.
• Very easy to use in python
• Hard to understand how they work though so we will look at some simpler
data structures first….
Strings
• Built in data structure made up of?
• Properties of a string
• Mutable?
• Ordered?
• Fixed length?
Strings
• Built in data structure made up of?
• Properties of a string
• Immutable
• Ordered
• Fixed length
• Component parts all the same size…
• So not a list….
Arrays
• “Simple” data structure – built in many languages(not python)
• One variable that can contain multiple items of the same type that are held in
consecutive memory locations
0 1 2 3 4 5 6 7 8
Grade
20 80 50 60 70 55 92 60 85
• print (surname)
• Could be created as
• Names =[“Bob”,”Sally”,”James”]
• grades=[[20,23,19],[21,22,20],[19,23,20]]
Names Bob Sally James
Mark1 20 23 19
Mark2 21 22 20
Mark3 19 23 20
• grades=[[20,23,19],[21,22,20],[19,23,20]]
• grades=[[20,23,19],[21,22,20],[19,23,20]]
• grades=[[20,23,19],[21,22,20],[19,23,20]]
• grades[1][0]
Names Bob Sally James
Mark1 20 23 19
Mark2 21 22 20
Mark3 19 23 20
• grades=[[20,23,19],[21,22,20],[19,23,20]]
• How could we work out Bob’s total score?
• Score =grades[0][0]+grades[0][1]+grades[0][2]
but this might take a long time to type if there were lots of tests
Score=0
For i in range(0,3):
score=score + grades[0][i]
Names Bob Sally James
Mark1 20 23 19
Mark2 21 22 20
Mark3 19 23 20
• grades=[[20,23,19],[21,22,20],[19,23,20]]
2 stack operations
Push – puts data at the top of the stack
Top of
Stack
“Bob” “Dave” “James”
Pop – removes data from the top of the stack and returns it
Top of
James
Stack
“Bob” “Dave”
Stack Exercises
• Theory p2
• Practical P3
Python list commands….
• list.append()
• list.pop()
Queue
• Data enters a queue from the back
• Data leaves a queue from the front
Dave John
2 operations
• Enqueue
• Add an item into the queue
• Dequeue
• Remove the first item in the queue
• move all items up?
Queue Exercise (page 4)
• Write two procedures
• enqueue
• #put the item at the back of the queue
• # change back of queue value
• dequeue
• #return first value
• #update rest of queue..
•
• all errors should be appropriately handled
•
• Queue and backofqueue may be used as global variables in this assignment
•
• easy, medium and hard code skeletons are available
Priority Queue
• Some things are more important than others!
• Items should be dequeued in order of priority then queue position
• Several ways of doing this:
Priority Queue
• Some things are more important than others!
• Items should be dequeued in order of priority then queue position
• Several ways of doing this:
• Change enqueue to put things in the right place!
• Lots of data shuffling, inefficient in an array based structure
• Change dequeue to find the high priority items first
Priority Queue- Dequeue
queue=[("bob",1),("James",1),(“David”,2),(“John“,1),"","","","","",""]
backofqueue=2
dequeue():
• loop through the queue to find the highest priority and its position
• store the value at that position
• move all the items from that point up down one
• update and clear back of queue
• return stored answer
• All this shuffling data about is wasteful
• We could just move a pointer
Front Back
Back
Dave
• If head = tail then the queue is defined to be empty
• if head = tail + 1, or tail = max and head = 0, it is defined to be full.
Back Front
front back
front back
pointer = front
count = 0
while count < i:
pointer next of current entry
count = count + 1
return the value of current entry
Linked List Update
myList.update(idx, newValue)
pointer = front
count = 0
while count < idx:
pointer next of currentEntry
count = count = 1
currentEntry.value = newValue
Linked List Insert
myList.insert(idx, newValue)