next up previous
Next: About this document Up: My Home Page

Data Structures and Programming
Lecture 1

Steven S. Skiena

Why Data Structures?

In my opinion, there are only three important ideas which must be mastered to write interesting programs.

At this point, I expect that you have mastered about 1.5 of these 3.

It is the purpose of Computer Science II to finish the job.

Data types vs. Data Structures

A data type is a well-defined collection of data with a well-defined set of operations on it.

A data structure is an actual implementation of a particular abstract data type.

Example: The abstract data type Set has the operations EmptySet(S), Insert(x,S), Delete(x,S), Intersection(S1,S2), Union(S1,S2), MemberQ(x,S), EqualQ(S1,S2), SubsetQ(S1,S2).

This semester, we will learn to implement such abstract data types by building data structures from arrays, linked lists, etc.

Modula-3 Programming

Control Structures: IF-THEN-ELSE, CASE-OF

Iteration Constructs: REPEAT-UNTIL (at least once), WHILE-DO (at least 0), FOR-DO (exactly n times).

Elementary Data Types: INTEGER, REAL, BOOLEAN, CHAR

Enumerated Types: COINSIDE = {HEADS, TAIL, SIDE}

Operations: +, -, <, >, #

Elementary Data Structures

ArraysThese let you access lots of data fast. (good)

You can have arrays of any other data type. (good)

However, you cannot make arrays bigger if your program decides it needs more space. (bad)

RecordsThese let you organize non-homogeneous data into logical packages to keep everything together. (good)

These packages do not include operations, just data fields (bad, which is why we need objects)

Records do not help you process distinct items in loops (bad, which is why arrays of records are used)

SetsThese let you represent subsets of a set with such operations as intersection, union, and equivalence. (good)

Built-in sets are limited to a certain small size. (bad, but we can build our own set data type out of arrays to solve this problem if necessary)

Subroutines

Subprograms allow us to break programs into units of reasonable size and complexity, allowing us to organize and manage even very long programs.

This semester, you will first encounter programs big enough that modularization will be necessary for survival.

Functions are subroutines which return values, instead of communicating by parameters.

Abstract data types have each operation defined by a subroutine.

Subroutines which call themselves are recursive. Recursion provides a very powerful way to solve problems which takes some getting used to.

Such standard data structures as linked lists and trees are inherently recursive data structures.

Parameter Passing

There are two mechanisms for passing data to a subprogram, depending upon whether the subprogram has the power to alter the data it is given.

In pass by value, a copy of the data is passed to the subroutine, so that no matter what happens to the copy the original is unaffected.

In pass by reference, the variable argument is renamed, not copied. Thus any changes within the subroutine effect the original data. These are the VAR parameters.

Example: suppose the subroutine is declared Push(VAR s:stack, e:integer) and called with Push(t,x). Any changes with Push to e have no effect on x, but changes to s effect t.

Generic Modula-3 Program I

MODULE Prim EXPORTS Main;        
(* Prime number testing with Repeat *)
 
  IMPORT SIO;
 
  VAR candidate, i: INTEGER;
 
 
BEGIN
  SIO.PutText("Prime number test\n");
  REPEAT
    SIO.PutText("Please enter a positive number; enter 0 to quit. ");
    candidate:= SIO.GetInt();
    IF candidate > 2 THEN
      i:= 1;
      REPEAT 
        i:= i + 1 
      UNTIL ((candidate MOD i) = 0) OR (i * i > candidate);
      IF (candidate MOD i) = 0 THEN
        SIO.PutText("Not a prime number\n")
      ELSE
        SIO.PutText("Prime number\n")
      END; (*IF (candidate MOD i) = 0 ...*)
    ELSIF candidate > 0 THEN
      SIO.PutText("Prime number\n")  (*1 and 2 are prime*)
    END; (*IF candidate > 2*)
  UNTIL candidate <= 0;
END Prim.

Generic Modula-3 Program II

MODULE Euclid2 EXPORTS Main;        (*17.05.94. LB*)
(* The Euclidean algorithm (with controlled input):
   Compute the greatest common divisor (GCD) *)
 
  IMPORT SIO;
 
  VAR
    a, b: INTEGER;     (* input values *) 
    x, y: CARDINAL;    (* working variables *) 
 
<*FATAL SIO.Error*>
 
BEGIN (*statement part*)
  SIO.PutText("Euclidean algorithm\nEnter 2 positive numbers:  ");
 
  a:= SIO.GetInt();
  WHILE a <= 0 DO 
    SIO.PutText("Please enter a positive number:  ");
    a:= SIO.GetInt(); 
  END; (*WHILE a < 0*)
 
  b:= SIO.GetInt();
  WHILE b <= 0 DO 
    SIO.PutText("Please enter a positive number:  ");
    b:= SIO.GetInt(); 
  END; (*WHILE b < 0*)
 
  x:= a; y:= b;   (*x and y can be changed by the algorithm*)    
  WHILE x # y DO
    IF x > y THEN x:= x - y ELSE y:= y - x END;
  END; (*WHILE x # y*)
 
  SIO.PutText("Greatest common divisor = ");
  SIO.PutInt(x);
  SIO.Nl();
END Euclid2.

Programming Proverbs

KISS - ``Keep it simple, stupid.'' - Don't use fancy features when simple ones suffice.

RTFM - ``Read the fascinating manual.'' - Most complaints from the compiler can be solved by reading the book. Logical errors are something else.

Make your documentation short but sweet. - Always document your variable declarations, and tell what each subprogram does.

Every subprogram should do something and hide something - If you cannot concisely explain what your subprogram does, it shouldn't exist. This is why I write the header comments before I write the subroutine.

Program defensively - Add the debugging statements and routines at the beging, because you know you are going to need them later.

A good program is a pretty program. - Remember that you will spend more time reading your programs than we will.

Perfect Shuffles

                    1   1   1   1   1   1   1   1   1
                    2   27  14  33  17  9   5   3   2
                    3   2   27  14  33  17  9   5   3
                    4   28  40  46  49  25  13  7   4
                    5   3   2   27  14  33  17  9   5
                    6   29  15  8   30  41  21  11  6
                    7   4   28  40  46  49  25  13  7
                    8   30  41  21  11  6   29  15  8
                    9   5   3   2   27  14  33  17  9
                    10  31  16  34  43  22  37  19  10
                    11  6   29  15  8   30  41  21  11
                    12  32  42  47  24  38  45  23  12
                    13  7   4   28  40  46  49  25  13
                    14  33  17  9   5   3   2   27  14
                    15  8   30  41  21  11  6   29  15
                    16  34  43  22  37  19  10  31  16
                    17  9   5   3   2   27  14  33  17
                    18  35  18  35  18  35  18  35  18
                    19  10  31  16  34  43  22  37  19
                    20  36  44  48  50  51  26  39  20
                    21  11  6   29  15  8   30  41  21
                    22  37  19  10  31  16  34  43  22
                    23  12  32  42  47  24  38  45  23
                    24  38  45  23  12  32  42  47  24
                    25  13  7   4   28  40  46  49  25
                    26  39  20  36  44  48  50  51  26
                    27  14  33  17  9   5   3   2   27
                    28  40  46  49  25  13  7   4   28
                    29  15  8   30  41  21  11  6   29
                    30  41  21  11  6   29  15  8   30
                    31  16  34  43  22  37  19  10  31
                    32  42  47  24  38  45  23  12  32
                    33  17  9   5   3   2   27  14  33
                    34  43  22  37  19  10  31  16  34
                    35  18  35  18  35  18  35  18  35
                    36  44  48  50  51  26  39  20  36
                    37  19  10  31  16  34  43  22  37
                    38  45  23  12  32  42  47  24  38
                    39  20  36  44  48  50  51  26  39
                    40  46  49  25  13  7   4   28  40
                    41  21  11  6   29  15  8   30  41
                    42  47  24  38  45  23  12  32  42
                    43  22  37  19  10  31  16  34  43
                    44  48  50  51  26  39  20  36  44
                    45  23  12  32  42  47  24  38  45
                    46  49  25  13  7   4   28  40  46
                    47  24  38  45  23  12  32  42  47
                    48  50  51  26  39  20  36  44  48
                    49  25  13  7   4   28  40  46  49
                    50  51  26  39  20  36  44  48  50
                    51  26  39  20  36  44  48  50  51
                    52  52  52  52  52  52  52  52  52




next up previous
Next: About this document Up: My Home Page

Steve Skiena
Thu Sep 4 20:51:33 EDT 1997