Question
I had a few questions from my computer science homework. The questions deal with finding the content of a list after a section of code
I had a few questions from my computer science homework. The questions deal with finding the content of a list after a section of code is executed:
1. Given a singly linked list simply shown as 10->13->20->14->17->8->5. with head refers to the first node (contains 10 ). Show the list contents at check point 1, 2, and 3, respectively. (simply use item->next item format to show the list content and head by default refers to the first node here)
prve=head.next;
head=prev;
curr=head.next;
Check Point 1
while(curr.next!=null){
if(prev.item%2==0) prev.item++;
else curr.item++;
prev=curr;
curr=curr.next;
}
Check Point 2
curr=head;
head=prev;
curr.next=null;
head.next.next=curr;
Check Point 3
2. Given two empty queue Q1, Q2 and an empty stack S, show the content in Q1 (clearly indicate the front and back) and S (clearly indicate the top direction) at check point 1 and 2, respectively.
int[] array= {8, 13, 10, 14, 17, 16, 23, 19, 14, 25};
for(int i=0;i
{
if(array[i]%2==0) Q1.enqueue(array[i]);
else S.push(array[i]);
}
CHECK POINT 1
while(!S.isEmpty())
{
if(S.peek()%2==0) Q1.enqueue(S.pop());
else Q2.enqueue(S.pop());
}
while(!Q2.isEmpty())
{
if(Q2.peek()%2!=0) S.push(Q2.dequeue());
Q2.dequeue();
}
CHECK POINT 2
3. Given int[] array={3, 5, 6, 4, 8, 10}. Show the content in the array after the following operations are executed. Assume Q is an empty queue and S is an emprt Satck in the beginning.
for(int i=0;i
Q.enqueue(array[i]);
while(Q.isEmpty()==false) S.push(Q.dequeue());
int i=0;
while(S.isEmpty()==false) {
array[i]=S.pop();
i++;
}
I think I have the first one right but I just wanted to check on that one and I need help with the other ones.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started