PS3-Slides typedef struct ProcessTag{ int id; int size; int time; int importance; int priority; } PrintJob; Priority is - say - a function of size, time.

Slides:



Advertisements
Similar presentations
Chapter6 LISTS AND STRINGS. Outline 1. List Specifications 2. List Implementations (a) Class Templates (b) Contiguous (c) Simply Linked (d) Simply Linked.
Advertisements

Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
C Functions. What are they? In general, functions are blocks of code that perform a number of pre-defined commands to accomplish something productive.
C Language.
Data Structure & Abstract Data Type
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Module R2 Overview. Process queues As processes enter the system and transition from state to state, they are stored queues. There may be many different.
Priority Queues and Heaps. Overview Our last ADT: PriorityQueueADT A new data structure: heaps One more sorting algorithm: heapsort Priority Queues and.
PS3-Slides typedef struct ProcessTag{ int id; int size; int time; int importance; int priority; } PrintJob; Priority is - say - a function of size, time.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
CS 201 Functions Debzani Deb.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
Data Structures Topic #3. Today’s Agenda Ordered List ADTs –What are they –Discuss two different interpretations of an “ordered list” –Are manipulated.
1 CSC241: Object Oriented Programming Lecture No 07.
Object Oriented Data Structures
Review C++ exception handling mechanism Try-throw-catch block How does it work What is exception specification? What if a exception is not caught?
Data Structures Using C++ 2E
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
General Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Holding Your Objects Generics and type-safe containers One of the problems of using pre-Java SE5 containers was that the compiler allowed.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
#include guards Practical session #2 Software Engineering
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
ENEE150 – 0102 ANDREW GOFFIN Project 4 & Function Pointers.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
1 MORE ON MODULAR DESIGN: MODULE COMMUNICATIONS. 2 WHEN A FUNCTION IS INVOKED, MEMORY IS ALLOCATED LOCALLY FOR THE FORMAL PARAMETERS AND THE VALUE OF.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Data Structures and Collections Principles.NET: –Two libraries: System.Collections System.Collections.Generics FEN 2014UCN Teknologi/act2learn1 Deprecated.
CS Data Structures I Chapter 2 Principles of Programming & Software Engineering.
Abstract Data Types and Stacks CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Computing II Modularity, Information Hiding and Abstract Data Types. Difficulty: Programs that solve “Real World” problems can get very large.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
1 Principles revisited.NET: Two libraries: System.Collections System.Collections.Generics Data Structures and Collections.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
ENEE150 – 0102 ANDREW GOFFIN Abstract Data Types.
Problem Session 4 Header Files and Unix. Where are they? You may need to examine the header file to determine what the data structures and functions provided.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Queues Manolis Koubarakis Data Structures and Programming Techniques 1.
Data Storage So far variables have been able to store only one value at a time. What do you do if you have many similar values that all need to be stored?
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Chapter 12 Classes and Abstraction
Abstract Data Types in C
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Abstract Data Types (ADTs)
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Classes and Data Abstraction
6 Chapter Functions.
A Heap Implementation Chapter 26 Adapted from Pearson Education, Inc.
Introduction to Data Structure
2. Second Step for Learning C++ Programming • Data Type • Char • Float
By Yogesh Neopaney Assistant Professor Department of Computer Science
Priority Queue and Heap
SPL – PS1 Introduction to C++.
Presentation transcript:

PS3-Slides typedef struct ProcessTag{ int id; int size; int time; int importance; int priority; } PrintJob; Priority is - say - a function of size, time and importance Process Queue Insert Process into Process Queue Remove Process of highest priority

PS3-Slides Using an Abstract Data Type Application program Module Implementation Module Interface Specification Process Manager Priority Queue Abstract Data Type

PS3-Slides Priority Queue Abstract Data Type Interface Specification 1) Application must define WHAT is stored. PQ ADT does not really need it (if “generic”). typedef struct { int id; /* assigned automatically and sequentially from 1 */ int size; /* memory requirements for process */ int time; /* time requirement for process */ int importance; /* insignificant = 0; average = 1; urgent = 2 */ int priority; } Process; 2) Functions the Priority Queue needs from the Application: bool Order(GenericPtr x, GenericPtr y);

PS3-Slides Priority Queue Abstract Data Type Interface Specification 3) Public Functions Available to Application from Priority Queue: void PQInitialize(member_type type, PriorityQueue *PQ, bool (* Order)(GenericPtr, GenericPtr)) bool PQEmpty(PriorityQueue *); bool PQFull(PriorityQueue *); int PQSize(PriorityQueue *); void PQInsert(GenericPtr, PriorityQueue *); GenericPtr PQRemove(PriorityQueue *);

PS3-Slides Communicating Between Application Program and Module /* Application Program File */ #include … #include ”Application.h" /* definitions of items and structures used in the application program, and prototypes of application functions the module needs */ #include ”ModuleInterface.h" /* public data and function prototypes in the module */ void ApplicationFunction1(void) { … } … int main(void){ … }

PS3-Slides Communicating Between Application Program and Module /* Module File */ #include … #include "ModuleInterface.h" /* see previous comments */ void ModuleFunction1(void){ … } …

PS3-Slides Application program Module Implementation 1 Module Interface Specification Application program Module Implementation 2 Module Interface Specification Ideally, these are identical These are different Changing Module Implementations

// Common Type definitions: // To be cast as needed - user or default… typedef void * GenericPtr; // Priority Queue Types supported (could be) enum member_type {Char, Int, Long, Float, Double, Proc}; // The Priority Queue Header: typed, order function // Notice that Content will point to the actual // implementation - invisible from outside. typedef struct{ member_type type; bool (* order)(GenericPtr, GenericPtr); int Count; GenericPtr Content; } PriorityQueue; PS3-Slides

Priority Queue Implemented with a Sorted Linked List Count Content NIPtr Link NIPtr Link NIPtr Link The Priority Queue typedef struct PQNodeTag { GenericPtrNodeItemPtr; struct PQNodeTag *Link; } PQListNode; type order Process typedef struct { int id; int size; int time; int importance; int priority; } Process;

PS3-Slides Priority Queue Implemented with Unsorted Array The Priority Queue ((GenericPtr *)(PQ->Content))[0] ((GenericPtr *)(PQ->Content))[1] ((GenericPtr *)(PQ->Content))[2] ((GenericPtr *)(PQ->Content))[3] ((GenericPtr *)(PQ->Content))[Count-1] ((GenericPtr *)(PQ->Content))[MAXCOUNT-1] Process PQ->Content = malloc(MAXCOUNT*sizeof(GenericPtr)); Items in order of INSERTION. Count Content type order

Process Management using a Priority Queue This program uses a Priority Queue to manage processes. The list can hold a maximum of 50 jobs. Using Sorted Linked-list Implementation of Priority Queue. MAKE SURE YOUR APPLICATION PROGRAM USES '#include PQImpl1.h'. Use the H command anytime to list valid commands. Valid commands are: A - Add a process to the Priority Queue R - Remove the highest priority process from the Priority Queue C - Show the number of processes in the Priority Queue H - List valid commands Q - Quit Command> a New process job: Size: 4 Time: 12 Importance (insignificant=0, average=1, or urgent=2): 1 Job id = 1 size = 4 time = 12 importance = 1 priority = 4 PS3-Slides

Command> a New process: Size: 2 Time: 8 Importance (insignificant=0, average=1, or urgent=2): 1 Job id = 2 size = 2 time = 8 importance = 1 priority = 2 Command> a New process: Size: 7 Time: 22 Importance (insignificant=0, average=1, or urgent=2): 1 Job id = 3 size = 7 time = 22 importance = 1 priority = 7 Command> c Number of jobs in Process Queue is 3 Command> r Highest priority process removed: Job id = 3 size = 7 time = 22 importance = 1 priority = 7 Command>

PS3-Slides What are we trading? The “regular” PS3 implementation requires a lot of information flowing back and forth, and allows only one type of Priority Queue to exist within a program. It also requires recompilation of both Application Program AND library module every time one switches from one implementation to another. It is relatively “type safe”, since the compiler can figure out statically whether you are trying to insert or retrieve objects of the wrong type.

This (“generic”) implementation requires no information flowing from the application program to the library module, and allows multiple types of Priority Queues to exist within a program. It does not require recompilation of either Application Program or module to change from one implementation to another: linking of object code files suffices (unless we want to extend the family of types we associate with the Priority Queue). All type safety is lost: only careful discipline on the part of the programmer will avoid disasters - almost no help will be available from the compiler. It is also somewhat more complex to implement. PS3-Slides