The first one is the array and the second one is the linked stack. import java.u
ID: 3598489 • Letter: T
Question
The first one is the array and the second one is the linked stack.
import java.util.Collections;
import java.util.LinkedList;
public class Stack {
private static int top ;
private static int[] stor;
Stack(int cap) {
if (cap == 0)
throw new IllegalArgumentException(
"Stack's capacity must be positive");
stor = new int[cap];
top = -1;
}
int push(int value) throws Exception {
if (top >= stor.length)
throw new Exception("Stack's underlying storage is overflow");
top++;
return stor[top] = value;
}
int pop() throws Exception {
if (top == -1)
throw new Exception("Stack is empty");
top--;
return stor[top];
}
public static void main(String args[]) throws Exception {
Stack stack1 = new Stack(20);
System.out.println("Push onto stack: " +"[" + stack1.push(1)+"]");
System.out.println("Push onto stack: " +"[" +stack1.push(7)+"]");
System.out.println("Push onto stack: " +"[" +stack1.push(3)+"]");
System.out.println("Push onto stack: " +"[" +stack1.push(4)+"]");
System.out.println("Push onto stack: " +"[" +stack1.push(9)+"]");
System.out.println("Push onto stack: " +"[" +stack1.push(2)+"]");
System.out.println("[" +stack1.pop() +"]");
System.out.println("[" +stack1.pop() +"]");
System.out.println("[" +stack1.pop() +"]");
System.out.println("[" +stack1.pop() +"]");
System.out.println("[" +stack1.pop() +"]");
}}
import javax.naming.LinkLoopException;
public class LinkedListStack {
Node top; // the first node
// nest class to define linkedlist node
private class Node {
int value;
Node next;
public Object item;
}
public LinkedListStack() {
top = null;
}
// Remove value from the beginning of the list for demonstrating behaviour of stack
public int pop() throws LinkLoopException {
if (top == null) {
throw new LinkLoopException();
}
int value = top.value;
top=top.next;
Node first = null;
int size;
return value;
}
// Add value to the beginning of the list for demonstrating behaviour of stack
public int push(int value) {
Node oldHead = top;
top = new Node();
top.value = value;
top.next = oldHead;
return value;
}
class Driver1 {
}
public static void main(String args[]) throws LinkLoopException
{
LinkedListStack stack1=new LinkedListStack();
System.out.print("Push on: " + stack1.push(1)+ ", ");
System.out.print("Push on: " + stack1.push(7)+ ", ");
System.out.print("Push on: " + stack1.push(3)+ ", ");
System.out.print("Push on: " + stack1.push(4)+ ", ");
System.out.print("Push on: "+ stack1.push(9)+ ", ");
System.out.println("Push on: " +stack1.push(2));
System.out.print("Stack: ");
printList(stack1.top);
System.out.println("Pop Off: " + stack1.pop());
System.out.println("Pop Off: " + stack1.pop());
System.out.println("Pop Off: " + stack1.pop());
System.out.println("Pop Off: " + stack1.pop());
System.out.println("Pop Off: " + stack1.pop());
System.out.println("Pop Off: " + stack1.pop());
}
private int size() {
// TODO Auto-generated method stub
return 0;
}
public static void printList(Node top) {
Node temp = top;
while (temp != null) {
System.out.print(" " + temp.value);
temp = temp.next;
}
System.out.println(" ");
}
}
Modify the main method of the Driver class from Lab #7. In this method, do the following: 1. 2. 3. Create instances of an ArrayQ and a LinkedQueue Enqueue the following int's onto the two queues: (1,7,3,4,9,2) Dequeue all the elements from the queues, displaying each int as it's removedExplanation / Answer
Hi friend, I have implemented ArrayQ and added test code in main method. Please repsot in separate post for implementation of LinkedQueue.
//A class to represent a queue
public class ArrayQ
{
int front, rear, size;
int capacity;
int array[];
public ArrayQ(int capacity) {
this.capacity = capacity;
front = this.size = 0;
rear = capacity - 1;
array = new int[this.capacity];
}
// Queue is full when size becomes equal to
// the capacity
boolean isFull()
{ return (size == capacity);
}
// Queue is empty when size is 0
boolean isEmpty()
{ return (size == 0); }
// Method to add an item to the queue.
// It changes rear and size
int enqueue( int item)
{
if (isFull())
return -1;
this.rear = (this.rear + 1)%this.capacity;
this.array[this.rear] = item;
this.size = this.size + 1;
return item;
}
// Method to remove an item from queue.
// It changes front and size
int dequeue()
{
if (isEmpty())
return Integer.MIN_VALUE;
int item = this.array[this.front];
this.front = (this.front + 1)%this.capacity;
this.size = this.size - 1;
return item;
}
// Method to get front of queue
int front()
{
if (isEmpty())
return Integer.MIN_VALUE;
return this.array[this.front];
}
// Method to get rear of queue
int rear()
{
if (isEmpty())
return Integer.MIN_VALUE;
return this.array[this.rear];
}
public static void main(String args[]) throws Exception {
ArrayQ queue = new ArrayQ(20);
System.out.println("enqueue onto stack: " +"[" + queue.enqueue(1)+"]");
System.out.println("enqueue onto stack: " +"[" +queue.enqueue(7)+"]");
System.out.println("enqueue onto stack: " +"[" +queue.enqueue(3)+"]");
System.out.println("enqueue onto stack: " +"[" +queue.enqueue(4)+"]");
System.out.println("enqueue onto stack: " +"[" +queue.enqueue(9)+"]");
System.out.println("enqueue onto stack: " +"[" +queue.enqueue(2)+"]");
System.out.println("[" +queue.dequeue() +"]");
System.out.println("[" +queue.dequeue() +"]");
System.out.println("[" +queue.dequeue() +"]");
System.out.println("[" +queue.dequeue() +"]");
System.out.println("[" +queue.dequeue() +"]");
}}
/*
Sample run:
enqueue onto stack: [1]
enqueue onto stack: [7]
enqueue onto stack: [3]
enqueue onto stack: [4]
enqueue onto stack: [9]
enqueue onto stack: [2]
[1]
[7]
[3]
[4]
[9]
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.