(TRIO) Adi, Akhil, Tristan
This Java code defines a class called Main
with two methods: ChallengeOne()
and main()
. The ChallengeOne()
method uses a Queue
implementation provided by the LinkedList
class to demonstrate how a queue works. Here are the main points of the code:
ChallengeOne()
method creates a new Queue
using the LinkedList
implementation and an array of strings with 12 elements.
ChallengeOne()
method adds each element of the array to the queue one by one, printing the queue after each addition.
ChallengeOne()
method then removes each element from the queue one by one, printing the queue after each removal.
main()
method simply calls the ChallengeOne()
method to run the code.
The LinkedList
class provides an implementation of the Queue
interface in Java.
To add an element to the back of the queue using a linked list, create a new node and set its next reference to null
. Set the current last node's next reference to the new node, and update the last node to be the new node. To remove an element from the front of the queue, set the first node's next reference to be the second node in the sequence, and update the first node to be the second node. The removed element is then returned.
ChallengeTwo()
method declares three Queue
objects of type Integer
: q1
, q2
, and qSum
.
array1
and array2
, are also declared and initialized with some integer values.
for
loop is used to add each element of array1
to q1
.
for
loop is used to add each element of array2
to q2
.
q1
and q2
.
while
loop compares the first element of q1
and q2
and adds the smaller value to qSum
. It then removes the added element from its respective Queue
.
Queue
s is empty, the remaining elements of the other Queue
are added to qSum
.
qSum
.
This program demonstrates how to invert the order of a queue using a stack. The following are the main steps:
A stack is a data structure that stores a collection of elements, and allows two main operations: push (adds an element to the top of the stack) and pop (removes the element at the top of the stack). It works on a "last in, first out" (LIFO) principle
In this program, the stack is used to temporarily store the elements of the queue in reverse order, before moving them back to the queue. Each element of the queue is pushed onto the stack, effectively reversing their order. Then, each element is popped from the stack and added back to the queue, in reverse order.
Name | Link to Contributions | Peer Score |
---|---|---|
Akhil Nandhakumar | Link | 0.9/1 |
Tristan Copley | Link | 0.9/1 |
Adi Khandelwal | Link | 0.9/1 |
unkown • 6/16/2025, 12:03:30 PM