Friday, April 7, 2017

Queue: Program code ( visual basic )

Computer Science(9608) notes and Resources




Queue: A Queue has a First in First Out ( FIFO ) data structure, i.e, the first element to enter would be the first one to be processed.




Implementation of Queue in Visual basic.





Module Module1

    Dim ListQueue(4) As String
    Dim FrontOfQueuePointer, EndOfQueuePointer, count As Integer


    Sub Main()
        createEmptyQueue()
        insertIntoQueue("dog")
        insertIntoQueue("cat")
        insertIntoQueue("elephant")
        insertIntoQueue("lion")
        Console.ReadLine()

        Console.WriteLine(" Items in queue: ")

        For count = EndOfQueuePointer To FrontOfQueuePointer Step -1
            Console.WriteLine(ListQueue(count))
        Next

        Console.ReadLine()


        Console.WriteLine()
        Console.WriteLine(" Remove one item form Queue:")
        Console.WriteLine()
        removeFromQueue()
        Console.WriteLine()
        Console.ReadLine()


        Console.WriteLine(" Insert an item into Queue:")
        insertIntoQueue("tiger")
        Console.WriteLine()
        Console.ReadLine()


        Console.WriteLine(" Search an item in queue: ")
        findItem("cat")
        Console.WriteLine()
        Console.ReadLine()


        Console.WriteLine(" Items in queue: ")
        For count = EndOfQueuePointer To FrontOfQueuePointer Step -1
            Console.WriteLine(ListQueue(count))
        Next
        Console.ReadLine()

    End Sub




    Sub createEmptyQueue()
        FrontOfQueuePointer = 0
        EndOfQueuePointer = 0
    End Sub




    Sub insertIntoQueue(ByVal item As String)
        If EndOfQueuePointer = ListQueue.Length() - 1 Then
            Console.WriteLine()
            Console.WriteLine("Overflow: Can't insert item, queue is full")
            Console.WriteLine(item & " is not inserted into Queue")
        Else
            If FrontOfQueuePointer = 0 And EndOfQueuePointer = 0 Then
                EndOfQueuePointer = 1
                FrontOfQueuePointer = 1
            Else
                EndOfQueuePointer = EndOfQueuePointer + 1
            End If
            ListQueue(EndOfQueuePointer) = item
            Console.WriteLine(item & " has been inserted into Queue")
        End If
    End Sub



    Sub removeFromQueue()
        Dim item As String
        If FrontOfQueuePointer = 0 Then
            Console.WriteLine("Underflow: Can't delete, queue is empty")
        Else
            item = ListQueue(FrontOfQueuePointer)
            If FrontOfQueuePointer = EndOfQueuePointer Then
                FrontOfQueuePointer = 0
                EndOfQueuePointer = 0
            Else
                FrontOfQueuePointer = FrontOfQueuePointer + 1
            End If
            Console.WriteLine(item & " has been deleted")
        End If
    End Sub



    Sub findItem(ByVal paraSearchItem As String)
        For count = EndOfQueuePointer To FrontOfQueuePointer Step -1
            If ListQueue(count) = paraSearchItem Then
                Console.WriteLine(paraSearchItem & " is found at position: " & count)
            End If
        Next
    End Sub
End Module



                                                                                                         - CIE A-LEVEL COMPUTER SCIENCE (  9608 )


No comments:

Post a Comment

Comments here....