I have this code public class ArrayQueue implements Queue { //fields to represent our…
- User
I have this code
public class ArrayQueue implements Queue { //fields to represent our stack in an array
//the array of items private Object[] items; //maximum capacity private int maximum; //pointer to the position of the front of the queue //this points to 0 when the stack is empty private int front; //Actual number of items in the queue private int size; //Constructor public ArrayQueue(){ //set the capacity to 20 maximum = 20; //create the array items = new Object[maximum]; //set the front to 0 as the stack is empty front = 0; //set the size to 0 size = 0; } @Override public Object dequeue() { // first check if the queue is empty. // If it is, print an error message and return null, // otherwise return the item at the front, // decrement the size and increment the front pointer. if (isEmpty()) { System.out.println("Stack Empty"); return null; } else { Object answer = items[front]; front = (front + 1) % maximum; size--; return answer; } } @Override public void enqueue(Object o) { // first check if the queue is full. // If it is, print an error message, // else calculate the position of the back of the queue // and put the item in the back, // and finally increment the size if (size == maximum){ System.out.println("Queue full"); } else { int back = (front + size) % maximum; items[back] = o; size++; } } @Override public boolean isEmpty() { //use the size! return (size==0); } @Override public int size() { //use the size! return size; } @Override public Object front() { // First check if the queue is empty. // If it is, print an error message and return null, // otherwise return the item at the front. if (isEmpty()) { System.out.println("Stack Empty"); return null; } else { return items[front]; } }}
public class ArrayStack implements Stack { //fields to represent our stack in an array
//the array of items private Object[] items; //maximum capacity private int maximum; //pointer to the position of the top of the stack //this points to -1 when the stack is empty private int top; public ArrayStack(){ //set the capacity to 20 maximum = 20; //create the array items = new Object[maximum]; //set the top to -1 as the stack is empty top = -1; } @Override public Object pop() { // First check if the stack is empty. // If it is, print an error message and return null, // otherwise, return the item the top is pointing to // and decrement the top pointer if (isEmpty()) { System.out.println("Stack Empty"); return null; } else { Object answer = items[top]; top--; return answer; } } @Override public void push(Object o) { // First check if the array is full. // If it is, print an error message, // else increment the top and put the item in that space if (top == (maximum - 1)){ System.out.println("Stack full"); } else { top++; items[top] = o; } } @Override public boolean isEmpty() { // The stack is empty if the top is pointing to -1 return (top == -1); } @Override public int size() { // The size of the stack can be calculated using the position of top return(top + 1); } @Override public Object top() { // First check if the stack is empty. // If it is, print an error message and return null, // otherwise, return the item the top is pointing to. if (isEmpty()) { System.out.println("Stack Empty"); return null; } else { return items[top]; } } - Assistant
This code includes multiple classes that work together to create simulations of directed graphs representing relationships between people in a social network.
- ArrayQueue class: This class implements a queue data structure using an array.
- ArrayStack class: This class implements a stack data structure using an array.
- DiGraph class: This class represents a directed graph, with methods to add edges and print the adjacency list representation of the graph.
- Main class: This class contains the
buildGraph,testBroadCastTo,canReachTest, andreceiversOfTestmethods, which are used to create the graph structure, test broadcasting to a person, test if a source person can reach a target person, and test receivers of a broadcast respectively.
- User
- Assistant