Software Development Techniques Topic 11 Array Data Structures





































- Slides: 37
Software Development Techniques Topic 11: Array Data Structures V 1. 0 © NCC Education Limited
Array Data Structures Topic 11 - 11. 2 Scope of Topic • Today, we finish off our discussion of software development techniques by discussing array-based data structures. – We will do this through the use of the objects we discussed in the last lecture. • There are many standard ways of representing certain kind of data access. – Designed to simplify your job as a developer. V 1. 0 © NCC Education Limited
Array Data Structures Topic 11 - 11. 3 Data Representation - 1 • Having the right representation of data makes a huge difference to how easy a program is to develop. – The right representation will make everything easier. – The wrong representation will make everything more difficult. • There are certain kinds of data access patterns that recur in software development. – We often have to manipulate data in a certain way. V 1. 0 © NCC Education Limited
Array Data Structures Topic 11 - 11. 4 Data Representation - 2 • Much as with algorithms, standard data structures exist to simplify these operations. – Rather than design our own data structure, we would implement one that has already been designed for us. • In this lecture, we will talk about several of these. – The List – The Stack – The Queue • We will also see how objects help us implement these. V 1. 0 © NCC Education Limited
Array Data Structures Topic 11 - 11. 5 Array Data Structures - 1 • All of these structures are based on the basic idea of an array. – The objects serve to wrap around an array and provide ways for it to be manipulated. • Most programming languages will give you versions of these structures that have already been coded. – It is important you understand how they work and why they exist. V 1. 0 © NCC Education Limited
Array Data Structures Topic 11 - 11. 6 Array Data Structures - 2 • A (perhaps surprising) example of an array data structure is the string. – Many languages represent a string as an array of single characters. – Syntax is usually simplified when dealing with these because of how often strings are manipulated in a program. • Strings though can be indexed like arrays, giving specific letters at specific locations. V 1. 0 © NCC Education Limited
Array Data Structures Topic 11 - 11. 7 The Wrapper • We use an object to implement the various things we need for a data structure. – It wraps around the data we have, permitting access via well defined functions. • Usually, we need the following at least: – – V 1. 0 A way to add elements A way to remove elements A way to count elements A way to see if structure is empty © NCC Education Limited
Array Data Structures Topic 11 - 11. 8 The Queue - 1 • The queue will be the first data structure we look at implementing. • It implements a First In First Out structure. – We do not put things in specific positions in the queue. – We add things to the end, we take things out from the front. • It does not matter especially how we implement it. – Our code should be a black box to anyone using it. • It only matters that it does what it is expected. V 1. 0 © NCC Education Limited
Array Data Structures Topic 11 - 11. 9 The Queue - 2 Class Whole. Number. Queue data contents as array (1000) of whole numbers data last. Entry as whole number Function add. To. Queue (needs num as whole number) contents[last. Entry] = num last. Entry = last. Entry + 1 End Function • We’re going to use a one thousand element array here. last. Entry indicates where the array has actual contents. • Adding to a queue adds to the end, which causes our last. Entry variable to increment. V 1. 0 © NCC Education Limited
Array Data Structures Topic 11 - 11. 10 The Queue - 3 Function remove. From. Queue () returns whole number data element as whole number data counter as whole number element = contents[0] counter = 1 loop while counter is less than last. Entry contents[counter-1] = contents[counter] next loop last. Entry = last. Entry - 1 return element End Function V 1. 0 © NCC Education Limited
Array Data Structures Topic 11 - 11. 11 The Queue - 4 Function count. Elements() returns whole number return last. Index End Function is. Empty() returns boolean data ret as boolean ret = call count. Elements() if ret is equal to 0 return true else return false end if end function V 1. 0 © NCC Education Limited
Array Data Structures Topic 11 - 11. 12 Using A Queue • Having written a data structure, if we ever need a First In First Out (FIFO) manipulation, we can use the queue. – Instantiate an object – Manipulate it as necessary using add and remove • Queues are useful in many situations. – They map precisely onto the idea of a pipeline. • A well written queue thus has value. V 1. 0 © NCC Education Limited
Array Data Structures Topic 11 - 11. 13 Generic Data Structures • Our pseudocode does not let us define a variable as just ‘some data’ – It has to be explicitly typed. • Many programming languages come with ways to manage this. – They are beyond the scope of this module to discuss. • When it comes time to create these in real code, there will be ways to make one object work for every kind of data. V 1. 0 © NCC Education Limited
Array Data Structures Topic 11 - 11. 14 The Stack - 1 • The stack is a LIFO structure – Last In, First Out • It is implemented using the same set of methods as a queue. – The only difference is what add and remove do. • Traditionally, stacks use a different terminology. – We ‘pop’ an element off of a stack. – We ‘push’ an element onto a stack. V 1. 0 © NCC Education Limited
Array Data Structures Topic 11 - 11. 15 The Stack - 2 Class Whole. Number. Stack data contents as array (1000) of whole numbers data last. Entry as whole number Function push (needs num as whole number) contents[last. Entry] = num last. Entry = last. Entry + 1 End Function pop () returns whole number last. Entry = last. Entry - 1 element = contents[last. Entry] return element End Function V 1. 0 © NCC Education Limited
Array Data Structures Topic 11 - 11. 16 The Stack - 3 • Stacks are used for many purposes in computers. – They are how function calls get handled in a real computer. – To let people ‘undo’ things that they have done – To let people navigate backwards through a series of operations • Like queues, they are a generically useful data structure and having a good stack simplifies much of the task of software development. V 1. 0 © NCC Education Limited
Array Data Structures Topic 11 - 11. 17 The List - 1 • One of the limits our arrays have is that they have a fixed size. – We have worked around that a bit with the resize function we have assumed to exist. • A list is an array that can grow in size as it is needed. – Using much of the mechanic that you can see in the stack and the queue. • Except we will cause it to resize if it gets too big. V 1. 0 © NCC Education Limited
Array Data Structures Topic 11 - 11. 18 The List - 2 Class Whole. Number. List data contents as array (1000) of whole numbers data last. Entry as whole number Function add (needs num as whole number) contents[last. Entry] = num last. Entry = last. Entry + 1 if last. Entry is greater than or equal to sizeof (contents) contents = resize (contents, sizeof (contents) * 2) contents = contents + 1 end if End Function get (needs index as whole number) returns whole number return contents[index] End Function V 1. 0 © NCC Education Limited
Array Data Structures Topic 11 - 11. 19 Data Structures • It would be much more convenient if, not knowing how many elements we need in an array, we could use an array that grows. – That is our List. • We can use it much the same way as an array. – We can add things to the end. • We could have an array that lets us add things in the middle. – We can pull out specific elements. • It just gets bigger as is needed. V 1. 0 © NCC Education Limited
Array Data Structures Topic 11 - 11. 20 Black Box Structures • There are hundreds of ways to implement this kind of functionality. – Some are specific to particular languages. • We use them as black box structures. – We do not care how they do what they do. – We just care that they do what they say they do. • Since they are objects, we can then use them in other programs. – Without needing to re-engineer our programs. V 1. 0 © NCC Education Limited
Array Data Structures Topic 11 - 11. 21 Recursion • Most implementations of these structures are a little more complex. – To be able to deal with multiple kinds of data and such. • Many of them make use of recursion to explore the internals. • We touched on this briefly in an earlier lecture. – However, it is often difficult to understand how it works. • Now that we have discussed a stack, we can see what drives it. V 1. 0 © NCC Education Limited
Array Data Structures Topic 11 - 11. 22 Recursion and the Stack • Recursion is an example of a process that makes use of a stack. – Each time we recurse into a function, we push the current values of all our variables onto a stack. – When we get to the final recursion, we start popping them back off the stack. • This is best explored with an example. – We will look at a recursive method for generating a factorial. V 1. 0 © NCC Education Limited
Array Data Structures Topic 11 - 11. 23 Factorials • You have seen this exercise a number of times, and the code is almost always quite ugly. function get. Factorial (needs num as whole number) returns whole number data total as whole number data counter as whole number loop while counter is less than or equal to num if counter is equal to 0 then total = counter else total = total * counter end if counter = counter + 1 next loop return counter end function V 1. 0 © NCC Education Limited
Array Data Structures Topic 11 - 11. 24 Factorial via Recursion • Recursion lets us represent the process much more elegantly: function get. Factorial (needs num as whole number) returns whole number if num is equal to 0 then return 1 else return num * get. Factorial (num-1) end if end function V 1. 0 © NCC Education Limited
Array Data Structures Topic 11 - 11. 25 How Does it Work? • Used with permission from http: //www. monkeys-atkeyboards. com/java 2/29. shtml V 1. 0 © NCC Education Limited
Array Data Structures Topic 11 - 11. 26 The Runtime Stack - 1 • In a simplified representation of computer memory, every line of code has an address. • Whenever we call a function, the computer will create what is known as an activation record. – It stores the value of parameters, the return value, and where in memory we should continue execution when we return. • Each of these records gets pushed onto the runtime stack. V 1. 0 © NCC Education Limited
Array Data Structures Topic 11 - 11. 27 The Runtime Stack - 2 • Used with permission from http: //www. monkeysat-keyboards. com/java 2/29. shtml V 1. 0 © NCC Education Limited
Array Data Structures Topic 11 - 11. 28 The Runtime Stack - 3 • As we can resolve the return value, we pop it off of the stack and return our execution to the appropriate memory location. – And we continue so on up the stack. V 1. 0 © NCC Education Limited
Array Data Structures Topic 11 - 11. 29 The Runtime Stack - 4 And so on, and so on. . . V 1. 0 © NCC Education Limited
Array Data Structures Topic 11 - 11. 30 Stacks, Queues and Lists • These data structures are used in many real world contexts. – They are not just an abstract exercise. • Spending some time developing your own, fully featured structure can save you a lot of time in future. – Because you can just use your ‘off the shelf’ solution. • What is most needed though is a way to handle generic data structures. V 1. 0 © NCC Education Limited
Array Data Structures Topic 11 - 11. 31 The Last Pseudocode Syntax • We are going to extend our pseudocode syntax to support generic data types. – We are going to add a new data type called mixed • It can hold anything. • However, this introduces a new problem. – How do we tell what kind of data is in a mixed type variable? • We will assume the existence of some functions for that. V 1. 0 © NCC Education Limited
Array Data Structures Topic 11 - 11. 32 Mixed and Reference • We will assume we have available a function called Type. Of – It will return a string that describes what is in the data. • We will also assume we have a function called typecast. – This will take our data and attempt to force it into the right kind of variable. • Most languages will have something for handling generic variables. V 1. 0 © NCC Education Limited
Array Data Structures Topic 11 - 11. 33 A Mixed List Class List data contents as array (1000) of mixed data last. Entry as whole number Function add (needs data as mixed) contents[last. Entry] = num last. Entry = last. Entry + 1 if last. Entry is greater than or equal to sizeof (contents) contents = resize (contents, sizeof (contents) * 2) end if End Function get (needs index as whole number) returns mixed return contents[index] End Function End Class V 1. 0 © NCC Education Limited
Array Data Structures Topic 11 - 11. 34 Using The Mixed Stack data data my. List as List counter as whole number sum as whole number tmp as whole number size as whole number call my. List->add (10) call my. List->add (20) call my. List->add (30) size = call my. List->count. Elements() loop while counter is less than size tmp = call Typecast (my. List->get (counter), "whole number") sum = sum + tmp counter = counter + 1 next loop output "Sum is " output sum V 1. 0 © NCC Education Limited
Array Data Structures Topic 11 - 11. 35 Conclusion • Good data structures simplify our work as developers – And some standard data structures exist that we need to understand. • Recursion is implemented using one of these data structures. – And is often used itself to manipulate complex data structures. • Data structures are most useful when they are generic. V 1. 0 © NCC Education Limited
Array Data Structures Topic 11 - 11. 36 Terminology • Queue – A first in, first out (FIFO) data structure. • Stack – A last in, first out (LIFO) data structure • List – An array that grows as needed • Typecasting – Converting a general case variable into a specific type V 1. 0 © NCC Education Limited
Array Data Structures Topic 11 - 11. 37 Topic 11 – Array Data Structures Any Questions? V 1. 0 © NCC Education Limited