0% found this document useful (0 votes)
18 views5 pages

DSA Lab

The document provides instructions and questions for a coding assignment. For question 1, students are asked to write a program to convert a decimal number to binary and octal representations using a stack. For question 2, students must take a string as input, split it into individual words, create queues for each word, enqueue the words to the queues, then concatenate all the queues and display the output. Question 3 asks students to list 3 real-world applications of stack and queue data structures. The instructions state that students must submit their code, output and answers in a word file and that cheating will result in lost marks.

Uploaded by

Daud Sajid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views5 pages

DSA Lab

The document provides instructions and questions for a coding assignment. For question 1, students are asked to write a program to convert a decimal number to binary and octal representations using a stack. For question 2, students must take a string as input, split it into individual words, create queues for each word, enqueue the words to the queues, then concatenate all the queues and display the output. Question 3 asks students to list 3 real-world applications of stack and queue data structures. The instructions state that students must submit their code, output and answers in a word file and that cheating will result in lost marks.

Uploaded by

Daud Sajid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Lab DSA 28th April, 2023

Instructions: make a folder with your name and take screenshots of your code
and output and paste it on a word file. If anyone caught with cheating
material or talking with your partner will get deduction in total marks.
Q1. Write a program using stack operations, which accepts a non-negative base 10
as a parameter, and display binary representation of a number. (10 marks)
Ans:
import java.util.*;

public class DecimalToBinaryAndOctal {

// Method to convert decimal to binary using a stack


public static String decimalToBinary(int decimal) {
Stack<Integer> stack = new Stack<Integer>();

// Convert decimal to binary using division by 2 and push the remainder onto the stack
while (decimal > 0) {
int remainder = decimal % 2;
stack.push(remainder);
decimal /= 2;
}

// Pop the stack and append the elements to a string to get the binary representation
StringBuilder binary = new StringBuilder();
while (!stack.isEmpty()) {
binary.append(stack.pop());
}

return binary.toString();
}

// Method to convert decimal to octal using a stack


public static String decimalToOctal(int decimal) {
Stack<Integer> stack = new Stack<Integer>();

// Convert decimal to octal using division by 8 and push the remainder onto the stack
while (decimal > 0) {
int remainder = decimal % 8;
stack.push(remainder);
decimal /= 8;
}
// Pop the stack and append the elements to a string to get the octal representation
StringBuilder octal = new StringBuilder();
while (!stack.isEmpty()) {
octal.append(stack.pop());
}

return octal.toString();
}

public static void main(String[] args) {


// Accept a non-negative base 10 number as a parameter
Scanner input = new Scanner(System.in);
System.out.print("Enter a non-negative decimal number: ");
int decimal = input.nextInt();

// Convert decimal to binary and octal using stack operations and display the result
System.out.println("Binary representation: " + decimalToBinary(decimal));
System.out.println("Octal representation: " + decimalToOctal(decimal));
}
}
Screenshot:
Q2. Take a single string as input. Using this input string, you have to create
multiple queries in which each queue will comprise of separate word appeared in
input string. At the end, you will again concatenate all queries to a single queue. At
the end concatenate all queues and display them. (10 marks)
Ans:
import java.util.Arrays;
import java.util.Scanner;

public class QueueConcatination {


String[] queueArray;
int queueSize;
int front, rear, numberOfItems = 0;

public QueueConcatination(int size) {


queueSize = size;
queueArray = new String[size];
front = rear = 0;
}

public void enqueue(String input) {


if (numberOfItems + 1 <= queueSize) {
queueArray[rear] = input;
rear++;
numberOfItems++;
System.out.println("ENQUEUE " + input + " was added to the Queue");
} else {
System.out.println("QUEUE is full");
}
}

public String dequeue() {


if (numberOfItems > 0) {
String dequeuedItem = queueArray[front];
front++;
numberOfItems--;
System.out.println("DEQUEUE " + dequeuedItem + " was removed from the Queue");
return dequeuedItem;
} else {
System.out.println("QUEUE is empty");
return "-1";
}

}
public static void main(String[] args) {
Scanner Scan = new Scanner(System.in);System.out.print("Input the String: ");
String input = Scan.nextLine();
String[] words = input.split("\\s+");
QueueConcatination[] queues = new QueueConcatination[words.length];

for (int i = 0; i < words.length; i++) {


queues[i] = new QueueConcatination(words.length);
String word = words[i];
queues[i].enqueue(word);
}

QueueConcatination resultQueue = new QueueConcatination(words.length * words.length);

for (int i = 0; i < words.length; i++) {


QueueConcatination currentQueue = queues[i];
while (currentQueue.numberOfItems > 0) {
String word = currentQueue.dequeue();
resultQueue.enqueue(word);
}
}

System.out.println("\nAll Queues concatenated:");


while (resultQueue.numberOfItems > 0) {
String word = resultQueue.dequeue();
System.out.print(word + " ");
}
}
}

Q3. Write down any 3 real world applications of stack and queue data structures?
(5 marks)
Ans:

1: Web browsing history:

2: Call center support:

3: Text editor undo/redo functionality

You might also like