Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The two classes mention in the question below. public class SinglyLinkedList { private Node head=null; private Node tail=null; private int size=0; public int size() {

The two classes mention in the question below.

image text in transcribed

public class SinglyLinkedList { private Node head=null; private Node tail=null; private int size=0; public int size() { return size;} public boolean isEmpty(){ return size==0;} public E first() { if (isEmpty()) return null; return head.getElement(); } public E last() { if (isEmpty()) return null; return tail.getElement(); } public void addFirst(E e) { head=new Node(e,head); if(size==0) tail=head; size++; } public void addLast(E e) { Node newest=new Node(e,null); if(isEmpty()) head=newest; else tail.setNext(newest); tail=newest; size++; } public E removeFirst() { if(isEmpty()) return null; E answer=head.getElement(); head=head.getNext(); size--; if (size==0) tail=null; return answer; } private static class Node{ private E element; private Node next; public Node(E e, Node n) { element=e; next=n; } public E getElement(){ return element; } public Node getNext() { return next; } public void setNext(Node n) { next=n; } } }

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 E last() { if (isEmpty()) return null; return trailer.getPrev().getElement(); } 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_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

Databases Illuminated

Authors: Catherine M. Ricardo, Susan D. Urban, Karen C. Davis

4th Edition

1284231585, 978-1284231588

More Books

Students also viewed these Databases questions

Question

3. Define skepticism and its role in scientific psychology.

Answered: 1 week ago