סוגי נתונים מופשטים (Abstract Data Type)

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be instantiated.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
CSCE 3110 Data Structures & Algorithm Analysis Stacks and Queues Reading: Chap.3 Weiss.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
More on Stacks and Queues. As we mentioned before, two common introductory Abstract Data Type (ADT) that worth studying are Stack and Queue Many problems.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
A stack is a data linear data structure in which addition of new element or deletion of an existing element always takes place at the same end. This.
COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues.
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
 Balancing Symbols 3. Applications
Sit-in lab 4.  Given a number of strings containing only “a”, “b”, “c”, “d”  Check each string that it follows the pattern a n b m c m d n where m,
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
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.
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: the.
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.
Reverse Polish Expressions Some general observations about what they are and how they relate to infix expressions. These 9 slides provide details about.
Stacks, Queues & Deques CSC212.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Objectives of these slides:
1 Stacks – Chapter 3 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. Alternatively,
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Sieve public class sieve { public static void main(String args[]) {
Cosc237/data structures1 Data Types Every data type has two characteristics: 1.Domain - set of all possible values 2.set of allowable operations Built-in.
September 05 Kraemer UGA/CSCI 2720 Lists – Part I CSCI 2720 Eileen Kraemer The University of Georgia.
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Object-Oriented Programming Simple Stack Implementation.
Stacks And Queues Chapter 18.
Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
1 Stacks & Queues CSC Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.
1 Chapter 17: Stacks and Queues Learn about stacks Examine various stack operations Learn how to implement a stack as an array Learn how to implement a.
CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008.
Stacks & Queues CSC 172 SPRING 2002 LECTURE 4 Agenda  Stack  Definition  Implementation  Analysis  Queue  Definition  Implementation  Analysis.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
CSC 172 DATA STRUCTURES. A TALE OF TWO STRUCTURES.
Stacks and Queues ● Introduction to stacks ● Stack implementation ● 4 Standard operations on a stack ● A stack program ● Introduction to queues ● Queue.
BCA II Data Structure Using C
Queues Chapter 4.
Unit 4 Stacks And Queues King Fahd University of Petroleum & Minerals
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.
Lists, Stacks and Queues in C
Stacks and Queues.
CSC 172 DATA STRUCTURES.
CSCE 3110 Data Structures & Algorithm Analysis
Data Structures and Analysis (COMP 410)
CMSC 341 Lecture 5 Stacks, Queues
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
אחסון (אירגון) מידע DATA DATA DATA Link Link Link … …
Elementary Data Structures
Dynamic Sets (III, Introduction)
Stacks and Queues 1.
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
class PrintOnetoTen { public static void main(String args[]) {
Abstract Data Type Abstract Data Type as a design tool
Stacks.
Stacks and Queues.
Stacks CS-240 Dick Steflik.
slides created by Alyssa Harding
Abstract Data Types Stacks CSCI 240
Lecture 9: Stack and Queue
Presentation transcript:

סוגי נתונים מופשטים (Abstract Data Type) בדר"כ כאשר אנו כותבים תוכנית אנו מתחייבים להגדיר את כל הנתונים באופן מדויק ( .(int, float, char אבל בתיאור של אלגוריתם אין צורך בהגדרות מדויקות של כל הפרטים ואפילו אין צורך בייצוג קונקרטי של הזכרון בו מאוכסנים הנתונים. למשל, בבעיית מיון עבור קבוצת נתונים S חשוב שלכל זוג a, b  S או a<b או a>b או .a=bלא משנה מאיזה סוג נתונים אבל חשוב שניתן להשוות ביניהם.

סוגי נתונים מופשטים (Abstract Data Types) הגדרה: אוסף נתונים ופעולות עליהם ללא פרטים למימוש נקרא סוג נתונים מופשט .(ADT) הכי חשוב באילו פעולות תומך ADT מסוים

רשימה ADT (List) היא סידרה של אלמנטים L={x1,x2,…, xn} |L| מסמן אורך הסדרה .

ההצגות הבסיסיות של רשימה על ידי: מערך (בזכרון רציף) רשימה מקושרת (בזכרון לא רציף)

פעולות מופשטות על רשימה פעולות בסיסיות Head()  Ptr Next(Ptr)  Ptr מיקום של האיבר הבא Prev(Ptr)  Ptr Key(Ptr)  value Insert(Ptr,newPtr)  Head() Delete(Ptr)  Head() MakeEmptyList() יצירת הרשימה Length()  value Outside(Ptr)  boolean אמת – מחוץ לגבולות

פעולות מופשטות על רשימה פעולות מורכבות Access(index)value ListSearch(value)Ptr IsEmptyList() בדיקה האם הרשימה ריקה

פעולות מופשטות על רשימה דוגמא ListSearch(value):Ptr Ptr x  Head() While (not Outside(x)) and Key(x) <> value x  Next(x) If Outside(x) Return nil Else Return x

מחסנית ADT (Stack) אוסף סדור של אלמנטים כאשר הכנסה והוצאה – דרך קצה אחד, הנקרא ראש המחסנית. האחרון הנכנס הוא הראשון שיצא. LIFO: last –in, first out))

פעולות מופשטות על מחסנית Top()  value בדיקה מהו האיבר בראש המחסנית Pop()  value הוצאת איבר תמיד מראש המחסנית Push(x) הכנסת איבר תמיד לראש המחסנית MakeEmptyStack( )Ptr יצירת המחסנית IsEmptyStack() בדיקה האם המחסנית ריקה

דוגמא לשימוש במחסנית ללא סוגריים:(1+5)*3 רוצים לחשב ביטוי(infix) 1 5 + 3 * (postfix) ? postfix ל-1+5*3איך נתרגם את :

דוגמא לשימוש במחסנית חישוב ביטוי ב-postfix נניח מחסנית ריקה: 1 5 + 3 * בא מספר --- מכניסים למחסנית: באה פעולה : מוציאים 2 איברים מהמחסנית מבצעים חישוב מכניסים תוצאה למחסנית כאשר הסדרה הסתיימה : האיבר (היחיד) בראש המחסנית הוא התשובה.

דוגמא לשימוש במחסנית סדרת הפעולות: 1 5 + 3 * דוגמא לשימוש במחסנית סדרת הפעולות: 1 5 + 3 * Push(1) Push(5) Pop(5) Pop(1) 5+1 Push(6) Push(3) Pop(3) Pop(6) 3*6 Push(18)

דוגמא לשימוש במחסנית מציאת ההוראה הבאה לבצוע דוגמא לשימוש במחסנית מציאת ההוראה הבאה לבצוע main f g X5; a2; v3; f(X); bg(a); w5; Z8; c4;

דוגמא לשימוש במחסנית מציאת ההוראה הבאה לבצוע קיים program counter (בקיצור: pc) יחיד, והוא מכיל את כתובת ההוראה הבאה לבצוע לאחר טעינת התכנית ה-pc מקבל את כתובת ההוראה הראשונה מחזור רגיל: טעינת ההוראה ל- cpu קידום ה-pc להוראה הבאה בצוע ההוראה שנטענה ל- cpu בזהוי שההוראה היא קריאה לפונקציה: הכנסת ערך ה- pc למחסנית נתינת כתובת ההוראה הראשונה של הפונקציה ל- pc ביציאה מהפונקציה: הוצאת ערך מהמחסנית ונתינתו ל- pc אם יוצאים מפונקציה והמחסנית ריקה - סיימנו

דוגמא לשימוש במחסנית סדרת הפעולות: main f g X5; a2; v3; f(X); bg(a); w5; Z8; c4; Push(pc  “z  8”) Push(pc  “c  4”) Pc  Pop() /* c  4 */ Pc  Pop() /* z  8 */ Pc  Pop() /* אבל המחסנית ריקה ... סיימנו!*/

ההצגות הבסיסיות של Stack על ידי: מערך (בזכרון רציף) רשימה מקושרת (בזכרון לא רציף)

class Stack{ int stack[]; int head;//head of stack int n; //size of the stack //........................................................... public Stack(int n){ stack = new int[n]; head = 0; this.n = n; } //......................................................... public boolean empty(){ return (head == 0); public boolean full(){ return (head == n);

public int pop(){ if (this.empty()){ System.out.println("The stack is empty"); //underflow error return 0; }else{ int el = stack[head-1]; head--; return el; } //........................................................... public void push(int el){ if (this.full()){ System.out.println("The stack is full");//overflow error stack[head] =el; head++;

//........................................................... public static void main(String args[]){ Stack s = new Stack (5); for(int i=0;i<7;i++) s.push(i); for(int i=0;i<6;i++) System.out.println(s.pop()+" "); } The stack is full 4 3 2 1 The stack is empty Process Exit...

תור ADT (Queue) אוסף סדור של אלמנטים כאשר הכנסה דרך קצה אחד, הנקרא סוף התור; הוצאה דרך קצה האחר הנקרא ראש התור. אלמנטים הראשון הנכנס הוא הראשון שיצא. FIFO: first –in, first -out))

פעולות מופשטות על התור Front()  value בדיקה מהו האיבר ראשון בתור Dequeue()  value הוצאת איבר תמיד מראש התור Enqueue(x) הכנסת איבר תמיד לסוף התור MakeEmptyQueue( ) יצירת התור IsEmptyQueue() בדיקה האם התור ריק

דוגמאות תור : Queue תור הדפסה פשוט Queue X2; YX+3; Z5;

ההצגות הבסיסיות של Queue על ידי: מערך (בזכרון רציף) רשימה מקושרת (בזכרון לא רציף)

תור ADT עדיפות (Priority Queue) כל אלמנט כולל שדה נוסף Priority אוסף סדור של אלמנטים כאשר הכנסה דרך קצה אחד; הוצאה דרך קצה האחר. האלמנט בעל ה- Priority המקסימלי (מינימלי) הוא הראשון שיצא.

פעולות על תור עדיפות Maximum()  value מחזיר איבר בעל עדיפות מקסימלית בתור Extract_Max()  value הוצאת איבר בעל עדיפות מקסימלית AddPriorityQueue (x) הוספת איבר לתור MakeEmptyPriorityQueue( ) יצירת התור IsEmptyPriorityQueue( ) בדיקה האם התור ריק PriorityQueueSort(set X )

דוגמאות תור עדיפות: to schedule jobs on a shared computer: דוגמאות תור עדיפות: to schedule jobs on a shared computer: When a job is finished, the highest-priority job is selected from the pending (by Extract_Max()) A new job can be added to the queue at any time (by AddPriorityQueue())