Docsity

Data Abstraction and Records in Computing and Algorithms, Study notes of Computer Science

Partial preview of the text

Download Data Abstraction and Records in Computing and Algorithms and more Study notes Computer Science in PDF only on Docsity!

Introduction to Computing

and Algorithms

Data Abstraction Part One

Chapter 5

Data Abstraction

The Most Fundamental Technique:

  • Descriptive Identifiers that are brief and informative ( not cyptic or cute )

Recall what we mean by abstraction :

  • Separating the essence from the particular instance or implementation.

For example:

  • For the purpose of represenitng an employee’s ID number , which one features the highest data abstraction? this_num isofype Num some_dude’s_num isoftype Num employee_ID_num isof type Num

Data Abstraction

Party definesa Record house_number isoftype Num time_starts isoftype Num time_ends isoftype Num endrecord // Party

halloween_party, new_years_party isoftype Party

Rather than a collection of 6 variables, we have:

  • 2 variables with 3 fields each.
  • both are identical in structure

Creating records is a powerful tool

Creating New Data Types

Party definesa Record house_number isoftype Num time_starts isoftype Num time_ends isoftype Num endrecord halloween_party, new_years_party isoftype Party

  • Changes to the type declaration cascade to all var’s of that data type Party definesa Record house_number isoftype Num time_starts isoftype Num time_ends isoftype Num police_came isoftype Boolean endrecord

We may have unlimited number of identical variables by declaring a type

Using Records :

Task:

Create a database to keep track of the kids in an elementary school

To make the example simple, we will only store:

  • Student number
  • Age
  • Grade in math this year

Creating New Data Types

Creating and Using Records :

Step 2: Declare Record variables of this new type:

mary, zach, herbert isoftype Kid_Record best_grade isoftype Char

Step 1: Define the new type :

Kid_Record definesa Record student_num isoftype Num age isoftype Num grade isoftype Char endrecord

Creating New Data Types

Types vs Variables

VARIABLE Declarations

  • Actually creates storage space
  • Have limited scope - only module containing the variable can “see” it
  • Must be based on an existing data type:
    • built-in: atomics (like Num) or String
    • user-defined (you invent the data type)
    • user-defined types must be defined before variables of that type can be declared

TYPE Definitions

  • Are used to create templates for new kinds of variables
  • Do not actually create a variable - no storage space is allocated
  • Have UNLIMITED scope

Anonymous Data Types

This_Record isoftype Record silly isoftype Num stupid isoftype Char endrecord // This_Record

  • Some languages allow this - Removes benefits of data typing...
  • Is in general bad ; do not do this even if a language allows it
  • In our pseudocode, you cannot

Anonymous data types combine the declaration of a complex variable with a data type definition. Thus, the data type itself has no name.

Combining Abstractions

  • Often, data abstractions (for example, records) need corresponding procedural abstractions (modules) as helpers
  • Common examples include: - Modifier modules: procedures used to read in or update data values - Accessor modules: procedures used to print out information in a record, or functions which return data values to other modules
  • We will revisit this idea when we talk about behavioral abstractions later in the course

For example :

Student_Rec definesa Record name isoftype String ssn isoftype Num num_hours isoftype Num endrecord

procedure Print_Student (somebody isoftype in Student_Record) // An accessor module which prints out all // data values stored in a Student_Rec print(“Name: ”, somebody.name) print( “SSN: ”, somebody.ssn ) print( “Hours:”, somebody.hours )

endprocedure

Combining Abstractions

Static vs. Dynamic Data

Records are often used as we have used them so far.

  • All we’ve seen so far is static data.
  • Static variables are those that we declare “ahead of time.”
  • They are declared in a module (or in the main algoriythm) and “live” for as long as that module is active.
  • If we declare more static variables than we need, we waste space.
  • If we declare fewer static variables than we need, we are out of luck.
  • Often, real world problems mean that we don’t know how many variables to declare, as the number needed will change over time.
  • For example: a list of employees

Static vs. Dynamic Data

Records can also be used (with pointers) to create and use dynamic data.

  • Dynamic data refers to data structures which can grow and shrink to fit changing data requirements.
  • We can allocate (create) additional dynamic varibles whenever we need them.
  • We can deallocate (kill) dynamic variables whenever we are done with them.
  • With pointer variables to connect them, we can use dynamic data structures to create a chain of data structures called a linked list.
  • A key advantage of dynamic data is that we can always have a linked list that consists of exactly the number of vars required, no more, no less...

Introducing the Heap

Main this_var 4 that_var 7 this_list

12 18 21 23

  • The heap is memory not used by the stack
  • As stack grows, heap shrinks
  • Static variables live in the stack
  • Dynamic variables live in the heap

The

heap

The

stack

Dynamic Data

  • We have three static variables: two Num variables, plus a Pointer variable which points to a dynamic list (known as a linked list
  • The linked list has (for now) 4 nodes
  • The pointer this_list points to the 1st node
  • Each node points to the next node
  • The last node’s pointer is NIL

(^12 18 )

Main this_var 4 that_var 7 this_list

23