subject

Write a method for the queue class in the queue. java program (listing 4.4) that displays the contents of the queue. note that this does not mean simply displaying the contents of the underlying array. you should show the queue contents from the first item inserted to the last, without indicating to the viewer whether the sequence is broken by wrapping around the end of the array. be careful that one item and no items display properly, no matter where front and rear are.
listing 4.4 is below
class queue
{
private int maxsize;
private long[] quearray;
private int front;
private int rear;
private int nitems;
//
public queue(int s)
{
maxsize = s;
quearray = new long[maxsize];
front =0;
rear = -1;
nitems = 0;
}
//
public void insert(long j)
{
if(rear == maxsize -1)
rear = -1;
quearray[++rear] = j;
nitems++;
}
//
public long remove()
{
long temp = quearray[front++];
if(front == maxsize)
front = 0;
nitems--;
return temp;
}
//
public long peekfront()
{
return quearray[front];
}
//
public boolean isempty()
{
return(nitems==0);
}
//
public boolean isfull()
{
return (nitems==maxsize);
}
//
public int size()
{
return nitems;
}
//
} //end class
class queueapp
{
public static void main(string[] args)
{
queue thequeue = new queue(5);
thequeue. insert(10);
thequeue. insert(20);
thequeue. insert(30);
thequeue. insert(40);
thequeue. remove();
thequeue. remove();
thequeue. remove();
thequeue. insert(50);
thequeue. insert(60);
thequeue. insert(70);
thequeue. insert(80);
while( ! thequeue. isempty() )
{
long n = thequeue. remove();
system. out. print(n);
system. out. print( " ");
}
system. out. println(" ");
} //end main()
} //end class
4.2
create a deque class based on the discussion of deques (double-ended queues) in this chapter. it should includeand isfull() methods. it will need to support wraparound at the end of the array, as queues do.
4.3
write a program that implements a stack class that is based on the deque class in the programming project 4.2. this stack class should have the same methods and capabillities as the stackx class in the stack. java program (listing 4.1).
listing 4.1 is below
class stackx
{
private int maxsize;
private long[] stackarray;
private int top;
//
public stackx(int s)
{
maxsize = s;
stackarray = new long[maxsize];
top = -1;
}
//
public void push(long j)
{
stackarray[++top] = j;
}
//
public long pop()
{
return stackarray[top --];
}
//
public long peek()
{
return stackarray[top];
}
//
public boolean isempty()
{
return (top == -1);
}
//
public boolean isfull()
{
return (top == maxsize-1);
}
//
} //end class stackx
class stackapp
{
public static void main(string[] args)
{
stackx the stack = new stackx(10);
thestack. push(20);
thestack. push(40);
thestack. push(60);
thestack. push(80);
while( ! thestack. isempty() )
{
long value = thestack. pop();
system. out. print(value);
system. out. print(" ");
} //end while
system. out. println(" ");
} //end main
} //end class

ansver
Answers: 1

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 05:00
Lisa has a section of her document that she would like to include in the index. which option should lisa choose? mark index mark entry insert endnote add text
Answers: 3
question
Computers and Technology, 22.06.2019 22:30
The qwerty keyboard is the most common layout of keys on a keyboard
Answers: 3
question
Computers and Technology, 23.06.2019 20:30
If chris has a car liability insurance, what damage would he be covered for
Answers: 1
question
Computers and Technology, 24.06.2019 00:00
Visualizing a game of β€œtag” to remember the meaning of contagious
Answers: 3
You know the right answer?
Write a method for the queue class in the queue. java program (listing 4.4) that displays the conten...
Questions
question
Chemistry, 01.07.2020 15:01
Questions on the website: 13722363