Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Iterator So I have this This is the full prompt I am stuck on the recursive part import java.util.ArrayList; public class GLProject { public

Java Iterator

So I have this

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

This is the full prompt I am stuck on the recursive part

image text in transcribedimage text in transcribedimage text in transcribed

import java.util.ArrayList; public class GLProject { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("hello generic lists"); MI Genericlist E abstract class GenericList { // inner class public class Node{ private I data; private Node next; // constructor public Node(){ this.data=null; this.next=null; // constructor with parameters public Node(1 d) { this.data=d; this.next=null; // getter and setter public Node getNext() { return next; public void setNext (Node next) { this.next = next; public I getData() { return data; public void setData(T data) { this.data = data; // elements private Node head; private int length; Il print public void print(){ System.out.println(""); // add data public abstract void add(I data); public I delete(){ return null; // return arraylist public ArrayList dumpList(){ return null; // setter and getter public void setLength(int i){ this.length=1; public void setHead (Node h) { this.head=h; public int getLength(){ return(this.length); public public Node getHead(){ return(this.head); ////////////////////// GenericStack.java class GenericStack extends Genericlist { // constructor public GenericStack({ this.setHead(null); this.setLength(); // implement add function @Override public void add(I data) { Node node=new Node(data); // if head is null if(this.getHead()==null){ this.setHead (node); this.setLength(this.getLength()+1); // head is not null else{ Node n=this.getHead(); Node cur=n; while(cur!=null){ if(cur.getNext()==null){ cur.setNext(node); else{ cur=cur.getNext(); // set new length this.setLength(this.getLength() +1); // set head with linked node this.setHead(n); // delete from last public I delete() { Node n=this.getHead(); Node cur=n; Node ret=null, prev=null; while(cur!=null){ if(cur.getNext()==null){ ret-cur; // delete node if(cur==n) { cur=null; else{ prev.setNext(null); this.setLength(this.getLength(-1); break; else{ prev=cur; cur=cur.getNext(); // set head with linked node this.setHead(n); // return value if(ret==null){ return null; else{ return ret.getData(); // push method public void push(I _data) { this.add(_data); // pop method public I pop() { return(this.delete()); // top element public I top() { Node n=this.getHead(); Node cur=n; Node ret=null; // find last element while(cur!=null){ if(cur.getNext()==null){ ret=cur; break; else{ cur=cur.getNext(); // return data if(ret==null){ return null; else{ return ret.getData(); // store all linked list data in arraylist public ArrayList dumpList0{ ArrayList list=new ArrayList(); Node n=this.getHead(); Node cur=n; while(cur!=null){ list.add(cur.getData()); cur=cur.getNext(); return list; Il print list data public void print(){ ArrayList list=this.dumpList(); for(int i=0;i extends Genericlist { // constructor public GenericQueue() { this.setHead(null); this.setLength(); // add data in linked list @Override public void add(I data) { Node node = new Node(data); // if head is null if (this.getHead() == null) { this.setHead(node); this.setLength(this.getLength() + 1); // if head is not empty else { Node n = this.getHead(); Node cur = n; while (cur != null) { if (cur.getNext() == null) { cur.setNext(node); break; } else { cur = cur.getNext(); this.setLength(this.getLength() + 1); this.setHead(n); Il delete first data public I delete() { Node n = this.getHead(); Node ret = null; ret = n; //if null, that that mean already empty if (ret == null) { return null; } else { // set head to next element n = n.getNext(); this.setHead(n); // set new length this.setLength(this.getLength() - 1); return ret.getData(); // push element public void push(I _data) { this.add(_data); // pop element public I pop() { return (this.delete()); // return top element public I top() { Node n = this.getHead(); Node ret = n; if (ret == null) { return null; } else { return ret.getData(); // store all linked list data in arraylist public ArrayList dumpList() { ArrayList list = new ArrayList(); Node n = this.getHead(); Node cur = n; while (cur != null) { list.add(cur.getData()); cur = cur.getNext(); return list; // print list data public void print() { ArrayList list = this.dumpList(); for (int i = 0; i head (this is the head of the list and should be private). int length (the length of the list and should be private) This class should include the following methods: print(): prints the items of the list, one value per line. If the list is empty, print "Empty List. add(I data): adds the value to the list. This method is abstract since the implementation depends on what the data structure is. public I delete(): returns the first value of the list and deletes the node. If the list is empty, return null. public ArrayList dumpList(): this method stores and returns all values currently in the list into an ArrayList and returns it. At the end of this method, your list should be empty. getLength() setLength() getHead() setHead(), these are getters/setters for private data members head and length. This class should also define a generic inner class Node: It will include two fields: T data and Nodenext; ***This class encapsulates a linked list. Defining a Node class and providing two methods that a queue and stack have in common while leaving adding a node to each implementation that inherits from it.*** Create two more classes Generic Queue and Generic Stack. They both should inherit from GenericList. The constructors for each class will take one parameter. That parameter will be a value that will go in the first node of the list encapsulated by each instance of the class. Each constructor should initialize the linked list head, with the value passed in by the constructor. Each class should also implement the method add(l data), GenericQueue will add to the back of the list while Generic Stack will add to the front. Each class must also keep track of the length of it's list using the length data field defined in the abstract class. Generic Queue will have the methods enqueue(l data) and public I dequeue() which will call the methods add(data) and delete() respectively. Enqueue and dequeue merely call add(l data) and delete(). The reason for this is that a user would expect these calls to be implemented in each of those data structures. You will do the same with Generic Stack Generic Stack will have the methods push(I data) and public I pop() which will call the methods add(l data) and delete() respectively. Once implemented, you should be able to create instances of both GenericQueue and Generic Stack in main with most primitive wrapper classes. You should be able to add and delete nodes to each data structure instance as well as print out the entire list and check for the length of the list. You must follow this implementation: meaning you can not add any additional data fields or classes. You may add getters/setters as need be. Implementing Iterator Design Pattern: You are to create an interface called Createlterator. It will have one abstract method: Iterator createlterator(). Since both Generic Queue and Generic Stack classes iterate through a list the same way, you will want to implement this interface in GenericList. You must also create a class to contain logic for iterating through your data structure. Call this class GLIterator. GLIterator should be a generic class since it provides the logic to iterate through a generic stack and queue. It should implement the java interface Iterator (java.util.Iterator). You will have to implement two inherited methods: public boolean hasNext(), checks to see if there is another value in the data structure and returns true or false, and public I next(), returns the current value in the data structure and advances to the next item. import java.util.ArrayList; public class GLProject { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("hello generic lists"); MI Genericlist E abstract class GenericList { // inner class public class Node{ private I data; private Node next; // constructor public Node(){ this.data=null; this.next=null; // constructor with parameters public Node(1 d) { this.data=d; this.next=null; // getter and setter public Node getNext() { return next; public void setNext (Node next) { this.next = next; public I getData() { return data; public void setData(T data) { this.data = data; // elements private Node head; private int length; Il print public void print(){ System.out.println(""); // add data public abstract void add(I data); public I delete(){ return null; // return arraylist public ArrayList dumpList(){ return null; // setter and getter public void setLength(int i){ this.length=1; public void setHead (Node h) { this.head=h; public int getLength(){ return(this.length); public public Node getHead(){ return(this.head); ////////////////////// GenericStack.java class GenericStack extends Genericlist { // constructor public GenericStack({ this.setHead(null); this.setLength(); // implement add function @Override public void add(I data) { Node node=new Node(data); // if head is null if(this.getHead()==null){ this.setHead (node); this.setLength(this.getLength()+1); // head is not null else{ Node n=this.getHead(); Node cur=n; while(cur!=null){ if(cur.getNext()==null){ cur.setNext(node); else{ cur=cur.getNext(); // set new length this.setLength(this.getLength() +1); // set head with linked node this.setHead(n); // delete from last public I delete() { Node n=this.getHead(); Node cur=n; Node ret=null, prev=null; while(cur!=null){ if(cur.getNext()==null){ ret-cur; // delete node if(cur==n) { cur=null; else{ prev.setNext(null); this.setLength(this.getLength(-1); break; else{ prev=cur; cur=cur.getNext(); // set head with linked node this.setHead(n); // return value if(ret==null){ return null; else{ return ret.getData(); // push method public void push(I _data) { this.add(_data); // pop method public I pop() { return(this.delete()); // top element public I top() { Node n=this.getHead(); Node cur=n; Node ret=null; // find last element while(cur!=null){ if(cur.getNext()==null){ ret=cur; break; else{ cur=cur.getNext(); // return data if(ret==null){ return null; else{ return ret.getData(); // store all linked list data in arraylist public ArrayList dumpList0{ ArrayList list=new ArrayList(); Node n=this.getHead(); Node cur=n; while(cur!=null){ list.add(cur.getData()); cur=cur.getNext(); return list; Il print list data public void print(){ ArrayList list=this.dumpList(); for(int i=0;i extends Genericlist { // constructor public GenericQueue() { this.setHead(null); this.setLength(); // add data in linked list @Override public void add(I data) { Node node = new Node(data); // if head is null if (this.getHead() == null) { this.setHead(node); this.setLength(this.getLength() + 1); // if head is not empty else { Node n = this.getHead(); Node cur = n; while (cur != null) { if (cur.getNext() == null) { cur.setNext(node); break; } else { cur = cur.getNext(); this.setLength(this.getLength() + 1); this.setHead(n); Il delete first data public I delete() { Node n = this.getHead(); Node ret = null; ret = n; //if null, that that mean already empty if (ret == null) { return null; } else { // set head to next element n = n.getNext(); this.setHead(n); // set new length this.setLength(this.getLength() - 1); return ret.getData(); // push element public void push(I _data) { this.add(_data); // pop element public I pop() { return (this.delete()); // return top element public I top() { Node n = this.getHead(); Node ret = n; if (ret == null) { return null; } else { return ret.getData(); // store all linked list data in arraylist public ArrayList dumpList() { ArrayList list = new ArrayList(); Node n = this.getHead(); Node cur = n; while (cur != null) { list.add(cur.getData()); cur = cur.getNext(); return list; // print list data public void print() { ArrayList list = this.dumpList(); for (int i = 0; i head (this is the head of the list and should be private). int length (the length of the list and should be private) This class should include the following methods: print(): prints the items of the list, one value per line. If the list is empty, print "Empty List. add(I data): adds the value to the list. This method is abstract since the implementation depends on what the data structure is. public I delete(): returns the first value of the list and deletes the node. If the list is empty, return null. public ArrayList dumpList(): this method stores and returns all values currently in the list into an ArrayList and returns it. At the end of this method, your list should be empty. getLength() setLength() getHead() setHead(), these are getters/setters for private data members head and length. This class should also define a generic inner class Node: It will include two fields: T data and Nodenext; ***This class encapsulates a linked list. Defining a Node class and providing two methods that a queue and stack have in common while leaving adding a node to each implementation that inherits from it.*** Create two more classes Generic Queue and Generic Stack. They both should inherit from GenericList. The constructors for each class will take one parameter. That parameter will be a value that will go in the first node of the list encapsulated by each instance of the class. Each constructor should initialize the linked list head, with the value passed in by the constructor. Each class should also implement the method add(l data), GenericQueue will add to the back of the list while Generic Stack will add to the front. Each class must also keep track of the length of it's list using the length data field defined in the abstract class. Generic Queue will have the methods enqueue(l data) and public I dequeue() which will call the methods add(data) and delete() respectively. Enqueue and dequeue merely call add(l data) and delete(). The reason for this is that a user would expect these calls to be implemented in each of those data structures. You will do the same with Generic Stack Generic Stack will have the methods push(I data) and public I pop() which will call the methods add(l data) and delete() respectively. Once implemented, you should be able to create instances of both GenericQueue and Generic Stack in main with most primitive wrapper classes. You should be able to add and delete nodes to each data structure instance as well as print out the entire list and check for the length of the list. You must follow this implementation: meaning you can not add any additional data fields or classes. You may add getters/setters as need be. Implementing Iterator Design Pattern: You are to create an interface called Createlterator. It will have one abstract method: Iterator createlterator(). Since both Generic Queue and Generic Stack classes iterate through a list the same way, you will want to implement this interface in GenericList. You must also create a class to contain logic for iterating through your data structure. Call this class GLIterator. GLIterator should be a generic class since it provides the logic to iterate through a generic stack and queue. It should implement the java interface Iterator (java.util.Iterator). You will have to implement two inherited methods: public boolean hasNext(), checks to see if there is another value in the data structure and returns true or false, and public I next(), returns the current value in the data structure and advances to the next item

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

Databases In Networked Information Systems 6th International Workshop Dnis 2010 Aizu Wakamatsu Japan March 2010 Proceedings Lncs 5999

Authors: Shinji Kikuchi ,Shelly Sachdeva ,Subhash Bhalla

2010th Edition

3642120377, 978-3642120374

More Books

Students also viewed these Databases questions