Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class DoublyLinkedList { private Node header; private Node trailer; private int size=0; public DoublyLinkedList() { header=new Node(null,null,null); trailer=new Node(null,header, null); header.setNext(trailer); } public int

image text in transcribed

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() // to return the first element

{

if (isEmpty())

return null;

return header.getNext().getElement();

}

public E last() // to return the last element

{

if (isEmpty())

return null;

return trailer.getPrev().getElement();

}

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

{

Employee e1;

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

predecessor.setNext(newest);

successor.setPrev(newest);

size++;

}

public E remove(Node e)

{

if(size==0)

return null;

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;

else

return remove(header.getNext());

}

public E removeLast()

{

if(isEmpty())

return null;

else

return remove(trailer.getPrev());

}

public void displayForward()

{

for (Node t=header.getNext();t!=trailer;t=t.getNext())

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

}

public void reverse()

{

Node i=header.getNext();

Node j=trailer.getPrev();

while (i.getNext()!=j && i!=j)

{

E t=i.getElement();

i.setElement(j.getElement());

j.setElement(t);

i=i.getNext();

j=j.getPrev();

}

}

public void displayBackward()

{

for (Node t=trailer.getPrev();t!=header;t=t.getPrev())

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

}

public void concat(DoublyLinkedList newList)

{

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

newList.header.getNext().setPrev(trailer.getNext());

trailer=newList.trailer;

}

public static class Node{

private E element;

private Node prev;

private Node next;

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

{

element=e;

prev=p;

next=n;

}

public E getElement()

{

return element;

}

public void setElement(E e)

{

element=e;

}

public Node getPrev()

{

return prev;

}

public Node getNext()

{

return next;

}

public void setPrev(Node n)

{

prev=n;

}

public void setNext(Node n)

{

next=n;

}

}

}

Add a new class called Employee that has three private attributes called id (int), name (String) Salary (Double). Add the setter and getter for all the data members. Import the class DoublyLinkedList.java covered in Lab-06 Add a new class called AppTest, create as many objects as you want different amount of Salaries. Using the method public void addBetween(E e, Node predecessor,Node E> successor) add all the objects created recently to the DoublyLinkedList.java in ascending order Add a new class called Employee that has three private attributes called id (int), name (String) Salary (Double). Add the setter and getter for all the data members. Import the class DoublyLinkedList.java covered in Lab-06 Add a new class called AppTest, create as many objects as you want different amount of Salaries. Using the method public void addBetween(E e, Node predecessor,Node E> successor) add all the objects created recently to the DoublyLinkedList.java in ascending order

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_2

Step: 3

blur-text-image_3

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

Relational Database Design A Practical Approach

Authors: Marilyn Campbell

1st Edition

1587193175, 978-1587193170

More Books

Students also viewed these Databases questions