מחסנית ותור Stacks and Queues.

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Stacks and Queues. Not really data structures – More of an enforcement of policy – Can be implemented using an array or linked list – Can store just about.
Stack & Queues COP 3502.
מבוא למדעי המחשב לתעשייה וניהול דוגמאות ותרגול נוסף במערך חד ממדי הרצאה 12.
Data Structures & Algorithms
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
רשימה מקושרת Linked Lists. דוגמא STRING איך עושים Dim x as String בלי לדעת מראש את הגודל !
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
שימוש במערך דינמי : ArrayList. מאפיינים חשובים בכל LIST יכולת להכניס מידע בלי תלות בטיפוס יכולת למחוק מידע יכולת להוסיף מידע פונקציות נוספות ( מיון, חיפוש.
מערכים מבוא לתכנות למנע"ס - שבוע מספר 8 - מאיר קומר - סמסטר ב' - תשס"ו והנה בעיה כתוב תוכנית אשר תקלוט 36 מספרים ותדפיס כמה מתוכם גדולים יותר מהממוצע.
מחסנית (Stack): מבנה נתונים עליו מוגדרות הפעולות הבאות:
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
מבני בקרה לולאות. פקודת CASE Module Module1 Sub Main() Dim input As Char input = Console.ReadLine() Select Case (input) Case "A" Console.WriteLine("A.
Stacks and Queues. Stack ADT סוג של מערך מוגבל מהר מאוד ולוקחים מעט זכרון שימוש ב LIFO – LIFO (Last In, First Out) lists. –אפשר להוסיף רק בסוף הרשימה.
תכנות מונחה עצמים Object Oriented Programming (OOP) אתגר מחזור ב' Templates תבניות.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
פונקציות. פונקציות מוכרות Imports System.Math Module Module1 Sub Main() Dim x As Decimal = Math.Abs(-10.4) Dim y As Decimal Console.WriteLine("I will.
Stacks, Queues, and Deques
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Structure. מה לומדים היום ? דרך לבנות מבנה נתונים בסיסי – Structure מייצר " טיפוס " חדש מתאים כאשר רוצים לאגד כמה משתנים יחד דוגמאות : עובד : שם, טלפון,
Stack and Queue.
Stacks and queues Basic operations Implementation of stacks and queues Stack and Queue in java.util Data Structures and Algorithms in Java, Third EditionCh04.
Stacks and Queues Introduction to Computing Science and Programming I.
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.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
מבנה נתונים ואלגוריתמים ) לשעבר - עיבוד מידע( ד"ר אבי רוזנפלד ד"ר אריאלה ריכרדסון.
September 05 Kraemer UGA/CSCI 2720 Lists – Part I CSCI 2720 Eileen Kraemer The University of Georgia.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
מבוא למדעי המחשב לתעשייה וניהול הרצאה 7. סברוטינות subroutines.
Stacks And Queues Chapter 18.
Arrays and Collections Tonga Institute of Higher Education.
Foundations of Data Structures Practical Session #4 Recurrence ADT 08/04/2013Amihai Savir & Ilya Mirsky
מחסנית ותור Stacks and Queues. מחסנית Stack מחסנית - Stack ADT סוג של מערך מוגבל מהיר מאוד ותופס מעט זיכרון שימוש ב LIFO – LIFO (Last In, First Out)
מבנה נתונים ואלגוריתמים ) לשעבר - עיבוד מידע( ד"ר אבי רוזנפלד ד"ר אריאלה ריכרדסון.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
מבוא למדעי המחשב לתעשייה וניהול הרצאה 12. ספריות.
מחסנית ותור Stacks and Queues. מחסנית Stack מחסנית - Stack ADT סוג של מערך מוגבל מהיר מאוד ותופס מעט זיכרון שימוש ב LIFO – LIFO (Last In, First Out)
Stacks and Queues CMSC 201. Stacks and Queues Sometimes, when we use a data-structure in a very specific way, we have a special name for it. This is to.
מבנה נתונים ואלגוריתמים ) לשעבר - עיבוד מידע( ד"ר אבי רוזנפלד ד"ר אריאלה ריכרדסון.
CS-2851 Dr. Mark L. Hornick 1 Stacks and Queues Behavior vs. Structure.
Click to edit Master text styles Stacks Data Structure.
Data Structures Intro2CS – week Stack ADT (Abstract Data Type) A container with 3 basic actions: – push(item) – pop() – is_empty() Semantics: –
אלגוריתמי חיפוש. Brute Force Module Module1 Function BruteForce(ByRef x() As Integer, ByRef item As Integer) As Integer Dim i As Integer For i = 0 To.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
Comprehensive Introduction to OOP with Java, C. Thomas Wu Stack ADT
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.
מבוא למדעי המחשב לתעשייה וניהול
QueueStack CS1020.
September 29 – Stacks and queues
Chapter 15 Lists Objectives
Stacks and Queues.
Stack and Queue APURBO DATTA.
מחלקות classes.
Data Structures and Database Applications Queues in C#
CMSC 341 Lecture 5 Stacks, Queues
Principles of Computing – UFCFA3-30-1
Web Services אוספים - Collections ליווי מקצועי : ארז קלר
Stacks, Queues, and Deques
Lecture 21 Stacks and Queues Richard Gesick.
Stacks, Queues, and Deques
Java Collections Framework
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Lecture 2: Stacks and Queues
Lecture 16 Stacks and Queues CSE /26/2018.
Abstract Data Type (ADT)
Stacks, Queues, and Deques
Lecture 16 Stacks and Queues CSE /26/2018.
Presentation transcript:

מחסנית ותור Stacks and Queues

מחסנית Stack

מחסנית - Stack ADT סוג של מערך מוגבל שימוש בLIFO מהיר מאוד ותופס מעט זיכרון שימוש בLIFO LIFO (Last In, First Out) lists. אפשר להוסיף רק בסוף הרשימה PUSH אפשר להוריד רק מסוף הרשימה POP (ADT – Abstract Data Type)

הפעולות הבסיסיות: השימוש של STACK הוא LIST עם מגבלות אפשר להוסיף רק לראש הרשימה PUSH סוג של INSERT POP סוג של REMOVE PEEK דרך לראות את הערך בראש הרשימה – הדרך היחידה לראות ערכים בלי להוריד אותם!

Push and Pop Primary operations: Push and Pop Push Pop Add an element to the top of the stack Pop Remove the element at the top of the stack empty stack push an element push another pop top B top top A A A top

דוגמא של POP Module Module1 Sub Main() Dim test As New Stack() Dim i As Integer For i = 1 To 5 test.Push(i) Next Console.WriteLine(test.Count) For i = 1 To test.Count Dim num As Integer = test.Pop() Console.WriteLine(num) End Sub End Module

דוגמא של PEEK Module Module1 Sub Main() Dim test As New Stack() Dim i As Integer For i = 1 To 5 test.Push(i) Next Console.WriteLine(test.Count) For i = 1 To test.Count Dim num As Integer = test.Peek() Console.WriteLine(num) End Sub End Module

תור Queue

Queue ADT סוג אחר של מערך מוגבל שימוש בFIFO מהיר מאוד, ולוקח מעט זיכרון שימוש בFIFO FIFO (First In, First Out) lists. אפשר להוסיף רק בסוף הרשימה Enqueue אפשר להוריד רק מהתחלת הרשימה Dequeue

דוגמא Module Module1 Sub Main() Dim queue As New Queue Dim i As Integer For i = 1 To 5 queue.Enqueue(i) Next For i = 1 To queue.Count Console.WriteLine(queue.Dequeue()) End Sub End Module

תרגיל כיתה אני מעונין לבנות מערכת לטפל בתהליך יצירת הדוחות בתוך משרד נבנה STRUCT פשוט לדוח נסמלץ תהליכי עבודה FIFO וLIFO נבנה פונקציות להדפיס נתונים ולחפש נתונים שימו לב: יש שינויים טכניים ולוגיים בין STACK וQUEUE (כמו שנראה)...

הSTRUCT Structure Report Dim code As Integer ' date type could be used instead... Dim Topic As String Dim Approval As Boolean Dim Content As String End Structure

הMAIN Sub Main() Dim ListQ As New Queue() Dim ListS As New Stack() Dim temp As Report For i = 0 To 5 temp.code = i temp.Topic = "Doch" + Convert.ToString(i) temp.Approval = False temp.Content = "blah" ListQ.Enqueue(temp) ListS.Push(temp) Next PrintStack(ListS) Console.WriteLine("And now...") Console.WriteLine("And the Queue...") PrintQueue(ListQ) Console.WriteLine("I found 0 in pos " & FindStack(ListS, 0)) Console.WriteLine("I found 0 in pos " & FindQueue(ListQ, 0)) End Sub

הPrintStack וPrintQueue Sub PrintStack(ByVal a As Stack) Dim extra As New Stack Dim times As Integer = a.Count Dim temp As New Report For i = 1 To times temp = a.Pop() ' why is a.Peek() a mistake? Console.WriteLine("The contents are {0} and {1} and {2}", temp.code, temp.Topic, temp.Content) extra.Push(temp) Next ' What happens without this??? temp = extra.Pop() a.Push(temp) End Sub Sub PrintQueue(ByVal a As Queue) Dim extra As New Queue Dim times As Integer = a.Count Dim temp As New Report For i = 1 To times temp = a.Dequeue() Console.WriteLine("The contents are {0} and {1} and {2}", temp.code, temp.Topic, temp.Content) extra.Enqueue(temp) Next ' What happens without this??? temp = extra.Dequeue() a.Enqueue(temp) End Sub

הצעה עבור FindStack וFindQueue Function FindStack(ByVal a As Stack, ByVal key As Integer) As Integer Dim extra As New Stack Dim times As Integer = a.Count Dim temp As New Report Dim count As Integer = 0 For i = 1 To times temp = a.Pop() count += 1 If temp.code = key Then Return count End If extra.Push(temp) Next temp = extra.Pop() a.Push(temp) Return -1 End Function Function FindQueue(ByVal a As Queue, ByVal key As Integer) As Integer Dim extra As New Queue Dim times As Integer = a.Count Dim temp As New Report Dim count As Integer = 0 For i = 1 To times temp = a.Dequeue() count += 1 If temp.code = key Then Return count End If extra.Enqueue(temp) Next temp = extra.Dequeue() a.Enqueue(temp) Return -1 End Function מה קורה אם מצאנו, ואח"כ נחפש שוב?

פתרון אפשרי עבור FindStack וFindQueue Function FindStack(ByVal a As Stack, ByVal key As Integer) As Integer Dim extra As New Stack Dim times As Integer = a.Count Dim temp As New Report Dim count As Integer = 0 For i = 1 To times temp = a.Pop() extra.Push(temp) count += 1 If temp.code = key Then For j = 1 To count temp = extra.Pop() a.Push(temp) Next Return (count) End If Return -1 End Function Function FindQueue(ByVal a As Queue, ByVal key As Integer) As Integer Dim extra As New Queue Dim times As Integer = a.Count Dim temp As New Report Dim count As Integer = 0 For i = 1 To times temp = a.Dequeue() extra.Enqueue(temp) count += 1 If temp.code = key Then For j = i + 1 To times Next For j = 1 To times temp = extra.Dequeue() a.Enqueue(temp) Return (count) End If Return -1 End Function

להמציא את הגלגל מחדש

להמציא מחדש את הגלגל יצירת מחסנית (עם פונקציות) להמציא מחדש את הגלגל יצירת מחסנית (עם פונקציות) Module Module1 Function Count(ByVal list As ArrayList) As Integer Return list.Count() End Function Sub Push(ByVal val As Object, ByRef list As ArrayList) list.Add(val) End Sub Function Pop(ByVal list As ArrayList) As Object Dim obj As Object = list.Item(list.Count - 1) list.RemoveAt(list.Count - 1) Return obj Function Peek(ByVal list As ArrayList) As Object Return list.Item(list.Count - 1) המשך....

להמציא מחדש את הגלגל - מחסנית, המשך Sub Main() Dim test As New ArrayList() Dim i As Integer For i = 0 To 4 Push(i, test) Next Console.WriteLine(Count(test)) For i = 0 To test.Count - 1 Dim num As Integer = Pop(test) Console.WriteLine(num) End Sub End Module

תרגיל: איך בונים QUEUE (עם פונקציות)? Function Count(ByVal list As ArrayList) As Integer Return List.Count() End Function Sub Enqueue(ByVal val As Object, ByRef list As ArrayList) ??? End Sub Function Dequeue(ByVal list As ArrayList) As Object Function Peek(ByVal list As ArrayList) As Object Return list.Item(0)

תרגיל: לחשב מחיר על בסיס LIFO וגם FIFO QUEUE לFIFO STACK לLIFO יש לבנות מבנה עם מחיר וכמות יש להכניס ערכים לתוך STACK וQUEUE Push, Enqueue יש לחשב את המחיר לפי הפונקציות: DEQUEUE (לQUEUE) POP (לSTACK)

איך מתחילים?? Structure Stock Dim Amount As Integer Dim Price As Decimal End Structure Module Module1 Sub Main() Dim List1 As New Queue() Dim List2 As New Stack() Dim temp As Stock temp.Amount = 10 temp.Price = 5.5 List1.Enqueue(temp) List2.Push(temp) temp.Amount = 50 temp.Price = 8.5 temp = List1.Peek() Console.WriteLine("What's the cost? " & temp.Price) temp = List2.Peek() End Sub End Module

איך מתחילים?? אפשרות נוספת Structure Stock Dim Price As Decimal End Structure Module Module1 Sub Main() Dim List1 As New Queue() Dim List2 As New Stack() Dim temp As Stock temp.Price = 5.5 List1.Enqueue(temp) List2.Push(temp) temp.Price = 8.5 temp = List1.Peek() Console.WriteLine("What's the cost? " & temp.Price) temp = List2.Peek() End Sub End Module