# 4-twoup - Data Structures Before we can specify a data...

This preview shows pages 1–5. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Data Structures Before we can specify a data structure, we must first give a mathematical definition of what we want to represent. Defn: A stack is a finite sequence. An abstract data type (ADT) is a mathematical structure, together with a specification of operations on that structure. 1 Stack Specification, Part 1 Precondition: true . Postcondition: Constructs an empty Stack . Stack () Precondition: true . Postcondition: a is added to the end of the represented sequence. Stack.Push ( a ) 2 Stack Specification, Part 2 Precondition: The stack is nonempty. Postcondition: The last element of the represented sequence is removed and returned. Stack.Pop () Precondition: true . Postcondition: Returns true iff the represented sequence is empty. Stack.IsEmpty () 3 Implementing an ADT An implementation of an ADT consists of: a representation a set of variables used to store the data; an interpretation relates a given set of values for the representation variables to a specific instance of the formal definition; a structural invariant a predicate that must be satisfied between operations; and algorithms for the constructors and operations. The values of the representation variables, together with all values used by the interpretation and the structural invariant, comprise the state of the data structure. 4 A proof of correctness for an implementation consists of: Initialization: If the precondition holds at the beginning of a constructor invocation, then the postcondition holds upon completion of the constructor. If the constructor terminates normally, then the structural invariant holds after the data structure has been constructed, regardless of the truth of the precondition. Maintenance: If the structural invariant holds prior to the beginning of an operation, then it holds following completion of that operation. 5 Security: If the structural invariant holds, then the state can only be modified by invoking one of its operations. Termination: Each operation and constructor terminates. Correctness: If the structural invariant and the precondition hold prior to the beginning of an operation, then the postcondition holds following the completion of that operation. 6 Callbacks Suppose that during execution of x. Op1 , a function call is made while the structural invariant is false . Suppose that this function call causes x. Op2 to be called. The structural invariant for x is false when Op2 is called. To avoid this problem, we will assume that if a callback is attempted, a runtime error results. Our correctness proofs will rest on the implicit assumption that callbacks do not occur. 7 Computational Model Each variable stores a reference to a data item....
View Full Document

## This note was uploaded on 09/06/2009 for the course CIS 11274 taught by Professor Howell during the Spring '09 term at Kansas State University.

### Page1 / 26

4-twoup - Data Structures Before we can specify a data...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online