Write a program that creates and processes a data structure (DS) (according to t
ID: 3840783 • Letter: W
Question
Write a program that creates and processes a data structure (DS) (according to the variant, MaxSize and fulfills such functions: Create a dynamic data structure (see. table) by manually entering the number of elements; Fill the DS with whole elements by adding elements: a) with the push function; b) with the enqueue function; According to the concept of the DS (LIFO, FIFO), delete from DS element: a) with the function pop; b) with the function dequeue; Ensure the output of the results of Size, Peek, Empty and Full functions: Carry out the following activities with the DS and ensure output of the results: a) count the elements equal to zero: b) count negative elements in the DS; c) find the number of elements that belong to the interval [-10: 10]: d) count how many pairs of elements are there in the DS.Explanation / Answer
Please find the code below: (for 1/2/3/4):
public class StackDS{
private int SIZE;
private int[] sArr;
private int top;
public StackDS(int s) {
SIZE = s;
sArr = new int[SIZE];
top = -1;
}
public void push(int j) {
sArr[++top] = j;
}
public int pop() {
return sArr[top--];
}
public int peek() {
return sArr[top];
}
public boolean isEmpty() {
return (top == -1);
}
public boolean isFull() {
return (top == SIZE - 1);
}
public static void main(String[] args) {
StackDS stackObj = new StackDS(10);
stackObj.push(1);
stackObj.push(2);
while (!stackObj.isEmpty()) {
System.out.println(stackObj.pop());
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.