Question: Rewrite the following C++ classes in Java and compare the result with the C++ version in terms of readability and writability. Hint: Both stack_2 and
Rewrite the following C++ classes in Java and compare the result with the C++ version in terms of readability and writability. Hint: Both stack_2 and queue_2 are *private* subclasses.
class single_linked_list {
private:
class node {
public:
node* link;
int contents;
};
node *head;
public:
single_linked_list() {head=0};
void insert_at_head(int);
void insert_at_tail(int);
int remove_at_head();
int empty();
};
class stack_2 : private single_linked_list {
public:
stack_2(){}
void push(int value) {
single_linked_list :: insert_at_head(value);
}
int pop() {
return single_linked_list :: remove_at_head();
}
single_linked_list :: empty;
};
class queue_2 : private single_linked_list {
public:
queue_2(){}
void enqueue(int value) {
single_linked_list :: insert_at_tail(value);
}
int dequeue() {
single_linked_list :: remove_at_head();
}
single_linked_list :: empty;
};
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
