23
רררררר ררררStacks and Queues

מחסנית ותור

  • Upload
    andren

  • View
    36

  • Download
    1

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: מחסנית ותור

מחסנית ותור

Stacks and Queues

Page 2: מחסנית ותור

מחסניתStack

Page 3: מחסנית ותור

Stack ADTמחסנית - סוג של מערך מוגבל•

מהיר מאוד ותופס מעט זיכרון

LIFOשימוש ב•–LIFO (Last In, First Out) lists. הרשימהבסוףאפשר להוסיף רק –

•PUSH

הרשימהמסוףאפשר להוריד רק –•POP

• (ADT – Abstract Data Type)

Page 4: מחסנית ותור

:הפעולות הבסיסיות

עם מגבלותLIST הוא STACKהשימוש של •אפשר להוסיף רק לראש הרשימה–

–PUSH סוג של INSERT–POP סוג של REMOVE–PEEK דרך לראות את הערך בראש הרשימה – הדרך

היחידה לראות ערכים בלי להוריד אותם!

Page 5: מחסנית ותור

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

Page 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

Page 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 SubEnd Module

Page 8: מחסנית ותור

תורQueue

Page 9: מחסנית ותור

Queue ADTסוג אחר של מערך מוגבל•

מהיר מאוד, ולוקח מעט זיכרון

FIFOשימוש ב•–FIFO (First In, First Out) lists. הרשימהבסוףאפשר להוסיף רק –

•Enqueue

הרשימהמהתחלתאפשר להוריד רק –•Dequeue

Page 10: מחסנית ותור

דוגמא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

Page 11: מחסנית ותור

תרגיל כיתה

אני מעונין לבנות מערכת לטפל בתהליך יצירת •הדוחות בתוך משרד

פשוט לדוחSTRUCTנבנה •LIFO וFIFOנסמלץ תהליכי עבודה •נבנה פונקציות להדפיס נתונים ולחפש נתונים• STACKשימו לב: יש שינויים טכניים ולוגיים בין •

)כמו שנראה(...QUEUEו

Page 12: מחסנית ותור

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

Page 13: מחסנית ותור

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

Page 14: מחסנית ותור

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

Page 15: מחסנית ותור

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 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 For i = 1 To times temp = extra.Dequeue() a.Enqueue(temp) Next Return -1 End Function

מה קורה אם מצאנו, ואח"כ

?נחפש שוב

Page 16: מחסנית ותור

FindStackפתרון אפשרי עבור ,FindQueueFunction 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 Next For i = 1 To times temp = extra.Pop() a.Push(temp) Next Return -1End 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 temp = a.Dequeue() extra.Enqueue(temp) Next For j = 1 To times temp = extra.Dequeue() a.Enqueue(temp) Next Return (count) End If Next For i = 1 To times temp = extra.Dequeue() a.Enqueue(temp) Next Return -1End Function

Page 17: מחסנית ותור

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

Page 18: מחסנית ותור

להמציא מחדש את הגלגליצירת מחסנית )עם פונקציות(

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....המשך

Page 19: מחסנית ותור

להמציא מחדש את הגלגל - המשךמחסנית,

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

Page 20: מחסנית ותור

)עם 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

Page 21: מחסנית ותור

תרגיל: לחשב מחיר על בסיסLIFO וגם FIFO

•QUEUEל FIFO •STACKל LIFO יש לבנות מבנה עם מחיר וכמות•QUEUE וSTACKיש להכניס ערכים לתוך •

–Push, Enqueueיש לחשב את המחיר לפי הפונקציות:•

–DEQUEUEל( QUEUE)–POPל( STACK)

Page 22: מחסנית ותור

??איך מתחילים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

Page 23: מחסנית ותור

איך מתחילים?? אפשרות נוספתStructure Stock

Dim Price As DecimalEnd 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 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