STACK: A Stack has a Last In First Out ( LIFO ) data structure, i.e., the last element to enter would be the first one to be processed.
Implementation of Stack in Visual basic.
Module Module1
Dim item As String
Dim top As Integer
Dim arraystack(10) As String
Sub Main()
Dim tempItem As String
Dim counter As Integer
Dim choice As Integer
createEmptyStack()
Do
Console.WriteLine("Select any option: ")
Console.WriteLine(" 1. Inserting items in Stack")
Console.WriteLine(" 2. Deleting an item in stack")
Console.WriteLine(" 3. Searching an item in stack")
Console.WriteLine(" 4. Making stack empty")
Console.WriteLine(" 5. Listing all the itms from stack")
Console.WriteLine(" 0. Exit")
Console.Write(" => ")
choice = Console.ReadLine()
Select Case choice
Case 1
Console.WriteLine()
Console.WriteLine(" Enter item: ")
Console.Write(" => ")
tempItem = Console.ReadLine()
insertItemStack(tempItem)
Console.Clear()
Case 2
removeItemStack()
Console.Clear()
Case 3
Console.Clear()
Console.Write(" Enter an item to be searched: ")
tempItem = Console.ReadLine()
findItem(tempItem)
Console.Clear()
Case 4
createEmptyStack()
Console.Clear()
Case 5
Console.Clear()
If top = 0 Then
Console.WriteLine(" Stack is empty")
Else
For counter = top To 1 Step -1
Console.WriteLine(arraystack(counter))
Next
End If
Console.ReadKey()
Console.Clear()
End Select
Loop Until choice = 0
End Sub
Sub createEmptyStack()
top = 0
End Sub
Sub insertItemStack(ByVal InsertItem As String)
If top = arraystack.Length() - 1 Then
Console.WriteLine("Stack is full")
Else
top = top + 1
arraystack(top) = InsertItem
Console.WriteLine(InsertItem & " inserted into sack")
Console.WriteLine()
Console.WriteLine(" Press any key to continue....")
End If
Console.ReadKey()
End Sub
Sub removeItemStack()
If top = 0 Then
Console.WriteLine("Stack is empty")
Else
item = arraystack(top)
top = top - 1
Console.WriteLine(item & " has been deleted")
Console.WriteLine()
Console.WriteLine(" Press any key to continue....")
End If
Console.ReadKey()
End Sub
Sub findItem(ByVal searchItem As String)
Dim count As Integer
For count = top To 1 Step -1
If arraystack(count) = searchItem Then
Console.WriteLine(searchItem & " is found at position " & count)
Console.WriteLine()
Console.WriteLine(" Press any key to continue....")
End If
Next
Console.ReadKey()
End Sub
End Module
- CIE A-LEVEL COMPUTER SCIENCE ( 9608 )
No comments:
Post a Comment
Comments here....