Write two classes , one for stack and one for queue that are child classes of th
ID: 3760482 • Letter: W
Question
Write two classes , one for stack and one for queue that are child classes of the link list class provided below:
package dynamic;
public class LinkedList {
private Node head;
public LinkedList(int x){
head = new Node(x);
}
public LinkedList(Node n){
head = n;
}
public void print(){
Node current = head;
while (current!=null){
System.out.print(current.getData()+" ");
current = current.getNext();
}
System.out.println();
}
public void insert(int x){
if(head==null){
head = new Node(x);
return;
}
if(x<=head.getData()){
head = new Node(x, head); // case 1
return;
}
Node current = head;
Node previous = head;
while(current!=null){
if(x>current.getData()){
previous = current;
current = current.getNext();
} else {
previous.setNext(new Node(x,current)); // case 2
return;
}
}
previous.setNext(new Node(x)); // case 3
}
public boolean delete(int x){
if(head.getData()==x){
head = head.getNext(); // case 1
return true;
}
Node current = head.getNext();
Node previous = head;
while(current!=null){
if(current.getData()==x){
previous.setNext(previous.getNext().getNext()); // cases 2 & 3
return true;
} else if(current.getData()>x){
break;
} else {
previous = current;
current = current.getNext();
}
} // while
return false;
}
public void append(int x){
if (head == null){
head = new Node(x);
return;
}
Node current = head;
while(current.getNext()!=null){
current = current.getNext();
}
// Node temp = new Node(x);
// current.setNext(temp);
current.setNext(new Node(x));
}
}// the code of linked list class
Explanation / Answer
public class Queue extends LinkedList
{
private Node tail;
public Queue(int x){
super(x);
head=new Node(x);
tail=head;
count = 1;
}
public void insert(int x)
{
if(head!=null)
{
if(head.getNext()==null)
{
Node temp = new Node(x);
tail=temp;
head.setNext(tail);
count++;
}
else
{
Node temp = new Node(x);
tail.setNext(temp);
tail=temp;
count++;
}
}
else
{
head = new Node(x);
count=1;
}
}
public boolean delete(int x)
{
Boolean test = false;
if(x>0)
{
for(int i=0;i<x;i++)
{
if(head!=null)
{
head = head.getNext();
count--;
test=true;
}
}
}
return test;
}
public void clearAll()
{
int index = count;
for(int i=0;i<index;i++)
{
System.out.println(head.getData());
delete(1);
}
}
public void print()
{
Node current = head;
if(head!=null)
{
for(int i=0;i<count;i++)
{
System.out.println(current.getData());
current=current.getNext();
}
}
}
}//class
public class Stack extends LinkedList
{
public Stack(int x)
{
super(x);
head=new Node(x);
count=1;
}
public void insert(int x)
{
if(head!=null)
{
Node temp = new Node(x);
temp.setNext(head);
head=temp;
count++;
}
else
{
head = new Node(x);
count=1;
}
}
public boolean delete(int x)
{
Boolean test = false;
if(x>0)
{
for(int i=0;i<x;i++)
{
if(head!=null)
{
head=head.getNext();
count--;
test=true;
}
}
}
return test;
}
public void clearAll()
{
int index = count;
for(int i=0;i<index;i++)
{
System.out.println(head.getData());
delete(1);
}
}
public void print()
{
Node current = head;
for(int i=0;i<count;i++)
{
System.out.println(current.getData());
current=current.getNext();
}
}
}//class
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.