Abstract Data Types (ADTs)

Slides:



Advertisements
Similar presentations
Computer Science 112 Fundamentals of Programming II Overview of Collections.
Advertisements

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.
CHAPTER 3 COLLECTIONS Abstract Data Types. 2 A data type consists of a set of values or elements, called its domain, and a set of operators acting on.
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
CS Winter 2011 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
Lecture 10: Class Review Dr John Levine Algorithms and Complexity March 13th 2006.
CS261 Data Structures Ordered Bag Dynamic Array Implementation.
Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map.
Lecture 7 : Intro. to STL (Standard Template Library)
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.
C++ Review STL CONTAINERS.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
CS Fall 2009 Linked List Introduction. Characteristics of Linked Lists Elements are held in objects termed Links Links are 1-1 with elements, allocated.
Abstract Data Types and Stacks CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington Last updated: 2/17/
UNIT-V ABSTRACT DATA TYPE 1.LIST 2.STACK 3.QUEUE EC6301-II-ECE-C.
Lecture 16 Stacks and Queues Richard Gesick. Sample test questions 1.Write a definition for a Node class that holds a number. 2.Write a method that sums.
Introduction toData structures and Algorithms
Using the Java Collection Libraries COMP 103 # T2
Comprehensive Introduction to OOP with Java, C. Thomas Wu Stack ADT
Cpt S 122 – Data Structures Abstract Data Types
Stacks.
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
Linked List Introduction
G64ADS Advanced Data Structures
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Stacks The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
Fundamentals of Programming II Overview of Collections
CSCI 104 Abstract Data Types
Data Structure Interview Question and Answers
Abstraction A tool (concept) to manage complexity
Data Structures and Algorithms
Abstract Data Types and Stacks
JAVA COLLECTIONS LIBRARY
Homework 4 questions???.
Hassan Khosravi / Geoffrey Tien
Cinda Heeren / Geoffrey Tien
STACKS AND QUEUES UNIT 2 DS THROUGH C++.
Week 15 – Monday CS221.
Data Structures and Database Applications Abstract Data Types
Introduction to Data Structure
CMSC 341 Lecture 5 Stacks, Queues
structures and their relationships." - Linus Torvalds
Stack ADT & Modularity 2 implementations of the Stack abstract data type: Array Linked List Program design: modularity, abstraction and information hiding.
Chapter 19: Stacks and Queues.
structures and their relationships." - Linus Torvalds
Linked Lists: Implementation of Queue & Deque
ITEC 2620M Introduction to Data Structures
Data Structures and Algorithms
Abstract Data Types and Stacks
Introduction to Data Structure
Abstract Data Types, Elementary Data Structures and Arrays
ECE 103 Engineering Programming Chapter 62 Stack Implementation
Fundaments of Game Design
Dynamic allocation (continued)
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Dynamic Array: Implementation of Stack
structures and their relationships." - Linus Torvalds
Abstract Data Types Stacks CSCI 240
A type is a collection of values
Abstract Data Types (ADTs)
Abstract Data Types and Stacks
Presentation transcript:

Abstract Data Types (ADTs) CS 261 – Data Structures Abstract Data Types (ADTs)

Three Levels of Abstraction ADT - Abstract Data Type Defines main characteristics of the container. Specifies operations over the container. Interface - in a particular library of containers Defines how to use it. Behavior constraints. Implementation - in a particular library Specifies how it works.

Abstract Data Type View -- What Described in a language-independent way Example: A Stack is a collection of data items where the last added item must be removed first (LIFO) Basic operations: Add an element to the container Remove an element from the container Check if an element is in the container…

The Interface View – How to Use It Gives specific names to operations In C, interface is defined by .h files Example: Stack interface initStack = Initialize the stack pushStack = Add an element to the stack popStack = Read and remove an element from the stack topStack = Read the top element of the stack isEmptyStack = Check if the stack is empty

The Interface View – How to Use It In addition, the interface also specifies behavior constraints E.g., Push and Pop must be constant time

The Classic ADTs Bag, Ordered Bag: simple collections of data Stack, Queue, Deque: ordered by insertion Priority Queue: ordered by removal List: captures sequential dependencies between data Tree, Graph: captures complex dependencies of data Set: has all unique elements => fast test Map (Dictionary): key/value associations

Bag ADT

Bag ADT Definition: Maintains an unordered collection of data elements Operations: Initialize the bag Add/Remove a data element Check if the bag contains a given element Check the size of the bag Behavior: specific requirements on time of operations

Bag Interface Provide functions for operations on a bag, effectively hiding implementation initBag (container); addBag (container, value); containsBag (container, value); removeBag (container, value); sizeBag (container);

Implement the Bag ADT using the Worksheet 0 Implement the Bag ADT using the static array type in C

Interface .h File for a Bag # define TYPE double # define MAX_SIZE 100 struct Bag { TYPE data[MAX_SIZE]; int size; };

Interface .h File for a Bag # define TYPE double # define MAX_SIZE 100 struct Bag { TYPE data[MAX_SIZE]; int size; }; /* function prototypes */ void initBag (struct Bag *b); void addBag (struct Bag *b, TYPE val); int containsBag (struct Bag * b, TYPE val); void removeBag (struct Bag * b, TYPE val); int sizeBag (struct Bag * b);

Implementation -- Add an Element to Bag void addBag (struct Bag * b, TYPE val){ /*check if the bag was initialized*/ assert(b != NULL); /*check if there is enough memory space*/ if (b->size >= MAX_SIZE) return; b->data[b->size] = val; b->size++; /*adding an element increases size*/ }

Remove an Element from Bag void removeBag (struct Bag * b, TYPE val) { assert(b != NULL); /*check if b was initialized*/ int i = b->size-1; /*index of the last element*/ while (i >= 0) {/*exhaustive search*/ if (b->data[i] == val){ /*found it*/ /*swap last with current to avoid the gap*/ b->data[i] = b->data[b->size - 1]; b->size--; /*the size decreases by one*/ return;/*removes one occurrence*/ } i--; Complexity? O(n)

Next Lecture Dynamic array -- introduction