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, 23.06.2019 03:30
In vista and windows 7, the appearance and personalization option allows you to change the
Answers: 1
question
Computers and Technology, 23.06.2019 03:50
Q-1 which of the following can exist as cloud-based it resources? a. physical serverb. virtual serverc. software programd. network device
Answers: 1
question
Computers and Technology, 23.06.2019 05:00
Which best explains why a digital leader would join a society specializing in technology
Answers: 1
question
Computers and Technology, 23.06.2019 13:30
Me ! evelyn is a manager in a retail unit. she wants to prepare a report on the projected profit for the next year. which function can she use? a. pmt b. round c. division d. what-if analysis
Answers: 2
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
Mathematics, 29.04.2021 01:00
question
English, 29.04.2021 01:00
question
Mathematics, 29.04.2021 01:00
question
Mathematics, 29.04.2021 01:00
question
Mathematics, 29.04.2021 01:00
question
Mathematics, 29.04.2021 01:00
Questions on the website: 13722361