Essays
Topics
Writing Tool
Machine Learning AI
ChatGPT
US History
Presidents of the United States
Joseph Robinette Biden
Donald Trump
Barack Obama
US States
States Ranked by Size & Population
States Ranked by Date
IPL
>
Accounting
>
Essential Java Stack and Queue Exercises for Beginners
Essential Java Stack and Queue Exercises for Beginners
School
California State University, Sacramento
*
*We aren't endorsed by this school
Course
CSC 20
Subject
Accounting
Date
Dec 11, 2024
Pages
10
Uploaded by AdmiralPenguinMaster928
Amina Barry
BJP5 Self-Check 14.13:
Output:
you, are, how
14.14:
Output
: 10
7
5
false
2
3
8
3
14.15:
Output:
2
10
10
4
6
6
3
14.16:
Output:
14.17:
Output:
1, 3, 5, 2, 4, 6
[-3, 15, 9, 71] [42, 4]
[3] [30, 20, 10, 60, 50 40, 0]
BJP5 Exercise 14.1:
public void splitStack(Stack<Integer> s) {
Queue<Integer> q = new LinkedList<Integer>();
int numNegatives = 0;
while(!s.isEmpty()) {
if(s.peek() < 0)
numNegatives++;
q.add(s.pop());
}
while(numNegatives > 0) {
if(q.peek() < 0) {
s.push(q.remove());
numNegatives--;
} else {
q.add(q.remove());
}
}
while(!q.isEmpty())
s.push(q.remove());
}
14.2:
public void stutter(Stack<Integer> s) {
Queue<Integer> q = new LinkedList<Integer>();
while(!s.isEmpty())
q.add(s.pop());
while(!q.isEmpty())
s.push(q.remove());
while(!s.isEmpty())
q.add(s.pop());
while(!q.isEmpty()) {
int n = q.remove();
s.push(n);
s.push(n);
}
}
14.3:
public Stack<Integer> copyStack(Stack<Integer> s1) {
Stack<Integer> s2 = new Stack<Integer>();
Queue<Integer> q = new LinkedList<Integer>();
while(!s1.isEmpty())
s2.push(s1.pop());
while(!s2.isEmpty())
q.add(s2.pop());
while(!q.isEmpty()) {
int n = q.remove();
s1.push(n);
s2.push(n);
}
return s2;
}
14.4:
public static void collapse(Stack<Integer> s) {
Queue<Integer> q = new LinkedList<Integer>();
if (s.size() % 2 != 0) {
q.add(s.pop());
}
while (!s.isEmpty()) {
q.add(s.pop() + s.pop());
}
while (!q.isEmpty()) {
s.push(q.remove());
}
while (!s.isEmpty()) {
q.add(s.pop());
}
while (!q.isEmpty()) {
s.push(q.remove());
}
}
14.5:
public boolean equals(Stack<Integer> s1, Stack<Integer> s2) {
int size1 = s1.size();
int size2 = s2.size();
if(size1 != size2)
return false;
Stack<Integer> s3 = new Stack<Integer>();
boolean areEqual = true;
while(!s1.isEmpty()) {
int n1 = s1.pop();
int n2 = s2.pop();
s3.push(n1);
s3.push(n2);
if(n1 != n2) {
areEqual = false;
break;
}
}
while(!s3.isEmpty()) {
s2.push(s3.pop());
s1.push(s3.pop());
}
return areEqual;
}
14.6:
public static void rearrange(Queue<Integer> q) {
Stack<Integer> s = new Stack<Integer>();
int oldSize = q.size();
for (int i = 0; i < oldSize; i++) {
int num = q.remove();
if (num % 2 == 0) {
s.push(num);
} else {
q.add(num);
}
}
for (int i = 0; i < 2; i++) {
while (!q.isEmpty()) {
s.push(q.remove());
}
while (!s.isEmpty()) {
q.add(s.pop());
}
}
}
14.7:
public void reverseHalf(Queue<Integer> q) {
Stack<Integer> s = new Stack<Integer>();
int size = q.size();
for(int i = 0; i < size; i++) {
if(i % 2 == 1)
s.push(q.remove());
else
q.add(q.remove());
}
while(!s.isEmpty()) {
q.add(q.remove());
q.add(s.pop());
}
if(size % 2 == 1)
q.add(q.remove());
}
14.8:
public static boolean isPalindrome(Queue<Integer> q) {
Stack<Integer> s = new Stack<Integer>();
int oldSize = q.size();
for (int i = 0; i < oldSize; i++) {
int num = q.remove();
q.add(num);
s.push(num);
}
boolean same = true;
for (int i = 0; i < oldSize; i++) {
int num = q.remove();
if (num != s.pop()) {
same = false;
}
q.add(num);
}
return same;
}
14.9:
public static void switchPairs(Stack<Integer> s) {
Queue<Integer> q = new LinkedList<Integer>();
if (s.size() % 2 != 0) {
q.add(s.pop());
}
while (!s.isEmpty()) {
int num1 = s.pop();
int num2 = s.pop();
q.add(num2);
q.add(num1);
}
while (!q.isEmpty()) {
s.push(q.remove());
}
while (!s.isEmpty()) {
q.add(s.pop());
}
while (!q.isEmpty()) {
s.push(q.remove());
}
}
14.10:
public static boolean isConsecutive(Stack<Integer> s) {
Queue<Integer> q = new LinkedList<Integer>();
if (s.size() < 2) {
return false;
}
while (!s.isEmpty()) {
q.add(s.pop());
}
boolean consecutive = true;
s.push(q.remove());
while (!q.isEmpty()) {
if (s.peek() - 1 != q.peek()) {
consecutive = false;
}
s.push(q.remove());
}
while (!s.isEmpty()) {
q.add(s.pop());
}
while (!q.isEmpty()) {
s.push(q.remove());
}
return consecutive;
}