Checkpoint 2 Review Ticket

(TRIO) Adi, Akhil, Tristan

By Akhil Nandhakumar3/27/2023
Article Thumbnail

Requirements:

  • Perform Queue Hacks in Data Structures lesson with your own Data
  • Build Stack and create your own stack hack

Queue Hack 1

 

Explanation:

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:

  • The ChallengeOne() method creates a new Queue using the LinkedList implementation and an array of strings with 12 elements.
  • The ChallengeOne() method adds each element of the array to the queue one by one, printing the queue after each addition.
  • The ChallengeOne() method then removes each element from the queue one by one, printing the queue after each removal.
  • The main() method simply calls the ChallengeOne() method to run the code.

LinkedList:

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.


Queue Hack 2:

 

Explanation:

  • The ChallengeTwo() method declares three Queue objects of type Integer: q1, q2, and qSum.
  • Two integer arrays, array1 and array2, are also declared and initialized with some integer values.
  • A for loop is used to add each element of array1 to q1.
  • A second for loop is used to add each element of array2 to q2.
  • The code then prints out the contents of q1 and q2.
  • The 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.
  • Once one of the Queues is empty, the remaining elements of the other Queue are added to qSum.
  • Finally, the code prints the contents of qSum.

Queue Hack 4:

 

Explanation:

This program demonstrates how to invert the order of a queue using a stack. The following are the main steps:

  • Declare a queue and a stack of integers, and an array of integers to add to the queue.
  • Add the integers from the array to the queue using a for loop.
  • Store the size of the queue in a variable.
  • Print the initial queue.
  • Move the integers from the queue to the stack using a for loop, effectively reversing their order.
  • Move the integers back from the stack to the queue in reverse order using another for loop.
  • Print the inverted queue.

Stack Explanation:

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.


Commits From Last Month

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