1 / 23

מחסנית ותור

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

andren
Télécharger la présentation

מחסנית ותור

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 ModuleModule1 Sub Main() Dim test AsNewStack() DimiAsInteger Fori = 1 To 5 test.Push(i) Next Console.WriteLine(test.Count) Fori = 1 Totest.Count Dim num AsInteger = test.Pop() Console.WriteLine(num) Next EndSub EndModule

  7. דוגמא של PEEK ModuleModule1 Sub Main() Dim test AsNewStack() DimiAsInteger Fori = 1 To 5 test.Push(i) Next Console.WriteLine(test.Count) Fori = 1 Totest.Count Dim num AsInteger = test.Peek() Console.WriteLine(num) Next EndSub EndModule

  8. תורQueue

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

  10. דוגמא ModuleModule1 Sub Main() Dim queue AsNewQueue DimiAsInteger Fori = 1 To 5 queue.Enqueue(i) Next Fori = 1 Toqueue.Count Console.WriteLine(queue.Dequeue()) Next EndSub EndModule

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

  12. הSTRUCT StructureReport Dim code AsInteger' date type could be used instead... Dim Topic AsString Dim Approval AsBoolean Dim Content AsString EndStructure

  13. הMAIN Sub Main() DimListQAsNewQueue() DimListSAsNewStack() Dim temp AsReport Fori = 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)) EndSub

  14. הPrintStack וPrintQueue 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 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

  15. הצעה עבור FindStack וFindQueue FunctionFindQueue(ByVal a AsQueue, ByVal key AsInteger) AsInteger Dim extra AsNewQueue Dim times AsInteger = a.Count Dim temp AsNewReport Dim count AsInteger = 0 Fori = 1 To times temp = a.Dequeue() count += 1 Iftemp.code = key Then Return count EndIf extra.Enqueue(temp) Next Fori = 1 To times temp = extra.Dequeue() a.Enqueue(temp) Next Return -1 EndFunction Function FindStack(ByVala As Stack, ByValkey 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 Fori = 1 Totimes temp = a.Pop() count += 1 If temp.code = key Then Return count End If extra.Push(temp) Next Fori = 1 To times temp = extra.Pop() a.Push(temp) Next Return -1 End Function מה קורה אם מצאנו, ואח"כ נחפש שוב?

  16. פתרון אפשרי עבור FindStack וFindQueue FunctionFindQueue(ByVal a AsQueue, ByVal key AsInteger) AsInteger Dimextra AsNewQueue Dimtimes AsInteger = a.Count Dimtemp AsNewReport Dimcount AsInteger = 0 Fori = 1 To times temp = a.Dequeue() extra.Enqueue(temp) count += 1 Iftemp.code = key Then For j = i + 1 Totimes temp = a.Dequeue() extra.Enqueue(temp) Next For j = 1 To times temp = extra.Dequeue() a.Enqueue(temp) Next Return (count) EndIf Next Fori = 1 Totimes temp = extra.Dequeue() a.Enqueue(temp) Next Return -1 EndFunction FunctionFindStack(ByVal a AsStack, ByVal key AsInteger) AsInteger Dimextra AsNewStack Dimtimes AsInteger = a.Count Dimtemp AsNewReport Dimcount AsInteger = 0 Fori = 1 To times temp = a.Pop() extra.Push(temp) count += 1 Iftemp.code = key Then Forj = 1 To count temp = extra.Pop() a.Push(temp) Next Return(count) EndIf Next Fori = 1 Totimes temp = extra.Pop() a.Push(temp) Next Return -1 EndFunction

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

  18. להמציא מחדש את הגלגליצירת מחסנית(עם פונקציות) ModuleModule1 Function Count(ByVal list AsArrayList) AsInteger Returnlist.Count() EndFunction Sub Push(ByValvalAsObject, ByRef list AsArrayList) list.Add(val) EndSub Function Pop(ByVal list AsArrayList) AsObject Dim obj AsObject = list.Item(list.Count - 1) list.RemoveAt(list.Count - 1) Returnobj EndFunction Function Peek(ByVal list AsArrayList) AsObject Returnlist.Item(list.Count - 1) EndFunction המשך....

  19. להמציא מחדש את הגלגל - מחסנית, המשך Sub Main() Dim test AsNewArrayList() DimiAsInteger Fori = 0 To 4 Push(i, test) Next Console.WriteLine(Count(test)) Fori = 0 Totest.Count - 1 Dim num AsInteger = Pop(test) Console.WriteLine(num) Next EndSub EndModule

  20. תרגיל: איך בונים QUEUE (עם פונקציות)? • Function Count(ByVal list AsArrayList) AsInteger • ReturnList.Count() • EndFunction • SubEnqueue(ByValvalAsObject, ByRef list AsArrayList) • ??? • EndSub • FunctionDequeue(ByVal list AsArrayList) AsObject • ??? • EndFunction • Function Peek(ByVal list AsArrayList) AsObject • Returnlist.Item(0) • EndFunction

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

  22. איך מתחילים?? StructureStock Dim Amount AsInteger Dim Price AsDecimal EndStructure ModuleModule1 Sub Main() Dim List1 AsNewQueue() Dim List2 AsNewStack() Dim temp AsStock 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) EndSub EndModule

  23. איך מתחילים?? אפשרות נוספת StructureStock Dim Price AsDecimal EndStructure ModuleModule1 Sub Main() Dim List1 AsNewQueue() Dim List2 AsNewStack() Dim temp AsStock temp.Price = 5.5 List1.Enqueue(temp) List2.Push(temp) 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) EndSub EndModule

More Related