kotlinCopy code
import java.util.*
fun main() {
val scanner = Scanner(System.`in`)
// Creating a stack and a queue
val stack = Stack<String>()
val queue = LinkedList<String>()
// Taking input from the user
println("Enter the names of 10 students and their corresponding classes:")
for (i in 1..10) {
print("Student $i name: ")
val name = scanner.nextLine()
print("Student $i class: ")
val className = scanner.nextLine()
// Pushing the student name to the stack
stack.push(name)
// Enqueueing the student class to the queue
queue.offer(className)
}
// Processing the input using stacks and queues
println("\nProcessing the input using stacks and queues:")
println("==========================================")
println("Processing student names from the stack:")
while (!stack.isEmpty()) {
val name = stack.pop()
println("Name: $name")
}
println("\nProcessing student classes from the queue:")
while (!queue.isEmpty()) {
val className = queue.poll()
println("Class: $className")
}
}
kotlinCopy code
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var outputTextView: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Initialize the outputTextView
outputTextView = findViewById(R.id.outputTextView)
// Process the input and get the processed output from DSA techniques
val processedOutput = getProcessedOutput()
// Format the output data appropriately for display
val formattedOutput = formatOutput(processedOutput)
// Update the UI elements with the final output
updateUI(formattedOutput)
}
private fun getProcessedOutput(): String {
// Perform the necessary DSA techniques to process the input
// Replace this with your own logic to process the input and get the output
val processedOutput = "Processed output"
return processedOutput
}
private fun formatOutput(output: String): String {
// Format the output data as per your requirements
// You can apply any necessary formatting or transformations here
// For example, you can add line breaks or additional text to the output
val formattedOutput = "Formatted output: $output"
return formattedOutput
}
private fun updateUI(output: String) {
// Update the UI elements with the final output
outputTextView.text = output
}
}
kotlinCopy code
import java.util.*
fun main() {
val scanner = Scanner(System.`in`)
// Taking input from the user
println("Enter a sorted array of numbers (space-separated):")
val input = scanner.nextLine().trim().split(" ").map { it.toInt() }.toIntArray()
println("Enter the target number to search:")
val target = scanner.nextInt()
// Applying binary search
val index = binarySearch(input, target)
// Displaying the result
if (index != -1) {
println("Target number $target found at index $index")
} else {
println("Target number $target not found in the array")
}
}
fun binarySearch(arr: IntArray, target: Int): Int {
var left = 0
var right = arr.size - 1
while (left <= right) {
val mid = left + (right - left) / 2
when {
arr[mid] == target -> return mid
arr[mid] < target -> left = mid + 1
else -> right = mid - 1
}
}
return -1
}
kotlinCopy code
import java.util.*
class Graph(private val numVertices: Int) {
private val adjacencyList: Array<LinkedList<Int>> = Array(numVertices) { LinkedList<Int>() }
fun addEdge(source: Int, destination: Int) {
adjacencyList[source].add(destination)
}
fun dfs(startVertex: Int) {
val visited = BooleanArray(numVertices)
dfsUtil(startVertex, visited)
}
private fun dfsUtil(vertex: Int, visited: BooleanArray) {
visited[vertex] = true
print("$vertex ")
val neighbors = adjacencyList[vertex]
for (neighbor in neighbors) {
if (!visited[neighbor]) {
dfsUtil(neighbor, visited)
}
}
}
}
fun main() {
val scanner = Scanner(System.`in`)
// Taking input from the user
println("Enter the number of vertices in the graph:")
val numVertices = scanner.nextInt()
val graph = Graph(numVertices)
println("Enter the number of edges in the graph:")
val numEdges = scanner.nextInt()
println("Enter the edges (source and destination vertices):")
repeat(numEdges) {
val source = scanner.nextInt()
val destination = scanner.nextInt()
graph.addEdge(source, destination)
}
println("Enter the starting vertex for DFS traversal:")
val startVertex = scanner.nextInt()
// Applying Depth-First Search (DFS)
println("DFS traversal starting from vertex $startVertex:")
graph.dfs(startVertex)
}