מחסנית ותור Stacks and Queues. מחסנית Stack מחסנית - Stack ADT סוג של...
-
Upload
george-booker -
Category
Documents
-
view
219 -
download
4
Transcript of מחסנית ותור Stacks and Queues. מחסנית Stack מחסנית - Stack ADT סוג של...
מחסנית ותור
Stacks and Queues
מחסניתStack
Stack ADTמחסנית - סוג של מערך מוגבל•
מהיר מאוד ותופס מעט זיכרון
LIFOשימוש ב•–LIFO (Last In, First Out) lists. הרשימהבסוףאפשר להוסיף רק –
•PUSH
הרשימהמסוףאפשר להוריד רק –•POP
• (ADT – Abstract Data Type)
:הפעולות הבסיסיות
עם מגבלותLIST הוא STACKהשימוש של •אפשר להוסיף רק לראש הרשימה–
–PUSH סוג של INSERT–POP סוג של REMOVE–PEEK דרך לראות את הערך בראש הרשימה – הדרך
היחידה לראות ערכים בלי להוריד אותם!
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
Atop
empty stack
top
top
top
push an element push another
A
B
pop
A
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
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 SubEnd Module
)עם להמציא מחדש את הגלגלפונקציות(
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 End Function
Function Peek(ByVal list As ArrayList) As Object Return list.Item(list.Count - 1) End Function....המשך
המשךלהמציא מחדש את הגלגל,
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
תורQueue
Queue ADTסוג אחר של מערך מוגבל•
מהיר מאוד, ולוקח מעט זיכרון
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()) Next
End SubEnd Module
תרגיל כיתה
אני מעונין לבנות מערכת לטפל בתהליך יצירת •הדוחות בתוך משרד
פשוט לדוחSTRUCTנבנה •LIFO וFIFOנסמלץ תהליכי עבודה •נבנה פונקציות להדפיס נתונים ולחפש נתונים• 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...") PrintStack(ListS) Console.WriteLine("And the Queue...") PrintQueue(ListQ) Console.WriteLine("And now...") PrintQueue(ListQ) Console.WriteLine("I found 0 in pos " & FindStack(ListS, 0)) Console.WriteLine("I found 0 in pos " & FindQueue(ListQ, 0)) End Sub
PrintQueue וPrintStackה 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 For i = 1 To times ' What happens without this??? temp = extra.Pop() a.Push(temp) Next 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 For i = 1 To times ' What happens without this??? temp = extra.Dequeue() a.Enqueue(temp) Next End Sub
FindQueue וFindStackה 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 For i = 1 To times temp = extra.Pop() a.Push(temp) Next Return -1 End Function
Function FindQueue(ByVal a As Queue, 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.Dequeue() count += 1 If temp.code = key Then Return count End If extra.Push(temp) Next For i = 1 To times temp = extra.Pop() a.Enqueue(temp) Next Return -1 End Function
)עם 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???End Function
Function Peek(ByVal list As ArrayList) As Object Return list.Item(0)End Function
תרגיל: לחשב מחיר על בסיסLIFO וגם FIFO
•QUEUEל FIFO •STACKל LIFO יש לבנות מבנה עם מחיר וכמות•QUEUE וSTACKיש להכניס ערכים לתוך •
–Push, Enqueueיש לחשב את המחיר לפי הפונקציות:•
–DEQUEUEל( QUEUE)–POPל( STACK)
??איך מתחיליםStructure Stock Dim Amount As Integer Dim Price As DecimalEnd 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 SubEnd Module