In: Computer Science
Question 1 |
|
Refer to the operations below: |
|
Add (10 + 5) |
|
Add (4+8) |
|
Add (7*2) |
|
Add (90 – 3) |
|
Print list |
|
Print peek |
|
Remove an item from the list |
|
Print list |
|
1.1 Implement the operations above into a Queue structure called q1. |
(10) |
1.2 Implement the operations above into a Stack structure called s1. |
(10 |
Guidelines: Please take note!
(a) Name your program Question1_1 for the queue structure and Question1_2 for the stack structure
(b) The Java Programmer is required to include screenshots from a JGrasp IDE which displays their commented code and the output and also indicates their laptop date and time of code execution, the screenshot of the code needs to be error-free and must not produce any logic errors!
Implementation in JAVA:
Question 1:
Implementation of queue using Array:
public class Question1_1 {
public static void main(String[] args) throws
Queuefullexception, Queueemptyexception {
QueueusingArray qe= new
QueueusingArray();
System.out.println("Add(10+5) :
");
qe.add(10+5);
System.out.print("Queue is :
");
for(int i=0;i<qe.size;i++)
{
System.out.print(qe.data[i]+" ");
}
System.out.println();
System.out.println("Add(4+8) :
");
qe.add(4+8);
System.out.print("Queue is :
");
for(int i=0;i<qe.size;i++)
{
System.out.print(qe.data[i]+" ");
}
System.out.println();
System.out.println("Add(7*2) :
");
qe.add(7*2);
System.out.print("Queue is :
");
for(int i=0;i<qe.size;i++)
{
System.out.print(qe.data[i]+" ");
}
System.out.println();
System.out.println("Add(90-3) :
");
qe.add(90-3);
System.out.print("Queue is :
");
for(int i=0;i<qe.size;i++)
{
System.out.print(qe.data[i]+" ");
}
System.out.println();
System.out.println();
System.out.println("List is :
");
for(int i=0;i<qe.size;i++)
{
System.out.print(qe.data[i]+" ");
}
System.out.println();
System.out.println("Peek element is
: "+ qe.peek());
System.out.print("Remove: ");
System.out.println(qe.remove());
System.out.print("List after
Removing : ");
for(int i=0;i<qe.size;i++)
{
System.out.print(qe.data[i]+" ");
}
System.out.println();
System.out.println();
System.out.println("Look here
Insertion and deletion of elements are on Different ends ");
}
}
//---- ----- classQueueusingArray
class QueueusingArray {
int data[];
int rear;
int front;
int size;
public QueueusingArray() {
data=new int[10];
rear=-1;
front=-1;
size=0;
}
public QueueusingArray(int capacity) {
data=new int[capacity];
rear=-1;
front=-1;
size=0;
}
public int size() {
return size;
}
public boolean isempty() {
return size==0;
}
public int peek() throws Queueemptyexception {
if(size==0) {
Queueemptyexception e= new Queueemptyexception();
throw e;
}
return data[front];
}
public void add(int elem) throws Queuefullexception
{
if(size()==data.length) {
//
Queuefullexception e= new Queuefullexception();
// throw e;
doublecapacity();
}
if(size==0) {
front=0;
}
size++;
rear++;
if(rear==data.length) {
rear=0;
}
data[rear]=elem;
}
public int remove() throws Queueemptyexception {
if(size()==0) {
Queueemptyexception e= new Queueemptyexception();
throw e;
}
int temp=data[front];
data=removeTheElement(data,0);
size--;
return temp;
}
public int[] removeTheElement(int[] arr,int
index)
{
if (arr == null|| index < 0|| index >= arr.length) {
return arr;
}
// Create another array of size one less
int[] anotherArray = new int[arr.length];
// Copy the elements except the index
// from original array to the other array
for (int i = 0, k = 0; i < arr.length-1; i++) {
// if the index is
// the removal element index
if (i == index) {
continue;
}
// if the index is not
// the removal element index
anotherArray[k++] = arr[i];
}
// return the resultant array
return anotherArray;
}
private void doublecapacity() {
int temp[];
temp=data;
data= new int[data.length*2];
for(int i=0;i<temp.length;i++)
{
data[i]=temp[i];
}
}
}
class Queuefullexception extends Exception {
}
class Queueemptyexception extends Exception {
}
SAMPLE OUTPUT:
QUESTION 2:
Implemantation of Stack using array in java
public class Question1_2 {
public static void main(String[] args) throws
Stackfullexception, Stackemptyexception {
Stackusingarray st= new
Stackusingarray();
System.out.println("Add(10+5) :
");
st.add(10+5);
System.out.print("Stack is :
");
for(int i=0;i<st.size();i++)
{
System.out.print(st.mydata[i]+" ");
}
System.out.println();
System.out.println("Add(4+8) :
");
st.add(4+8);
System.out.print("Stack is :
");
for(int i=0;i<st.size();i++)
{
System.out.print(st.mydata[i]+" ");
}
System.out.println();
System.out.println("Add(7*2) :
");
st.add(7*2);
System.out.print("Stack is :
");
for(int i=0;i<st.size();i++)
{
System.out.print(st.mydata[i]+" ");
}
System.out.println();
System.out.println("Add(90-3) :
");
st.add(90-3);
System.out.print("Stack is :
");
for(int i=0;i<st.size();i++)
{
System.out.print(st.mydata[i]+" ");
}
System.out.println();
System.out.println();
System.out.println("List is :
");
for(int i=0;i<st.size();i++)
{
System.out.print(st.mydata[i]+" ");
}
System.out.println();
System.out.println("Peek element is
: "+ st.peek());
System.out.print("Remove: ");
System.out.println(st.remove());
System.out.print("List after
Removing : ");
for(int i=0;i<st.size();i++)
{
System.out.print(st.mydata[i]+" ");
}
System.out.println();
System.out.println();
System.out.println("Look here
Insertion and deletion of elements are on same ends ");
}
}
// stack class
class Stackusingarray {
// declaring mydata array
int mydata[];
// and top also
int top;
// no argument constructor
public Stackusingarray() {
mydata=new int[10];
top=-1;
}
// argument constructor with capacity defined
public Stackusingarray(int capacity) {
mydata=new int[capacity];
top=-1;
}
// will return size
public int size() {
return top+1;
}
// will return either stack is empty or not
public boolean isempty() {
if(size()==0) {
return
true;
}
return false;
}
// will return top element of stack
// in case of if stack is empty will throw exception
public int peek() throws Stackemptyexception {
if(top==0) {
Stackemptyexception e= new
Stackemptyexception();
throw e;}
return mydata[top];
}
// will remove topmost element from stack
// in case of stack is empty throw exception
public int remove() throws Stackemptyexception {
if(size()==0) {
Stackemptyexception e= new
Stackemptyexception();
throw e;
}
int temp=mydata[top];
top--;
return temp;
}
// will push element in stack
// in case of if capacity exceeds will throw stack full
exception
public void add(int i) throws Stackfullexception
{
// TODO Auto-generated method
stub
if(top==mydata.length-1) {
// Stackfullexception e=new
Stackfullexception();
// throw e;
// this method is
implemented below
// it double the
capacity of array
doublecapacity();
}
mydata[top+1]=i;
top++;
}
// will double the capacity of array
private void doublecapacity() {
// TODO Auto-generated method
stub
int temp[]=new
int[mydata.length];
temp=mydata;
mydata=new
int[2*mydata.length];
for(int i=0;i<=top;i++) {
mydata[i]=temp[i];
}
}
}
class Stackemptyexception extends Exception {
}
class Stackfullexception extends Exception {
}
SAMPLE OUTPUT:
If you have any doubt regarding this question please ask me in comments
//THANK YOU:-)