SlideShare a Scribd company logo
Implement the interface you wrote for Lab B (EntryWayListInterface). As a reminder,
EntryWayListInterface allows the user to access to list elements only through the beginning and
end of the list.
Your implementation can use either an expandable array or linked nodes- it is your choice. You
can decide what instance variables are needed. You must implement every method from the
interface. Make sure to account for special conditions such as empty lists and singleton lists.
Note: your instance data variable should be either a) an array or b) one or more nodes. It should
not be an AList or LList object or an ArrayList object.
Your implementation must:
compile
contain these implemented methods:
insertHead
insertTail
deleteHead
deleteTail
display
contains
isEmpty
isFull
Also create a driver program to test your implementation. The driver program will operate from
the client perspective. Your driver program should:
display an empty list
add five entries to the list- some at the head and some at the tail
display the list
remove the first entry
remove the last entry
display the list
test to see if elements are in the list (test one element that is in the list and one that is not)
remove the last three elements in the list
try to remove an element from the empty list
Write a second class to implement EntryWayListInterface. Instead of using an array or linked
nodes, use either an AList or LList object as your instance data variable. In this way, you are
using an existing data type (AList or LList) to implement a new data type
(EntryWayListInterface). Inside the methods of this class, invoke methods on the AList or LList
object to accomplish the task.
/** An interface for the ADT list.
Entries in a list have positions that begin with 1.
* Accesses only entries that are the head or tail of the list.
*/
public interface EntryWayInterface {
/** Adds a new entry to this list T.
* @param newEntry The object to be added as a new entry.
* @return True if the addition is successful, or false if not. */
public boolean insertHead(T newEntry);
/** Deletes a new entry to this list T.
* @param newEntry The object to be added as a new entry.
* @return True if the addition is successful, or false if not. */
public boolean insertTail(T newEntry);
/** Deletes the object that is at the head of list T.
* @return the object that has been deleted and returns
* null if no object was deleted.
*/
public T deleteHead();
/** Deletes the object that is at the tail of list T.
* @return the object that has been deleted or else return null
* if no object was deleted.
*/
public T deleteTail();
/** Displays the contents of the list T.
*/
public void display();
/** See whether this list T contains a given entry.
* @param anEntry The object that is the desired entry.
* @return the position of the entry that was found.
* else returns false if the object is not in the list.
*/
public int contains(T anEntry);
/** Sees whether this list is empty.
* @return True if the list is empty, or false if not. */
public boolean isEmpty();
/** Sees whether this list is full.
* @return True if the list is full, or false if not. */
public boolean isFull();
}//end EntryWayInterface
Solution
public class LEntryWayList implements EntryWayListInterface {
private Node firstNode;
private Node lastNode;
private int length;
public LEntryWayList() {
clear();
}
public final void clear() {
firstNode = null;
lastNode = null;
length = 0;
}
/**
* Task: Places a new object at beginning of list
*
* @param newEntry is a valid object
* @return true if insert is successful; false otherwise
*/
public boolean insertHead(T newEntry) {
Node newNode = new Node(newEntry);
if (isEmpty()) {
firstNode = newNode;
lastNode = newNode;
} else {
newNode.next = firstNode;
firstNode = newNode;
}
length++;
return true;
}
/**
* Task: Places a new object at the end of the list
*
* @param newEntry is a valid object
* @return true if insertion successful; false otherwise
*/
public boolean insertTail(T newEntry) {
Node newNode = new Node(newEntry);
if (isEmpty()) {
firstNode = newNode;
} else {
lastNode.next = newNode;
}
lastNode = newNode;
length++;
return true;
}
/**
* Task: delete the object at the beginning of the list
*
* @return the object that has been deleted
*/
public T deleteHead() {
T result = null;
if (length >= 1) {
assert !isEmpty();
if (length == 1) {
result = firstNode.data;
firstNode = null;
lastNode = null;
} else {
result = firstNode.data;
firstNode = firstNode.next;
}
length--;
}
return result;
}
/**
* Task: delete the object at the end of the list
*
* @return the object that has been deleted, or null if the list was empty
*/
public T deleteTail() {
T result = null;
if (length >= 1) {
assert !isEmpty();
if (length == 1) {
result = firstNode.data;
firstNode = null;
lastNode = null;
} else {
Node nodeBefore = getNodeAt(length - 1);
Node nodeToRemove = nodeBefore.next;
nodeBefore.next = null;
result = nodeToRemove.data;
lastNode = nodeBefore;
}
length--;
}
return result;
}
/**
* Task: display the contents of the list on the console, in order, one per
* line
*/
public void display() {
Node currentNode = firstNode;
while (currentNode != null) {
System.out.println(currentNode.getData());
currentNode = currentNode.getNext();
}
}
/**
* Task: search the list for the given object and return its position in the
* list, or -1 if it's not found
*
* @param anEntry is a valid object to find in the list
* @return the position of the entry that was found, or -1 if it's not found
*/
public int contains(T anEntry) {
int found = 0;
Node currentNode = firstNode;
while (currentNode != null) {
found++;
if (anEntry.equals(currentNode.getData())) {
return found;
} else {
currentNode = currentNode.getNext();
}
}
return -1;
}
/**
* Task: check to see if list is empty
*
* @return true if list is empty, false if list contains one or more
* objects.
*/
public boolean isEmpty() {
boolean result;
if (length == 0) {
assert firstNode == null;
result = true;
} else {
assert firstNode != null;
result = false;
}
return result;
}
/**
* Task: check if list is full
*
* @return true if list is full, false if list has space for more objects
*/
public boolean isFull() {
return false; // Linked lists always return false
}
private Node getNodeAt(int givenPosition) {
assert !isEmpty()
&& ((1 <= givenPosition) && (givenPosition <= length));
Node currentNode = firstNode;
for (int counter = 1; counter < givenPosition; counter++) {
currentNode = currentNode.getNext();
}
assert currentNode != null;
return currentNode;
}
private class Node {
private T data;
private Node next;
private Node(T dataPortion) {
data = dataPortion;
next = null;
}
private Node(T dataPortion, Node nextNode) {
data = dataPortion;
next = nextNode;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
public static void main(String []args){
LEntryWayList l = new LEntryWayList();
l.display();
}
}

More Related Content

PDF
Please help me to make a programming project I have to sue them today- (1).pdf
PDF
Use the singly linked list class introduced in the lab to implement .pdf
PDF
Interface @author prmsh public interface EntryWa.pdf
PDF
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
PDF
STAGE 2 The Methods 65 points Implement all the methods t.pdf
PDF
Rewrite this code so it can use a generic type instead of integers. .pdf
PDF
Hi,I have added the methods and main class as per your requirement.pdf
PDF
-JAVA-provide a test class that do the required -you may add met.pdf
Please help me to make a programming project I have to sue them today- (1).pdf
Use the singly linked list class introduced in the lab to implement .pdf
Interface @author prmsh public interface EntryWa.pdf
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
STAGE 2 The Methods 65 points Implement all the methods t.pdf
Rewrite this code so it can use a generic type instead of integers. .pdf
Hi,I have added the methods and main class as per your requirement.pdf
-JAVA-provide a test class that do the required -you may add met.pdf

Similar to Implement the interface you wrote for Lab B (EntryWayListInterface)..pdf (20)

DOCX
Please complete all the code as per instructions in Java programming.docx
PDF
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
PDF
please i need help Im writing a program to test the merge sort alg.pdf
PDF
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PDF
Create a new java class called ListNode. Implement ListNode as a gen.pdf
PDF
For each task, submit your source java code file.(1) Objective Im.pdf
PDF
Note- Can someone help me with the public boolean isEmpty()- public bo.pdf
PDF
Lab02kdfshdfgajhdfgajhdfgajhdfgjhadgfasjhdgfjhasdgfjh.pdf
PDF
import java-util--- public class MyLinkedList{ public static void.pdf
PDF
Implementation The starter code includes List.java. You should not c.pdf
PDF
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
PDF
public class MyLinkedListltE extends ComparableltEgtg.pdf
PDF
Given below is the completed implementation of MyLinkedList class. O.pdf
PDF
Using Java programming language solve the followingSLL CLASS.pdf
PDF
Design, implement, test(In Java ) a doubly linked list ADT, using DL.pdf
PDF
Note             Given Code modified as required and required met.pdf
DOCX
Note- Can someone help me with the private E get(int index- int curren (1).docx
PDF
Class DiagramIn the Assignment #10, you are given three files Ass.pdf
PDF
a) Complete both insert and delete methods. If it works correctly 10.pdf
PDF
hi i have to write a java program involving link lists. i have a pro.pdf
Please complete all the code as per instructions in Java programming.docx
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
please i need help Im writing a program to test the merge sort alg.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
Create a new java class called ListNode. Implement ListNode as a gen.pdf
For each task, submit your source java code file.(1) Objective Im.pdf
Note- Can someone help me with the public boolean isEmpty()- public bo.pdf
Lab02kdfshdfgajhdfgajhdfgajhdfgjhadgfasjhdgfjhasdgfjh.pdf
import java-util--- public class MyLinkedList{ public static void.pdf
Implementation The starter code includes List.java. You should not c.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
public class MyLinkedListltE extends ComparableltEgtg.pdf
Given below is the completed implementation of MyLinkedList class. O.pdf
Using Java programming language solve the followingSLL CLASS.pdf
Design, implement, test(In Java ) a doubly linked list ADT, using DL.pdf
Note             Given Code modified as required and required met.pdf
Note- Can someone help me with the private E get(int index- int curren (1).docx
Class DiagramIn the Assignment #10, you are given three files Ass.pdf
a) Complete both insert and delete methods. If it works correctly 10.pdf
hi i have to write a java program involving link lists. i have a pro.pdf
Ad

More from rishabjain5053 (20)

PDF
write a short essay report to describe a topic of your interest rela.pdf
PDF
Write a C++ program to do the followingProject Name Computer Sho.pdf
PDF
Which statement pertaining to Port Scanning is FALSEA technique use.pdf
PDF
When did almost all animal phyla divergeSolutionAccording to .pdf
PDF
What is the nipah virus Describe mechanism of infection How is it .pdf
PDF
What is organizational inertia List some sources of inertia in a co.pdf
PDF
Use fundamental identities to write tan t sec2t in terms of sint only.pdf
PDF
Twenty-five people responded to a questionnaire about what types of p.pdf
PDF
The universe began 13 7 billion years ago, is most directly an exa.pdf
PDF
The Stevens Company provided $57000 of services on acco.pdf
PDF
The papillae on the tongue that do not contain any taste buds are the.pdf
PDF
QUESTION 13 Which of the following benefits is a component of Social .pdf
PDF
Padre, Inc., buys 80 percent of the outstanding common stock of Sier.pdf
PDF
Operating Systems Structure1- Explain briefly why the objectives o.pdf
PDF
Need help with this java code. fill in lines where needed. Also, not.pdf
PDF
IP has no mechanism for error reporting or error-correcting. ICMPv4 .pdf
PDF
Informed Consent is a significant requirement for all provider types.pdf
PDF
In the MVC Architecture, why is it important to keep the model, view.pdf
PDF
in roughly 400 words please describe3 affective outcomes of diver.pdf
PDF
Im having issues with two homework questions. I get the gist of th.pdf
write a short essay report to describe a topic of your interest rela.pdf
Write a C++ program to do the followingProject Name Computer Sho.pdf
Which statement pertaining to Port Scanning is FALSEA technique use.pdf
When did almost all animal phyla divergeSolutionAccording to .pdf
What is the nipah virus Describe mechanism of infection How is it .pdf
What is organizational inertia List some sources of inertia in a co.pdf
Use fundamental identities to write tan t sec2t in terms of sint only.pdf
Twenty-five people responded to a questionnaire about what types of p.pdf
The universe began 13 7 billion years ago, is most directly an exa.pdf
The Stevens Company provided $57000 of services on acco.pdf
The papillae on the tongue that do not contain any taste buds are the.pdf
QUESTION 13 Which of the following benefits is a component of Social .pdf
Padre, Inc., buys 80 percent of the outstanding common stock of Sier.pdf
Operating Systems Structure1- Explain briefly why the objectives o.pdf
Need help with this java code. fill in lines where needed. Also, not.pdf
IP has no mechanism for error reporting or error-correcting. ICMPv4 .pdf
Informed Consent is a significant requirement for all provider types.pdf
In the MVC Architecture, why is it important to keep the model, view.pdf
in roughly 400 words please describe3 affective outcomes of diver.pdf
Im having issues with two homework questions. I get the gist of th.pdf
Ad

Recently uploaded (20)

PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Cell Structure & Organelles in detailed.
PPTX
Pharma ospi slides which help in ospi learning
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
Classroom Observation Tools for Teachers
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Lesson notes of climatology university.
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Computing-Curriculum for Schools in Ghana
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Final Presentation General Medicine 03-08-2024.pptx
Cell Structure & Organelles in detailed.
Pharma ospi slides which help in ospi learning
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Classroom Observation Tools for Teachers
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Chinmaya Tiranga quiz Grand Finale.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Lesson notes of climatology university.
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Microbial diseases, their pathogenesis and prophylaxis
Computing-Curriculum for Schools in Ghana

Implement the interface you wrote for Lab B (EntryWayListInterface)..pdf

  • 1. Implement the interface you wrote for Lab B (EntryWayListInterface). As a reminder, EntryWayListInterface allows the user to access to list elements only through the beginning and end of the list. Your implementation can use either an expandable array or linked nodes- it is your choice. You can decide what instance variables are needed. You must implement every method from the interface. Make sure to account for special conditions such as empty lists and singleton lists. Note: your instance data variable should be either a) an array or b) one or more nodes. It should not be an AList or LList object or an ArrayList object. Your implementation must: compile contain these implemented methods: insertHead insertTail deleteHead deleteTail display contains isEmpty isFull Also create a driver program to test your implementation. The driver program will operate from the client perspective. Your driver program should: display an empty list add five entries to the list- some at the head and some at the tail display the list remove the first entry remove the last entry display the list test to see if elements are in the list (test one element that is in the list and one that is not) remove the last three elements in the list try to remove an element from the empty list Write a second class to implement EntryWayListInterface. Instead of using an array or linked nodes, use either an AList or LList object as your instance data variable. In this way, you are using an existing data type (AList or LList) to implement a new data type (EntryWayListInterface). Inside the methods of this class, invoke methods on the AList or LList object to accomplish the task.
  • 2. /** An interface for the ADT list. Entries in a list have positions that begin with 1. * Accesses only entries that are the head or tail of the list. */ public interface EntryWayInterface { /** Adds a new entry to this list T. * @param newEntry The object to be added as a new entry. * @return True if the addition is successful, or false if not. */ public boolean insertHead(T newEntry); /** Deletes a new entry to this list T. * @param newEntry The object to be added as a new entry. * @return True if the addition is successful, or false if not. */ public boolean insertTail(T newEntry); /** Deletes the object that is at the head of list T. * @return the object that has been deleted and returns * null if no object was deleted. */ public T deleteHead(); /** Deletes the object that is at the tail of list T. * @return the object that has been deleted or else return null * if no object was deleted. */ public T deleteTail(); /** Displays the contents of the list T. */ public void display(); /** See whether this list T contains a given entry. * @param anEntry The object that is the desired entry. * @return the position of the entry that was found. * else returns false if the object is not in the list. */ public int contains(T anEntry); /** Sees whether this list is empty. * @return True if the list is empty, or false if not. */ public boolean isEmpty();
  • 3. /** Sees whether this list is full. * @return True if the list is full, or false if not. */ public boolean isFull(); }//end EntryWayInterface Solution public class LEntryWayList implements EntryWayListInterface { private Node firstNode; private Node lastNode; private int length; public LEntryWayList() { clear(); } public final void clear() { firstNode = null; lastNode = null; length = 0; } /** * Task: Places a new object at beginning of list * * @param newEntry is a valid object * @return true if insert is successful; false otherwise */ public boolean insertHead(T newEntry) { Node newNode = new Node(newEntry); if (isEmpty()) { firstNode = newNode; lastNode = newNode; } else { newNode.next = firstNode; firstNode = newNode; } length++;
  • 4. return true; } /** * Task: Places a new object at the end of the list * * @param newEntry is a valid object * @return true if insertion successful; false otherwise */ public boolean insertTail(T newEntry) { Node newNode = new Node(newEntry); if (isEmpty()) { firstNode = newNode; } else { lastNode.next = newNode; } lastNode = newNode; length++; return true; } /** * Task: delete the object at the beginning of the list * * @return the object that has been deleted */ public T deleteHead() { T result = null; if (length >= 1) { assert !isEmpty(); if (length == 1) { result = firstNode.data; firstNode = null; lastNode = null; } else { result = firstNode.data; firstNode = firstNode.next; }
  • 5. length--; } return result; } /** * Task: delete the object at the end of the list * * @return the object that has been deleted, or null if the list was empty */ public T deleteTail() { T result = null; if (length >= 1) { assert !isEmpty(); if (length == 1) { result = firstNode.data; firstNode = null; lastNode = null; } else { Node nodeBefore = getNodeAt(length - 1); Node nodeToRemove = nodeBefore.next; nodeBefore.next = null; result = nodeToRemove.data; lastNode = nodeBefore; } length--; } return result; } /** * Task: display the contents of the list on the console, in order, one per * line */ public void display() { Node currentNode = firstNode; while (currentNode != null) { System.out.println(currentNode.getData());
  • 6. currentNode = currentNode.getNext(); } } /** * Task: search the list for the given object and return its position in the * list, or -1 if it's not found * * @param anEntry is a valid object to find in the list * @return the position of the entry that was found, or -1 if it's not found */ public int contains(T anEntry) { int found = 0; Node currentNode = firstNode; while (currentNode != null) { found++; if (anEntry.equals(currentNode.getData())) { return found; } else { currentNode = currentNode.getNext(); } } return -1; } /** * Task: check to see if list is empty * * @return true if list is empty, false if list contains one or more * objects. */ public boolean isEmpty() { boolean result; if (length == 0) { assert firstNode == null; result = true; } else { assert firstNode != null;
  • 7. result = false; } return result; } /** * Task: check if list is full * * @return true if list is full, false if list has space for more objects */ public boolean isFull() { return false; // Linked lists always return false } private Node getNodeAt(int givenPosition) { assert !isEmpty() && ((1 <= givenPosition) && (givenPosition <= length)); Node currentNode = firstNode; for (int counter = 1; counter < givenPosition; counter++) { currentNode = currentNode.getNext(); } assert currentNode != null; return currentNode; } private class Node { private T data; private Node next; private Node(T dataPortion) { data = dataPortion; next = null; } private Node(T dataPortion, Node nextNode) { data = dataPortion; next = nextNode; } public T getData() { return data; }
  • 8. public void setData(T data) { this.data = data; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; } } public static void main(String []args){ LEntryWayList l = new LEntryWayList(); l.display(); } }