0% found this document useful (0 votes)
26 views9 pages

Queue and Stack Problem Solutions

The document contains multiple Java programs that implement various data structures and algorithms using queues and stacks. Each problem demonstrates different functionalities such as sorting based on rank, replacing stack elements based on digit sums, matching content from two queues, and processing prime factors. The programs also include operations for summing pairs, arranging elements based on specific criteria, and handling input and output for different data types.

Uploaded by

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

Queue and Stack Problem Solutions

The document contains multiple Java programs that implement various data structures and algorithms using queues and stacks. Each problem demonstrates different functionalities such as sorting based on rank, replacing stack elements based on digit sums, matching content from two queues, and processing prime factors. The programs also include operations for summing pairs, arranging elements based on specific criteria, and handling input and output for different data types.

Uploaded by

datascience9029
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

//Problem 1

public class Problem1 {


public static void main(String[] args) {
Queue instrqueue = new Queue(4);
[Link]("Naveen:3");
[Link]("Raju:2");
[Link]("Shanu:2");
[Link]("Radha:1");
[Link]("Before changing based on the rank ");
[Link]();
Queue outstrqueue = displayBasedOnRank(instrqueue);
[Link]("After changing based on the rank ");
[Link]();
}

public static Queue displayBasedOnRank(Queue instrqueue) {


Queue result = new Queue(4);

String[] arrvalues = new String[[Link]()];


int[] rankarray = new int[[Link]()];

for (int i = 0; i < [Link](); i++) {


String name = [Link]();
arrvalues[i] = name;
int ranklast = [Link]("" + [Link]([Link]() -
1));
rankarray[i] = ranklast;
}
[Link](rankarray);

for (int i : rankarray) {


String name= "";
for (int j = 0; j <=[Link]-1; j++) {
name = arrvalues[j] ;

char ch = [Link]([Link]()-1) ;
int n = [Link](ch+ "");
//[Link](n);
if(i == n )
{
[Link](arrvalues[j]);
break;
}
}
}
return result;

// Problem 2

public class Problem2 {


public static void main(String[] args) {
Stack values = new Stack(5);
[Link](81);
[Link](29);
[Link](12);
[Link](56);
[Link](34);
[Link]("Before");
[Link]();
Stack result = replaceSTack(values);
[Link]();
[Link]("After");
[Link]();

public static Stack replaceSTack(Stack data) {

Stack values = new Stack([Link]());


Stack result = new Stack([Link]());
Stack odd = new Stack([Link]());
Stack even = new Stack([Link]());
while (![Link]()) {
int n = [Link]();
int sum = sumOfDigits(n);
if (sum % 2 == 0) {
[Link](n);

} else {
[Link](n);
}
}

while (![Link]()) {
int n = [Link]();
[Link](n);
}
while (![Link]()) {
int n = [Link]();
[Link](n);
}
while (![Link]()) {
int n = [Link]();
[Link](n);
}

return result;

}
}

//Problem3

public class Problem3 {


public static void main(String[] args) {
Queue intstrqueue1 = new Queue(10);
[Link]("Laptop:Electronics");
[Link]("Apple:Fruit");
[Link]("Shampoo:Toilery");
Queue intstrqueue2 = new Queue(10);
[Link]("tablet:Electronics");
[Link]("Banana:Fruit");
[Link]("Soap:Toilery");
Queue outputqueue = matchCotnet(intstrqueue1, intstrqueue2);
[Link]();

public static Queue matchCotnet(Queue intstrqueue1 , Queue intstrqueue2) {


Queue result = new Queue(10);

while( ![Link]()) {
String s= [Link]();
String name1=dataString(s);

String s2= [Link]();


String name2=dataString(s2);

String res= name1 +"-"+ name2 ;


[Link](res);

}
return result;

}
public static String dataString(String s ) {
String name = "";
for (int i = 0; i <=[Link]()-1; i++) {
String ch = [Link](i)+"";
if([Link](i) == ':') {
break;
}
else {
name =[Link](ch+"");
}
}

return name;

//Problem 4
for this question output they given and my output is different

public class Problem4 {

public static void main(String[] args) {


Stack values = new Stack(5);
[Link](12);
[Link](15);
[Link](9);
[Link](12);
[Link](21);
Stack outputStack = primeFactors(values);
[Link]();
}

public static Stack primeFactors(Stack stack) {


Stack temp1 = new Stack(5);
Stack temp2 = new Stack(5);
Stack result = new Stack(5);
while (![Link]()) {
int n = [Link]();
int count = 0;
for (int i = 1; i <=n ; i++) {

if (n % i == 0) {
int countprime =0;
for (int j = 1; j <=i; j++) {
if (i % j == 0) {
countprime++;
}
}
if (countprime == 2) {
count++;
//[Link](i +" " + n + " "+
count);

}
}

}
//[Link](count + " "+n);
if (count % 2 == 0) {
// [Link](n);
[Link](n);
} else {
[Link](n);
}

}
//[Link]();
while (![Link]()) {
[Link]([Link]());
}
while (![Link]()) {
[Link]([Link]());
}
return result;

//problem 5

public class Problem5 {


public static void main(String[] args) {

Queue values = new Queue(5);

[Link](2);
[Link](7);
[Link](5);
[Link](10);
Queue values2 = new Queue(5);

[Link](11);
[Link](1);
[Link](8);

Queue resQueue = sumofprimePairs(values, values2);


[Link]();

public static Queue sumofprimePairs(Queue que1, Queue que2) {


Queue result = new Queue([Link]() + [Link]());

while (![Link]()) {
int n1 = [Link]();
if(! [Link]()) {
int n2 = [Link]();
int sum = n1 + n2;
int count = 0;
for (int i = 2; i <= sum / 2; i++) {
if (sum % i == 0) {
count++;
}
}
if (count == 0) {
[Link](n1);
[Link](n2);
}
}
}

return result;

//problem 6

public class Problem6 {


public static void main(String[] args) {
Stack values = new Stack(5);
[Link](4);
[Link](8);
[Link](9);
//[Link](10);

Queue values2 = new Queue(5);

[Link](2);
[Link](4);
[Link](16);
//[Link](16);

Queue resQueue = sumofprimePairs(values, values2);


//[Link]();
}
public static Queue sumofprimePairs(Stack stack, Queue que2) {
Queue result = new Queue([Link]() + [Link]());

while (![Link]()) {
int n1 = [Link]();
int resvaluestack = n1 * n1;
if(! [Link]()) {
int n2 = [Link]();
int resvaluequeue = n2*n2*n2;
if(resvaluestack == resvaluequeue) {
[Link](n1);
[Link](n2);
}

}
}
while (![Link]()) {
[Link]([Link]());

}
return result;

//problem 7

////7th problem
public class ExamExample {
public static void main(String[] args) {
ExamExample [] classNames = new ExamExample[5];
Stack intstrstack = new Stack(10);

[Link]("b");
[Link]("Z");
[Link]("5");

Queue intstrqueue = new Queue(10);


[Link]("ab5c");
[Link]("2");
[Link]("Qwd4zs");
[Link]("4");
[Link]("bige");
[Link]("0");
Queue result = resultData(intstrstack, intstrqueue);
[Link]();

}
public static Queue resultData(Stack stack , Queue queue) {

Queue valuesResult = new Queue([Link]()) ;

while ( ![Link]()) {
String s= [Link]();
String name = [Link]();
int num=[Link]([Link]()) ;
String s1 = [Link](num)+ "";
if([Link](s1)) {
[Link](s);
}else {
[Link]("-1");
}
}
return valuesResult;
}
}

problem 8

----->Problem 8 need to do

problem 10
public class Problem10 {
public static void main(String[] args) {

Queue values = new Queue(10);

[Link](2);
[Link](7);
[Link](4);
[Link](9);
[Link](5);
[Link](2);
[Link](10);

Queue values2 = new Queue(10);

[Link](3);
[Link](6);
// [Link](5);
// [Link](2);
// [Link](10);

Queue resQueue = sumofprimePairs(values, values2);


[Link]();
}
public static Queue sumofprimePairs(Queue que1, Queue que2) {
Queue result = new Queue([Link]() + [Link]());
while(![Link]() || ![Link]()) {
int n =0;
int n1 =0;
if(![Link]()) {
n = [Link]();
// [Link](n);
}
if(![Link]()) {
n1 = [Link]();
}

if(n !=0 && n1 !=0) {


if(n %2 == 0) {
[Link](n+n1);
}
else {
[Link]( n-n1);
}
}
else {
int val = n > n1 ? n : n1;
[Link](val);
}
}
return result;
}

//problem 11

public class Problem11 {


public static void main(String[] args) {
//Queue intstrqueue1 = new Queue(10);
Stack intstrstack = new Stack(10);

[Link]("Alice:D1");
[Link]("Bob02");
[Link]("Eve:D3");
[Link]("Oscar:D1");
[Link]("Charlie02");
Queue outqueue = vowelArrange(intstrstack);

}
public static Queue vowelArrange(Stack intstrstack ) {
Queue temp1 = new Queue(10);
Queue temp2 = new Queue(10);
Queue result = new Queue(10);

while(! [Link]()) {
String name = [Link]();
String ch = [Link](0)+"";
if([Link]("a") || [Link]("e") ||
[Link]("i") ||
[Link]("o") ||
[Link]("u")) {
[Link](name);

}
else {
[Link](name);
}
}

while(! [Link]()) {
String name = [Link]();
[Link](name);
[Link](name);

}
while(! [Link]()) {
String name = [Link]();
[Link](name);

}
[Link]();
return null;

// problem 12
public class Poblem12 {
public static void main(String[] args) {
//Queue intstrqueue1 = new Queue(10);
Stack intstrstack = new Stack(10);

[Link]("Laptop-A");
[Link]("Chair-B");
[Link]("Book-C");
[Link]("Phone-A");
[Link]("Table-B");
Queue outqueue = vowelArrange(intstrstack);
[Link]();

}
public static Queue vowelArrange(Stack intstrstack ) {
Queue temp1 = new Queue(10);
Queue temp2 = new Queue(10);
Queue temp3 = new Queue(10);
Queue result = new Queue(10);

while(! [Link]()) {
String name = [Link]();
String ch = [Link]([Link]()-1)+"";
if([Link]("A")) {
[Link](name);
}
else if([Link]("B")) {
[Link](name);
}
else if([Link]("C")) {
[Link](name);
}

while(! [Link]()) {
String name = [Link]();
[Link](name);

}
while(! [Link]()) {
String name = [Link]();
[Link](name);

}
while(! [Link]()) {
String name = [Link]();
[Link](name);

return result;

Common questions

Powered by AI

The method 'sumofprimePairs' operates by dequeuing numbers from two queues and summing them. It checks if the sum is prime by seeing if it is not divisible by any number up to half its value. If the sum is prime (meaning the count of divisors is zero), both numbers are retained and enqueued into the result queue. This logic ensures that only pairs forming a prime sum are kept .

The 'primeFactors' function involves popping each number from the input stack and determining its prime factors' count. For each number, its divisors are checked, and if a divisor is prime, the count is incremented. Based on whether the count of prime factors is even or odd, each number is placed into one of the two stacks ('temp1' for odd counts, 'temp2' for even counts). These stacks are then combined by moving their contents into the result stack after their figures have been segregated and analyzed, with an order that biases odd prime factor counts before even ones .

In Problem 12's 'vowelArrange', elements are categorized based on the character following a dash at the end of each string from the stack. This character acts as a category identifier ('A', 'B', or 'C'), directing the element into corresponding temporary queues ('temp1', 'temp2', or 'temp3'). The elements are subsequently collected in the result queue in the sequence of category 'A' first, followed by 'B' and 'C', thus employing organized categorization for systematic enqueuing .

In 'vowelArrange', strings beginning with a vowel are isolated into a temporary queue, 'temp1', while others go into 'temp2'. After categorizing all elements, 'temp1' elements are sequentially enqueued into the final result queue, followed by enqueuing elements from 'temp2'. This results in an arrangement where strings starting with vowels are prioritized over those that do not .

The alignment in 'matchCotnet' is achieved by aligning categories between elements from two queues, based on the name preceding the ':' in each entry. The implication for the resulting queue is a composite output where each new entry is a concatenation of two aligned entities from separate queues, thus maintaining relational semantics in the form of concatenated entries showing category alignment .

The method 'displayBasedOnRank' processes elements by dequeuing each from the input queue and extracting a rank located at the end of each string element. These ranks, represented as integers, are stored in an array and sorted in ascending order. For each integer in the sorted ranks array, the corresponding string from the original input, having an integer at its end that matches the sorted integer, is enqueued into the result queue. This reordering based on rank allows the queue to be organized according to the numerical value attached to each element initially .

The 'sumofprimePairs' algorithm in Problem 10 processes two queues simultaneously, offering an aggregated result based on presence and parity operations. When one of the queues is exhausted, the remaining values are appraised based on their parity, with even remaining values enqueued directly after summation to any lingering second queue elements (or standalone if unpaired), and odd each undergoes subtraction prior to storage, ensuring continuity in computation regardless of queue insufficiency .

In 'replaceSTack', each element from the provided stack is evaluated by summing its digits. If the sum is even, the element is pushed onto an 'even' stack; otherwise, it is pushed onto an 'odd' stack. Afterward, elements from the 'odd' stack are moved into a merged stack first, followed by elements from the 'even' stack, resulting in a rearranged stack where elements with odd sums precede those with even sums .

In 'resultData', each character element from the stack is compared to a specific character at a designated index within a string dequeued from the queue. If the characters match, that character is enqueued into the result queue; if not, a placeholder ('-1') is enqueued. This conditional operation ensures that only verified matches are preserved while standard placeholders denote failed matches .

Elements from two queues in Problem 3 are matched by dequeuing and processing corresponding entities from each queue separately. For each element, the name (before the colon ':' character) is extracted from both queues. These are then concatenated, separated by a hyphen, and enqueued into the result queue. This results in paired elements linked by their categorizations, creating a mapped relation between objects of different queues .

You might also like