1 Abstract Data Types (ADTs) Professor Jennifer Rexford COS 217.

Slides:



Advertisements
Similar presentations
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Advertisements

David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Stacks Example: Stack of plates in cafeteria.
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
1 Abstract Data Types. Objectives To appreciate the concept and purpose of abstract data types, or ADTs To understand both the abstract behavior and the.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
Chapter 6: Stacks STACK APPLICATIONS STACK IMPLEMENTATIONS CS
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 7 : September 8.
1 Generics Professor Jennifer Rexford
Stack C and Data Structures Baojian Hua
1 Midterm Review (overview of fall 2005 midterm) Professor Jennifer Rexford COS 217.
1 Midterm Review (overview of fall 2005 midterm) Professor Jennifer Rexford COS 217.
Stacks, Queues, and Deques
Lists CSE1303 Part A Data Structures and Algorithms.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A5 – Basic Data Structures – Continued (Lists)
Stacks, Queues, and Deques
CS 1031 C++: Object-Oriented Programming Classes and Objects Template classes Operator Overloading Inheritance Polymorphism.
Review for Midterm Chapter 1-9 CSc 212 Data Structures.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Dale Roberts, Lecturer
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
CHAPTER 3 Lists, Stacks, and Queues §1 Abstract Data Type (ADT) 【 Definition 】 Data Type = { Objects }  { Operations } 〖 Example 〗 int = { 0,  1, 
Modularity Lecture 4 Course Name: High Level Programming Language Year : 2010.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Data Structures & Algorithms
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
Week 10 - Wednesday.  What did we talk about last time?  Linked list implementations  Started doubly linked lists.
Stacks Nour El-Kadri CSI Stacks Software stacks are abstract data types (structures) similar to physical stacks, Plates Trays Books PEZ dispenser.
CS 261 Fall 2009 Dynamic Array Introduction (aka Vector, ArrayList)
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Abstract Data Types and Stacks CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Stack Data Structure By Marwa M. A. Elfattah. Stack - What A stack is one of the most important non- primitive linear data structure in computer science.
1 Linked List. 2 List A list refers to a sequence of data items  Example: An array The array index is used for accessing and manipulation of array elements.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
Lecture No.05 Data Structures Dr. Sohail Aslam.  Josephus Problem #include "CList.cpp" void main(int argc, char *argv[]) { CList list; int i, N=10, M=3;
CSC 243 – Java Programming, Fall, 2008 Tuesday, September 30, end of week 5, Interfaces, Derived Classes, and Abstract Classes.
Abstract Data Types and Stacks CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington Last updated: 2/17/
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Abstract Data Types.
Abstract Data Types.
Data Structures and Algorithms
Abstract Data Types and Stacks
The material for this lecture is drawn from Princeton COS217.
Algorithms and Data Structures
Pointers and Linked Lists
Abstract Data Types (ADTs)
Stack ADT & Modularity 2 implementations of the Stack abstract data type: Array Linked List Program design: modularity, abstraction and information hiding.
Data Structures and Algorithms
CSE 303 Concepts and Tools for Software Development
Introduction to Data Structure
Programming Languages and Paradigms
Abstract Data Types (ADTs), After More on the Heap
Stacks, Queues, and Deques
Generics Jennifer Rexford.
Dynamic Array: Implementation of Stack
Abstract Data Types Stacks CSCI 240
Abstract Data Types and Stacks
Presentation transcript:

1 Abstract Data Types (ADTs) Professor Jennifer Rexford COS 217

2 Preparing 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:  Finals:

3 Abstract 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 pointers

4 ADT 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  ReduceRational

5 Several Ways to Represent Rational typedef struct { int numerator; int denominator; } Rational; typedef struct { int ar[2]; } Rational;

6 Another Example ADT: Sets Sets  A collection of elements Interface functions  MakeNullSet  IsMember  Insert  Delete  Union  Intersection

7 Several Ways to Represent Sets typedef struct { Etype elem[MAX]; int size; } Set; Or, as a linked list…

8 Another 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 contexts

9 Goals 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 pointers

10 Stack Interface ( stack.h ) #ifndef STACK_INCLUDED #define STACK_INCLUDED typedef 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); #endif What’s this for?

11 Notes 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 collisions

12 Stack Implementation: Array stack.c #include #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; }

13 Careful Checking With Assert stack.c #include #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!

14 Stack 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]; } count is 3 CAPACITY is 6

15 Problems With Array Implementation CAPACITY too large: waste memory CAPACITY too small: assertion failure (if you were careful) buffer overrun (if you were careless) wasted space data

16 Linked List Would be Better… next val head 321 empty stack push(1); push(2); push(3); struct List { int val; struct List *next; } *head;

17 Popping and Pushing pop( ); head push(4);

18 Stack Implementation: Linked List stack.c #include #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; }

19 Stack Implementation: Linked List int 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; } head 3214 t

20 stack.c, continued Item_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; } head 321

21 Client Program: Uses Interface client.c #include #include “item.h” #include “stack.h” int main(int argc, char *argv[]) { int i; Stack_T s = Stack_new(); for (i = 1; i < argc; i++) Stack_push(s, Item_new(argv[i])); while (!Stack_empty(s)) Item_print(Stack_pop(s)); return 0; }

22 Problem: Multiple Kinds of Stacks? Good, but still not flexible enough  What about a program with multiple kinds of stacks  E.g., a stack of books, and a stack of pancakes  But, you can only define Item_T once Solution in C, though it is a bit clumsy  Don’t define Item_T (i.e., let it be a “void *”)  Good flexibility, but you lose the C type checking typedef 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, void *item); void *Stack_pop(Stack_T stk);

23 stack.h (with void* ) #ifndef STACK_INCLUDED #define STACK_INCLUDED typedef 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, void *item); void *Stack_pop(Stack_T stk); #endif

24 Stack Implementation (with void* ) stack.c #include #include “stack.h” struct Stack {struct List *head;}; struct List {void *val; struct List *next;}; Stack_T Stack_new(void) { Stack_T stk = malloc(sizeof(*stk)); assert(stk); stk->head = NULL; return stk; }

25 stack.c (with void* ) continued int Stack_empty(Stack_T stk) { assert(stk != NULL); return stk->head == NULL; } void Stack_push(Stack_T stk, void *item) { Stack_T t = malloc(sizeof(*t)); assert(t != NULL); assert(stk != NULL); t->val = item; t->next = stk->head; stk->head = t; }

26 stack.c (with void* ) continued void *Stack_pop(Stack_T stk) { void *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; }

27 Client Program (With Void) client.c (with void* ) #include #include “item.h” #include “stack.h” int main(int argc, char *argv[]) { int i; Stack_T s = Stack_new(); for (i = 1; i < argc; i++) Stack_push(s, Item_new(argv[i])); while (!Stack_empty(s)) printf(“%s\n”,Stack_pop(s)); return 0; }

28 Structural Equality Testing Suppose we want to test two stacks for equality: int Stack_equal(Stack_T s1, Stack_T s2); How can this be implemented? int Stack_equal(Stack_T s1, Stack_T s2) { return (s1 == s2); } We want to test whether two stacks are equivalent stacks, not whether they are the same stack.

29 How about this: int Stack_equal(Stack_T s1, Stack_T s2) { struct List *p, *q; for (p=s1->head, q=s2->head; p && q; p=p->next, q=q->next) if (p->val != q->val) return 0; return p==NULL && q==NULL; } This is better, but what we want to test whether s1->val is equivalent to s2->val, not whether it is the same. Almost, But Not Quite...

30 How about this: int Stack_equal(Stack_T s1, Stack_T s2) { struct List *p, *q; for (p=s1->head, q=s2->head; p && q; p=p->next, q=q->next) if ( ! Item_equal(p->val, q->val)) return 0; return p==NULL && q==NULL; } This is good for the “Item_T” version of stacks (provided the Item interface has an Item_equal function), but what about the void* version of stacks? Item ADT Provides Equal Test

31 How about this: int Stack_equal(Stack_T s1, Stack_T s2, int (*equal)(void *, void *)) { struct List *p, *q; for (p=s1->head, q=s2->head; p && q; p=p->next, q=q->next) if ( ! equal((void*)p->val, (void*) q->val)) return 0; return p==NULL && q==NULL; } The client must pass an equality-tester function to Stack_equal. Function Pointers

32 int Stack_equal(Stack_T s1, Stack_T s2, int (*equal)(void *, void *)) { struct List *p, *q; for (p=s1->head, q=s2->head; p && q; p=p->next, q=q->next) if ( ! equal((void*)p->val, (void*) q->val)) return 0; return p==NULL && q==NULL; } Client: int char_equal (char *a, char *b) { return (!strcmp(a,b)); } int string_stacks_equal(Stack_T st1, Stack_T st2) { return Stack_equal(st1, st2, (int (*)(void*, void*)) char_equal); } Passing a Function Pointer cast

33 Conclusions Abstract Data Types (ADTs)  Separation of interface and implementation  Don’t even allow the client to manipulate the data directly Example of a stack  Implementation #1: array  Implementation #2: linked list Void pointers  To allow multiple kinds of elements in the same program  At the expense of type checking Function pointers  Passing functions into other functions