Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

- Add a new class called AppTest, create as many objects as you want from the class Employee with different amount of Salaries. - Using

- Add a new class called AppTest, create as many objects as you want from the class Employee with different amount of Salaries.

- Using the method public void addBetween(E e, Node predecessor,Nodesuccessor) add all the objects created recently to the DoublyLinkedList.java in ascending order.

using these classes:

public class Employee {

private int id;

private String name;

private double Salary;

public Employee(int id, String name, double salary) {

this.id = id;

this.name = name;

Salary = salary;

}

public double getSalary() {

return Salary;

}

public void setSalary(double salary) {

Salary = salary;

}

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

@Override

public String toString() {

return "Employee [id=" + id + ", name=" + name + ", salary=" + Salary + "]" ;

}

}

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

public class DoublyLinkedList {

private Node header;

private Nodetrailer;

private int size=0;

public DoublyLinkedList()

{

header=new Node<>(null,null,null);

trailer=new Node<>(null,header, null);

header.setNext(trailer);

}

public int size() { return size;}

public boolean isEmpty(){ return size==0;}

public E first()

{

if (isEmpty()) return null;

return header.getNext().getElement();

}

public void displayfarward() {

Nodep=header.getNext();

while(p!=trailer)

{

System.out.println(p.getElement());

p=p.next;

}

}

public void displayBackward()

{

Nodep=trailer.getPrev();

while(p!= header)

{

System.out.println(p.getElement());

p=p.prev;

}

}

public void concatenate(DoublyLinkedList newlist)

{

//trailer.prev.next= newlist.header;

//newlist.header.prev=trailer.prev.next;

trailer.getPrev().setNext(newlist.header.getNext());

newlist.header.getNext().setPrev(trailer.getPrev());

trailer=newlist.trailer;

}

public void reverse()

{

Nodetemp=null;

Nodecurrent=header;

while(current!=null)

{

temp=current.prev;

current.prev=current.next;

current.next=temp;

current=current.prev;

}

}

public E last()

{

if (isEmpty()) return null;

return trailer.getPrev().getElement();

}

public boolean find(E element)

{

Node temp=header.next;

while(temp!=trailer)

{

if(temp.getElement().equals(element))

return true;

temp=temp.next;

}

return false;

}

public int count(Node t)

{

int count=0;

Nodetemp = header.next;

while(temp !=null)

{

count++;

temp=temp.next;

}

return count;

}

public int countRec(Node t)

{

Nodetemp=header.next;

if(temp ==null)

return 0;

else

return 1+countRec(t.next);

}

private void addBetween(E e, Node predecessor,Nodesuccessor)

{

Node newest=new Node<>(e,predecessor, successor);

predecessor.setNext(newest);

successor.setPrev(newest);

size++;

}

private E remove(Node e)

{

Node predecessor=e.getPrev();

Node successor=e.getNext();

predecessor.setNext(successor);

successor.setPrev(predecessor);

size--;

return e.getElement();

}

public void addFirst(E e)

{

addBetween(e,header,header.getNext());

}

public void addLast(E e)

{

addBetween(e,trailer.getPrev(),trailer);

}

public E removeFirst()

{

if(isEmpty()) return null;

return remove(header.getNext());

}

public E removeLast()

{

if(isEmpty()) return null;

return remove(trailer.getPrev());

}

private static class Node{

private E element;

private Node next;

private Node prev;

public Node(E e,Node p, Node n)

{

element=e;

prev=p;

next=n;

}

public E getElement(){ return element; }

public Node getPrev() { return prev;}

public Node getNext() { return next;}

public void setPrev(Node n) {prev=n;}

public void setNext(Node n) {next=n;}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Spatial And Temporal Databases 10th International Symposium Sstd 2007 Boston Ma Usa July 2007 Proceedings Lncs 4605

Authors: Dimitris Papadias ,Donghui Zhang ,George Kollios

2007th Edition

3540735399, 978-3540735397

More Books

Students also viewed these Databases questions