DOC PREVIEW
Princeton COS 217 - Abstract Data Types (ADTs)

This preview shows page 1-2-15-16-17-32-33 out of 33 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Abstract Data Types (ADTs)Preparing for the Midterm ExamAbstract Data Type (ADT)ADT Example: Rational NumbersSeveral Ways to Represent RationalAnother Example ADT: SetsSeveral Ways to Represent SetsAnother ADT Example: StacksGoals for the StackSlide 10Notes on stack.hStack Implementation: ArrayCareful Checking With AssertStack Implementation: Array (Cont.)Problems With Array ImplementationLinked List Would be Better…Popping and PushingStack Implementation: Linked ListSlide 19stack.c, continuedClient Program: Uses InterfaceProblem: Multiple Kinds of Stacks?Slide 23Stack Implementation (with void*)stack.c (with void*) continuedSlide 26Client Program (With Void)Structural Equality TestingAlmost, But Not Quite...Item ADT Provides Equal TestFunction PointersPassing a Function PointerConclusions1Abstract Data Types (ADTs)Professor Jennifer RexfordCOS 2172Preparing for the Midterm Exam•Exam logisticsDate/time: Wednesday March 12 in lectureOpen books, open notes, open mind, but not open laptop/PDACovering material from lecture, precept, and reading, but not tools•Preparing for the midtermLecture and precept materials available onlineCourse textbooks, plus optional books on reserveOffice hours and the course listserv•Old exams from previous semestersMidterms: http://www.cs.princeton.edu/courses/archive/spring08/cos217/exam1old/Finals: http://www.cs.princeton.edu/courses/archive/spring08/cos217/exam2old/3Abstract Data Type (ADT)•An ADT module provides:Data typeFunctions that operate on the type•Client does not manipulate the data representation directlyThe client should just call functions•“Abstract” because the observable results (obtained by client) are independent of the data representationSeparation of interface and implementation•Programming language support for ADTEnsure that client cannot possibly access representation directlyC++, Java, other object-oriented languages have private fieldsC has opaque pointers4ADT Example: Rational Numbers•Rational numbersCan be written as a/b where a and b are int and b != 0Precision may be lost in representing as a single number•Interface functionsMakeRationalAddRationalMultiplyRationalEqualRationalPrintRationalReduceRational5Several Ways to Represent Rationaltypedef struct { int numerator; int denominator;} Rational; typedef struct { int ar[2];} Rational;6Another Example ADT: Sets•SetsA collection of elements•Interface functionsMakeNullSetIsMemberInsertDeleteUnionIntersection7Several Ways to Represent Setstypedef struct { Etype elem[MAX]; int size;} Set; Or, as a linked list…8Another ADT Example: Stacks•LIFO: Last-In, First-Out•Like the stack of trays at the cafeteria“Push” a tray onto the stack“Pop” a tray off the stack•Useful in many contexts9Goals for the Stack•Hide implementation details from the clientPut only the interface in stack.h, and implementation in stack.cOnly allow the client to have a pointer to a Stack•Allow multiple instances of stacks in the same programDefine a type of Stack, rather than a global variable in stack.c•Allow different stacks to have different kinds of elementsAllow another abstract data type to define the type of ItemOnly allow the Stack to refer to a pointer to an Item•Allow different stacks with different element types in the same programUsing void pointers10Stack Interface (stack.h)#ifndef STACK_INCLUDED#define STACK_INCLUDEDtypedef struct Item *Item_T;typedef struct Stack *Stack_T;Stack_T Stack_new(void);int Stack_empty(Stack_T stk); void Stack_push(Stack_T stk, Item_T item);Item_T Stack_pop(Stack_T stk); #endifWhat’s this for?11Notes on stack.h•Type Stack_T is an opaque pointerClients can pass Stack_T around but can’t look insideDefinition of Stack can change without recompiling the client code•Type Item_T is also an opaque pointer… but defined in some other ADTSo, Stack implementation doesn’t need to know about itAnd, Stack implementation can be used for many kinds of items•Stack_ is a disambiguating prefixA convention that helps avoid name collisions12Stack Implementation: Arraystack.c#include <assert.h>#include <stdlib.h>#include “stack.h”enum {CAPACITY = 1000};struct Stack { int count; Item_T data[CAPACITY];};Stack_T Stack_new(void) { Stack_T stk = malloc(sizeof(*stk)); assert(stk != NULL); stk->count = 0; return stk; }13Careful Checking With Assertstack.c#include <assert.h>#include <stdlib.h>#include “stack.h”enum {CAPACITY = 1000};struct Stack { int count; Item_T data[CAPACITY]; };Stack_T Stack_new(void) { Stack_T stk = malloc(sizeof(*stk)); assert(stk != NULL); stk->count = 0; return stk; }Make sure stk!=NULL, or halt the program!14Stack Implementation: Array (Cont.)int Stack_empty(Stack_T stk) { assert(stk != NULL); return (stk->count == 0); }void Stack_push(Stack_T stk, Item_T item) { assert(stk != NULL); assert(stk->count < CAPACITY); stack->data[stack->count] = item; stack->count++;}Item_T Stack_pop(Stack_T stk) { assert(stk != NULL); assert(stk->count > 0); stk->count--; return stk->data[stk->count];}012345count is 3CAPACITY is 615Problems With Array ImplementationCAPACITY too large: waste memoryCAPACITY too small: assertion failure (if you were careful) buffer overrun (if you were careless)wasted spacedatadata16Linked List Would be Better…nextvalheadhead3 2 1empty stackpush(1); push(2); push(3);struct List {int val; struct List *next;} *head;17Popping and Pushingpop( );head3 2 1head3 2 1head2 14push(4);18Stack Implementation: Linked Liststack.c#include <assert.h>#include <stdlib.h>#include “stack.h”struct Stack {struct List *head;};struct List {Item_T val; struct List *next;};Stack_T Stack_new(void) { Stack_T stk = malloc(sizeof(*stk)); assert(stk != NULL); stk->head = NULL; return stk; }19Stack Implementation: Linked Listint Stack_empty(Stack_T stk) { assert(stk != NULL); return (stk->head == NULL); }void Stack_push(Stack_T stk, Item_T item) { struct List *t = malloc(sizeof(*t)); assert(t != NULL); assert(stk != NULL); t->val = item; t->next = stk->head; stk->head = t; }head3 2 14t20stack.c, continuedItem_T Stack_pop(Stack_T stk) { Item_T x; struct List *p; assert(stk != NULL); assert(stk->head != NULL); x = stk->head->val; p = stk->head; stk->head = stk->head->next; free(p); return x;}head3 2 121Client Program: Uses


View Full Document

Princeton COS 217 - Abstract Data Types (ADTs)

Documents in this Course
Summary

Summary

4 pages

Lecture

Lecture

4 pages

Generics

Generics

14 pages

Generics

Generics

16 pages

Lecture

Lecture

20 pages

Debugging

Debugging

35 pages

Types

Types

7 pages

Lecture

Lecture

21 pages

Assembler

Assembler

16 pages

Lecture

Lecture

20 pages

Lecture

Lecture

39 pages

Testing

Testing

44 pages

Pipeline

Pipeline

19 pages

Lecture

Lecture

6 pages

Signals

Signals

67 pages

Building

Building

17 pages

Lecture

Lecture

7 pages

Modules

Modules

12 pages

Generics

Generics

16 pages

Testing

Testing

22 pages

Signals

Signals

34 pages

Lecture

Lecture

19 pages

Load more
Download Abstract Data Types (ADTs)
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Abstract Data Types (ADTs) and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Abstract Data Types (ADTs) 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?