Download Data Structure - Abstract Data Types - 2 - Notes and more Study notes Data Structures and Algorithms in PDF only on Docsity!
Abstract Data Types
- (^) Basic rule concerning programming : No routine should ever exceed a page.
- (^) This is accomplished by breaking the program down into modules.
- (^) Each module is a logical unit and does a specific job.
Abstract Data Types
Advantages of Modularity
- (^) It is much easier to debug small routines than large routines.
- (^) It is easier for several people to work on a modular program simultaneously.
- (^) A well-written modular program places certain dependencies in only one routine, making changes easier
Abstract Data Types
- (^) Implementation of these operations is written once in the program,
- (^) Any other part of the program that needs to perform an operation on the ADT can do so by calling the appropriate function In essence ADT’s provide a form of true 'modularity', in which the operations provide:
- (^) Low data coupling
- (^) High functional cohesion
- (^) Specific public interfaces
Cohesion and Coupling
Low Data Coupling
- (^) Coupling is the extent to which different operations rely on one another to perform their tasks.
- (^) Low coupling, allows us to reuse our operations with the minimum of changes within the code that carries out the operation.
- (^) Thus an add operation should only require the data type and the item to add for minimum coupling.
- (^) The simplest connections are those which pass a small number of parameters in the clearest way possible.
ADT Characteristics
ADT interface is a specification of the values and operations that has two specific properties:
- (^) It specifies everything you need to know about the data type
- (^) It makes no reference to the manner or the computer language in which the data type will be implemented. Using ADTs splits our program into two distinct areas or pieces:
- (^) The application that uses the data type
- (^) The implementation that codes the actual data type.
ADT Characteristics
As the following diagram shows these two areas are completely independent. It is possible to take the implementation developed for one application and use it in another:
Pre-Conditions and Post-Conditions
- (^) Pre-condition - a condition that should be true, or conditions that should be true, when the operation is called. The operation is not guaranteed to perform as it should unless the pre-conditions have been met.
- (^) Post-condition - condition that will be true when the operation has completed its task. If the operation is correct and the pre-condition(s) met, then the post-condition is guaranteed to be true.
Stack ADT Specification
- (^) A stack is either empty or it consists of two parts, a top element and stack (the remaining elements).
- (^) Stack is the F irst- I n- L ast- O ut type data structure. The element that has entered last in stack is supposed to be at the Top of the stack.
- (^) The elements in a stack may be of any type, but all the elements in a given stack must be the same type
Stack ADT Specification
- (^) Pop(element) Pre-conditions: stack is not empty Post-conditions: the top element has been removed from the stack Inputs: the stack Outputs: the changed stack, ie top element, has been removed Algorithm: remove the top element of the stack; give a copy of the element back to the user
- (^) Push(element) Pre-conditions: the stack exists, element is of appropriate type Post-conditions: Element is put onto the top of the stack. Inputs: a stack and an element Outputs: a changed stack Algorithm: insert the element into the stack
Stack ADT Specification
- (^) IsEmpty() Pre-conditions: a stack exists Post-conditions: returns true if empty false otherwise. Inputs: a stack Outputs: Boolean value Algorithm: check initial status of the stack and report whether empty or not.
- (^) Size() - returns the number of items in the stack
- (^) Display() - Gives a picture of the stack contents on the screen
Push(element)
Pre-conditions: The stack exists, and element is of appropriate type Post-conditions: Element is put onto the top of the stack Inputs: A stack and an element Outputs: A changed stack Algorithm: Insert the element into the stack Here we are adding an element to the data structure. We need to conform to our ADT definition so the element will be inserted at the top of the structure.
Pop(element)
Pre-conditions: Stack is not empty Post-conditions: The top element has been removed from the stack Inputs: The stack Outputs: The changed stack, ie top element, has been removed Algorithm: Remove the top element of the stack; give a copy of the element back to the user Here we are removing the last element that we added to the stack.
IsEmpty()
Pre-conditions: A stack exists Post-conditions: returns true if empty, false otherwise Inputs: a stack Outputs: boolean value In order to check if the stack is empty we need to look at the value held in the Top variable. If this is NULL then the stack is empty. If it is any other value then the stack cannot be empty. Note : It is important that we initialize the value of Top when we create the stack, otherwise we may have a value in here even though the stack is technically empty.
Size()
- (^) This is not one of the primitive operations, but is generally useful for programming. This operation will return a count of the number of elements in the stack structure.
- (^) In order to perform this we need to start at the Top of the structure, move through each node and increase our count until we reach the end of the Stack.
- (^) This operation has to be non-destructive, ie we cannot change any elements or affect the structure in any way.