1 / 15

מחסנית ותור

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

quickj
Download Presentation

מחסנית ותור

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. מחסניתStack

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

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

  5. A A B A top top top Push and Pop • Primary operations: Push and Pop • Push • 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

  6. דוגמא של 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) Next End Sub End Module

  7. דוגמא של 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) Next End Sub End Module

  8. להמציא מחדש את הגלגל (עם פונקציות) Module Module1 Function Count(ByVal list As ArrayList) As Integer Return list.Count() End Function Sub Push(ByValval 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 End Function Function Peek(ByVal list As ArrayList) As Object Return list.Item(list.Count - 1) End Function המשך....

  9. להמציא מחדש את הגלגל, המשך 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) Next End Sub End Module

  10. תורQueue

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

  12. דוגמא 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()) Next End Sub End Module

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

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

  15. איך מתחילים?? 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 List1.Enqueue(temp) List2.Push(temp) temp = List1.Peek() Console.WriteLine("What's the cost? " & temp.Price) temp = List2.Peek() Console.WriteLine("What's the cost? " & temp.Price) End Sub End Module

More Related