Stacks and Queues - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Stacks and Queues

Description:

Stacks and Queues Stack ADT LIFO LIFO (Last In, First Out) lists. – PowerPoint PPT presentation

Number of Views:174
Avg rating:3.0/5.0
Slides: 14
Provided by: AviR8
Category:
Tags: queues | stacks

less

Transcript and Presenter's Notes

Title: Stacks and Queues


1
Stacks and Queues
2
Stack ADT
  • ??? ?? ???? ?????
  • ??? ???? ??????? ??? ?????
  • ????? ?LIFO
  • LIFO (Last In, First Out) lists.
  • ???? ?????? ?? ???? ??????
  • PUSH
  • ???? ?????? ?? ???? ??????
  • POP

3
?????? ????????
  • ?????? ?? STACK ??? LIST ?? ??????
  • ???? ?????? ?? ???? ??????
  • PUSH ??? ?? INSERT
  • POP ??? ?? REMOVE
  • PEEK ??? ????? ?? ???? ???? ?????? ???? ??????
    ????? ????? ??? ?????? ????!

4
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
5
????? ?? 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
6
????? ?? 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
7
?????? ???? ?? ?????
Public Class CStack Private index
As Integer Private list As New
ArrayList() Public Sub New()
index -1 End Sub Public
Function Count() As Integer Return
list.Count() End Function Public
Sub Push(ByVal val As Object)
list.Add(val) index 1 End
Sub Public Function Pop() As Object
Dim obj As Object list.Item(index)
list.RemoveAt(index) index -
1 Return obj End Function
Public Function Peek() As Object
Return list.Item(index) End Function
End Class
8
????? ?MAIN (???? ???)
Sub Main() Dim test As New CStack()
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
9
Queue ADT
  • ??? ??? ?? ???? ?????
  • ??? ????, ??????? ??? ?????
  • ????? ?FIFO
  • FIFO (First In, First Out) lists.
  • ???? ?????? ?? ???? ??????
  • Enqueue
  • ???? ?????? ?? ????? ??????
  • Dequeue

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 Sub
11
????? ??? ????? QUEUE?
Public Class CStack Private index As
Integer Private list As New ArrayList()
Public Sub New() index -1
End Sub Public Function Count() As
Integer Return list.Count()
End Function Public Sub Enqueue(ByVal 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
12
????? ???? ???? ?? ???? LIFO ??? FIFO
  • QUEUE ?FIFO
  • STACK ?LIFO
  • ?? ????? ???? ?? ???? ?????
  • ????? ???? STACK ?QUEUE
  • ???? ?? ????? ??? ????????? DEQUEUE (?QUEUE) ?POP
    (?STACK)

13
??? ?????????
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
Write a Comment
User Comments (0)
About PowerShow.com