1 Problem Solving Abstraction Oftentimes, different real-world problems can be modeled using the same underlying idea Examples: Runtime storage, Undo operation.
Published byModified over 6 years ago
Presentation on theme: "1 Problem Solving Abstraction Oftentimes, different real-world problems can be modeled using the same underlying idea Examples: Runtime storage, Undo operation."— Presentation transcript:
1 Problem Solving Abstraction Oftentimes, different real-world problems can be modeled using the same underlying idea Examples: Runtime storage, Undo operation. In both cases we have a sequence of data items (activation records, actions) and we can only add and remove items from one end of the sequence. We can design a data organization scheme that retains the general characteristics of this "data and operations" model (without any dependencies on the type of data or method of implementation). This is called data abstraction. Big advantage: code reuse. We can use the same scheme for both problems.
2 Problem Solving Encapsulation The way the data is organized and the operations that can be performed on it are implemented should be hidden. Data can be manipulated in a controlled way, only through an interface. The internal details are hidden. This is called encapsulation. Big advantage: The code has higher maintainability. The internal organization/implementation can be modified/improved without changing the interface. Big advantage: Outside objects cannot interfere with the internal organization, inadvertently corrupting it.
3 Problem Solving Information hiding A solution typically consists of different modules that interact with one another. Information hiding is the idea of concealing details from other modules that do not need to know those details.
4 Data organization Major components: The data The operations allowed on it These make up an Abstract Data Type A Data Structure is a construct that implements a particular ADT. Example: An indexed-list ADT.
5 The List ADT Data: a collection of homogeneous elements arranged in a sequence Operations: Insert Delete Find Update Retrieve Length
6 The List ADT: Operations Retrieve Which element? Specify an index, OR Use a pointer to the current location What additional operations will be needed in this case? Insert Where? At some index, OR Before /after current location What complications arise in this case? We'll have problems inserting at the tail/head of the list. Possible solution: a dummy node. BUT, we must be careful not to allow data retrieval at that node.
7 The List Data Structure Implementation 1 : Contiguous memory Use a dynamic array How is each operation implemented? How efficient is each operation? Random access capability good for retrieval when we use index rather than current pointer. Important: the list ADT does NOT provide random access. We will need to shift elements every time we insert or delete. In addition, if the array fills up, we need to reallocate a bigger chunk of memory. We will need to reallocate when it fills up.
8 The List Data Structure Implementation 2 : Linked memory Use a node structure to store the data and a pointer to the next node, to create a chain of nodes. Uses more space than the array (due to the pointers) but insert/delete do not require shifting However, deleting requires us to traverse the whole list in order to access the predecessor of the current node. Trick solution (works for deleting any node but the last): move the next node's contents into the one to be deleted and then physically remove the next node. This maintains the correct abstract picture of the structure. We can use a similar trick for the insert operation. See ~b11/labs/Templates for an example.