Stacks and Queues Push and Pop Primary operations

  • Slides: 13
Download presentation
Stacks and Queues

Stacks and Queues

Push and Pop • Primary operations: Push and Pop • Push – Add an

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 top top A push another B A pop top A

POP דוגמא של Module 1 Sub Main() Dim test As New Stack() Dim i

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

PEEK דוגמא של Module 1 Sub Main() Dim test As New Stack() Dim i

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

 להמציא מחדש את הגלגל Public Class CStack Private index As Integer Private list

להמציא מחדש את הגלגל Public Class CStack Private index As Integer Private list As New Array. List() Public Sub New() index = -1 End Sub Public Function Count() As Integer Return list. Count() End Function Public Sub Push(By. Val val As Object) list. Add(val) index += 1 End Sub Public Function Pop() As Object Dim obj As Object = list. Item(index) list. Remove. At(index) index -= 1 Return obj End Function Public Function Peek() As Object Return list. Item(index) End Function End Class

( )אותו דבר MAIN שימוש ב Sub Main() Dim test As New CStack() Dim

( )אותו דבר MAIN שימוש ב Sub Main() Dim test As New CStack() Dim i As Integer For i = 1 To 5 test. Push(i) Next Console. Write. Line(test. Count) For i = 1 To test. Count Dim num As Integer = test. Pop() Console. Write. Line(num) Next End Sub

 דוגמא Module 1 Sub Main() Dim queue As New Queue Dim i As

דוגמא Module 1 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. Write. Line(queue. Dequeue()) Next End Sub

? QUEUE איך בונים : תרגיל Public Class CStack Private index As Integer Private

? QUEUE איך בונים : תרגיל Public Class CStack Private index As Integer Private list As New Array. List() Public Sub New() index = -1 End Sub Public Function Count() As Integer Return list. Count() End Function Public Sub Enqueue(By. Val val As Object) ? ? End Sub Public Function Dequeue() As Object ? ? ? End Function Public Function Peek() As Object Return list. Item(0) End Function End Class

Structure Stock Dim Amount As Integer Dim Price As Decimal End Structure ? ?

Structure Stock Dim Amount As Integer Dim Price As Decimal End Structure ? ? איך מתחילים Module 1 Sub Main() Dim List 1 As New Queue() Dim List 2 As New Stack() Dim temp As Stock temp. Amount = 10 temp. Price = 5. 5 List 1. Enqueue(temp) List 2. Push(temp) temp. Amount = 50 temp. Price = 8. 5 List 1. Enqueue(temp) List 2. Push(temp) temp = List 1. Peek() Console. Write. Line("What's the cost? " & temp. Price) temp = List 2. Peek() Console. Write. Line("What's the cost? " & temp. Price) End Sub End Module