SlideShare a Scribd company logo
1 of 7
Download to read offline
I need help implementing a Stack with this java programming assignment:
Given a doubly linked list (code provided below) implement a Stack. You must use the doubly
link list class and utilize it as an object in your implementation of the stack.
A Stack is a Last in First Out (LiFO) structure which translates to the processes of outputting the
last element which was inputted in the collection. The Stack is based on the process of putting
things on top of one another and taking the item on top (think of a stack of papers).
Operations to implement:
push (E): Add an element to the start of the sequence
pop: Remove an element from the start of the sequence
Peek: Return the first element of the sequence without removing it
atIndex(x): Return the element at the given index (x) Or throw an exception if it is out of bound
(if you can control the user input then do that instead)
Size: Return the size of the Stack
isEmpty: Boolean, returns true if the Stack is empty
Empty: Empty the Stack
Code Part 1:
public class DoublyLinkedList{
private DNode head;
private DNode tail;
private int size;
public DoublyLinkedList(){ // construct an empty list
this.tail = new DNode (null, null, this.head);
this.head = new DNode (null, this.tail, null);
this.size = 0;
}
public DoublyLinkedList(DNode next){ // constructs a list
// out of a single node
this.tail = new DNode (null, null, next);
this.head = new DNode (null, next, null);
next.changeNext(this.tail);
next.changePrev(this.head);
this.size = 1;
}
public DoublyLinkedList(Object [] objectArray){ // construct a list out of
// an array
this.tail = new DNode (null, null, this.head);
this.head = new DNode (null, this.tail, null);
DNode temp = this.head;
for (Object e : objectArray)
{
//Anonomus function
new DNode (e, temp.getNext(),temp).insertBetweenNodes(temp, temp.getNext());
temp = temp.getNext();
this.size += 1;
}
}
public void addToFrontofList(Object toAdd){ // Appends the begining
// of the list
DNode temp = new DNode (toAdd, this.head.getNext(), this.head);
this.head.getNext().changePrev(temp);
this.head.changeNext(temp);
this.size += 1;
}
public void addToendofList (Object toAdd) // appends the end of the list
// with a node
{
DNode temp = new DNode (toAdd, this.tail, this.tail.getPrev());
this.tail.getPrev().changeNext(temp);
this.tail.changePrev(temp);
this.size += 1;
}
public void insertAfterNode(DNode current, Object input)// Inserts a new
// a new node after
// current node
{
current.insertAfterNode(input);
this.size += 1;
}
public int getSize() // returns the size of the list
{
return this.size;
}
public String toString()
{
String result = "";
for (DNode temp = this.head.getNext();
temp.hasNext(); temp = temp.getNext())
{
result += temp.getValue();
result += " -> ";
}
result += "End of list";
return result;
}
}
and code part2:
public class DNode{
private Object e; // place holder for the type of object the
//collection is storing
private DNode next; // reference to the next node in the sequence
// of nodes making up the linked list. Remember
// that even though Java passes Objects by value
// an object consisting of reference will behave
// the same as an object passed by reference unless
// a deep copy was made.
private DNode prev;
public DNode(Object e){ // Constructor for implementing a single node
this.e = e; // The value of the element to be assigned to
// this particular instance of node
this.next = null; // An empty reference since there is no node
// to follow.
this.prev = null;
}
public DNode(Object e, DNode next, DNode prev){ // Constructor for
//implementing a node that
//comes after another node
this.e = e; // The value of the element to be assigned to
// this particular instance of node
this.next = next; // reference to the subsequent node to follow
this.prev = prev;
}
public void changeNext(DNode next){ // Changes the link to the next node
this.next = next;
}
public void changePrev(DNode prev){ // Changes the link to the next node
this.prev = prev;
}
public DNode getNext(){ // Returns the node next in the sequence
return this.next;
}
public Object getValue(){ // Returns the value a node is holding
return this.e;
}
public Boolean hasNext(){ // Returns a boolean determining regarding
// the status of subsequent nodes after
// the current node
return !(this.next.getValue() == null || this.next == null);
}
public Boolean hasprev(){ // Returns a boolean determining regarding
// the status of subsequent nodes after
// the current node
return !(this.prev.getValue() == null || this.prev == null);
}
public void insertAfterNode( Object input){
this.changeNext(new DNode (input, this.getNext(), this));
}
public void insertAfterNode(DNode input){
this.changeNext(input);
input.changeNext(this.next);
input.changePrev(this);
}
public void insertbeforeNode(DNode input){
this.changeNext(input);
}
public void insertBetweenNodes(DNode before, DNode after){
this.changeNext(after);
this.changePrev(before);
before.changeNext(this);
after.changePrev(this);
}
public DNode getPrev(){
return this.prev;
}
}
Solution
Following is the program that contain functions like push,pop,isEmpty etc. call this function with
proper data.
import java.util.NoSuchElementException;
public class DoublyLinkedListImpl {
private Node head;
private Node tail;
private int size;
public DoublyLinkedListImpl() {
size = 0;
}
// this class keeps track all element info
private class Node {
E element;
Node next;
Node prev;
public Node(E element, Node next, Node prev) {
this.element = element;
this.next = next;
this.prev = prev;
}
}
// returns the size of the linked list
public int size() { return size; }
// return whether the list is empty or not
public boolean isEmpty() { return size == 0; }
// adds element at the starting of the linked list
public void push(E element) {
Node tmp = new Node(element, head, null);
if(head != null ) {head.prev = tmp;}
head = tmp;
if(tail == null) { tail = tmp;}
size++;
System.out.println("adding: "+element);
}
// this method removes element from the start of the linked list
public E pop() {
if (size == 0) throw new NoSuchElementException();
Node tmp = head;
head = head.next;
head.prev = null;
size--;
System.out.println("deleted: "+tmp.element);
return tmp.element;
}
public E Peek() {
if (isEmpty()) throw new NoSuchElementException("Stack underflow");
return head.element;
}
public void remove()
{
head = null;
}
// Takes index as argument and return data at index
public E GetNth(int index)
{
Node current = head;
int count = 0;
while (current != null)
{
if (count == index)
return current.element;
count++;
current = current.next;
}
assert(false);
return current.element;
}
public static void main(String a[]){
// Testing data to test the functions
DoublyLinkedListImpl dll = new DoublyLinkedListImpl();
dll.remove();
dll.push(10);
dll.push(34);
dll.size();
dll.Peek();
dll.GetNth(2);
dll.pop();
}
}

More Related Content

Similar to I need help implementing a Stack with this java programming assignme.pdf

PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdfPROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdfclimatecontrolsv
 
To complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdfTo complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdfezycolours78
 
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdfimport java-util-Iterator- import java-util-NoSuchElementException- im.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdfStewart29UReesa
 
Sorted number list implementation with linked listsStep 1 Inspec.pdf
 Sorted number list implementation with linked listsStep 1 Inspec.pdf Sorted number list implementation with linked listsStep 1 Inspec.pdf
Sorted number list implementation with linked listsStep 1 Inspec.pdfalmaniaeyewear
 
Hi, Please find my code.I have correted all of your classes.Plea.pdf
Hi, Please find my code.I have correted all of your classes.Plea.pdfHi, Please find my code.I have correted all of your classes.Plea.pdf
Hi, Please find my code.I have correted all of your classes.Plea.pdfpritikulkarni20
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfEricvtJFraserr
 
JAVA A double-ended queue is a list that allows the addition and.pdf
JAVA A double-ended queue is a list that allows the addition and.pdfJAVA A double-ended queue is a list that allows the addition and.pdf
JAVA A double-ended queue is a list that allows the addition and.pdfamrishinda
 
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
Java Foundations StackADT-java ---  - Defines the interface to a stack.docxJava Foundations StackADT-java ---  - Defines the interface to a stack.docx
Java Foundations StackADT-java --- - Defines the interface to a stack.docxVictorXUQGloverl
 
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdfHow do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdfmail931892
 
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdfWrite a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdfrozakashif85
 
import java.util.Iterator; import java.util.NoSuchElementException; .pdf
  import java.util.Iterator; import java.util.NoSuchElementException; .pdf  import java.util.Iterator; import java.util.NoSuchElementException; .pdf
import java.util.Iterator; import java.util.NoSuchElementException; .pdfdeepakangel
 
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdfHow do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdfmail931892
 
Hi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdfHi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdfannaelctronics
 
Main issues with the following code-Im not sure if its reading the fil.pdf
Main issues with the following code-Im not sure if its reading the fil.pdfMain issues with the following code-Im not sure if its reading the fil.pdf
Main issues with the following code-Im not sure if its reading the fil.pdfaonesalem
 
Design, implement, test(In Java ) a doubly linked list ADT, using DL.pdf
Design, implement, test(In Java ) a doubly linked list ADT, using DL.pdfDesign, implement, test(In Java ) a doubly linked list ADT, using DL.pdf
Design, implement, test(In Java ) a doubly linked list ADT, using DL.pdftrishacolsyn25353
 
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdfUsing NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdfsiennatimbok52331
 
How do I fix it in javaLinkedList.java Defines a doubl.pdf
How do I fix it in javaLinkedList.java Defines a doubl.pdfHow do I fix it in javaLinkedList.java Defines a doubl.pdf
How do I fix it in javaLinkedList.java Defines a doubl.pdffmac5
 
The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
 The MyLinkedList class used in Listing 24.6 is a one-way directional .docx The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
The MyLinkedList class used in Listing 24.6 is a one-way directional .docxKomlin1
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfrohit219406
 
How do I fix it in LinkedList.javaLinkedList.java Define.pdf
How do I fix it in LinkedList.javaLinkedList.java Define.pdfHow do I fix it in LinkedList.javaLinkedList.java Define.pdf
How do I fix it in LinkedList.javaLinkedList.java Define.pdfmail931892
 

Similar to I need help implementing a Stack with this java programming assignme.pdf (20)

PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdfPROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
 
To complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdfTo complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdf
 
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdfimport java-util-Iterator- import java-util-NoSuchElementException- im.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
 
Sorted number list implementation with linked listsStep 1 Inspec.pdf
 Sorted number list implementation with linked listsStep 1 Inspec.pdf Sorted number list implementation with linked listsStep 1 Inspec.pdf
Sorted number list implementation with linked listsStep 1 Inspec.pdf
 
Hi, Please find my code.I have correted all of your classes.Plea.pdf
Hi, Please find my code.I have correted all of your classes.Plea.pdfHi, Please find my code.I have correted all of your classes.Plea.pdf
Hi, Please find my code.I have correted all of your classes.Plea.pdf
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdf
 
JAVA A double-ended queue is a list that allows the addition and.pdf
JAVA A double-ended queue is a list that allows the addition and.pdfJAVA A double-ended queue is a list that allows the addition and.pdf
JAVA A double-ended queue is a list that allows the addition and.pdf
 
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
Java Foundations StackADT-java ---  - Defines the interface to a stack.docxJava Foundations StackADT-java ---  - Defines the interface to a stack.docx
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
 
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdfHow do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
 
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdfWrite a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
 
import java.util.Iterator; import java.util.NoSuchElementException; .pdf
  import java.util.Iterator; import java.util.NoSuchElementException; .pdf  import java.util.Iterator; import java.util.NoSuchElementException; .pdf
import java.util.Iterator; import java.util.NoSuchElementException; .pdf
 
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdfHow do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
 
Hi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdfHi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdf
 
Main issues with the following code-Im not sure if its reading the fil.pdf
Main issues with the following code-Im not sure if its reading the fil.pdfMain issues with the following code-Im not sure if its reading the fil.pdf
Main issues with the following code-Im not sure if its reading the fil.pdf
 
Design, implement, test(In Java ) a doubly linked list ADT, using DL.pdf
Design, implement, test(In Java ) a doubly linked list ADT, using DL.pdfDesign, implement, test(In Java ) a doubly linked list ADT, using DL.pdf
Design, implement, test(In Java ) a doubly linked list ADT, using DL.pdf
 
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdfUsing NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
 
How do I fix it in javaLinkedList.java Defines a doubl.pdf
How do I fix it in javaLinkedList.java Defines a doubl.pdfHow do I fix it in javaLinkedList.java Defines a doubl.pdf
How do I fix it in javaLinkedList.java Defines a doubl.pdf
 
The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
 The MyLinkedList class used in Listing 24.6 is a one-way directional .docx The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdf
 
How do I fix it in LinkedList.javaLinkedList.java Define.pdf
How do I fix it in LinkedList.javaLinkedList.java Define.pdfHow do I fix it in LinkedList.javaLinkedList.java Define.pdf
How do I fix it in LinkedList.javaLinkedList.java Define.pdf
 

More from sauravmanwanicp

Are hydrophobic interactions weak or strongSolutionHydrophobi.pdf
Are hydrophobic interactions weak or strongSolutionHydrophobi.pdfAre hydrophobic interactions weak or strongSolutionHydrophobi.pdf
Are hydrophobic interactions weak or strongSolutionHydrophobi.pdfsauravmanwanicp
 
What is another word for account based forecast What is anothe.pdf
What is another word for account based forecast What is anothe.pdfWhat is another word for account based forecast What is anothe.pdf
What is another word for account based forecast What is anothe.pdfsauravmanwanicp
 
What function can be used to return multiple numeric data such as in.pdf
What function can be used to return multiple numeric data such as in.pdfWhat function can be used to return multiple numeric data such as in.pdf
What function can be used to return multiple numeric data such as in.pdfsauravmanwanicp
 
Answer the following questions in reference to Drosophila melanogast.pdf
Answer the following questions in reference to Drosophila melanogast.pdfAnswer the following questions in reference to Drosophila melanogast.pdf
Answer the following questions in reference to Drosophila melanogast.pdfsauravmanwanicp
 
There are five feasible solutions to a three-objective optimization p.pdf
There are five feasible solutions to a three-objective optimization p.pdfThere are five feasible solutions to a three-objective optimization p.pdf
There are five feasible solutions to a three-objective optimization p.pdfsauravmanwanicp
 
An air-filled parallel-plate capacitor has capacitance Co. If two ide.pdf
An air-filled parallel-plate capacitor has capacitance Co. If two ide.pdfAn air-filled parallel-plate capacitor has capacitance Co. If two ide.pdf
An air-filled parallel-plate capacitor has capacitance Co. If two ide.pdfsauravmanwanicp
 
The incandescent lightbulb was actually invented by Humphry Davy in 1.pdf
The incandescent lightbulb was actually invented by Humphry Davy in 1.pdfThe incandescent lightbulb was actually invented by Humphry Davy in 1.pdf
The incandescent lightbulb was actually invented by Humphry Davy in 1.pdfsauravmanwanicp
 
The fossil record provides little information about ancient mosses. D.pdf
The fossil record provides little information about ancient mosses. D.pdfThe fossil record provides little information about ancient mosses. D.pdf
The fossil record provides little information about ancient mosses. D.pdfsauravmanwanicp
 
The DNA sequence below is a shortened version of the sequence on the .pdf
The DNA sequence below is a shortened version of the sequence on the .pdfThe DNA sequence below is a shortened version of the sequence on the .pdf
The DNA sequence below is a shortened version of the sequence on the .pdfsauravmanwanicp
 
the conducting tissue found in vascular plants that functions in tra.pdf
the conducting tissue found in vascular plants that functions in tra.pdfthe conducting tissue found in vascular plants that functions in tra.pdf
the conducting tissue found in vascular plants that functions in tra.pdfsauravmanwanicp
 
Show that f(x)=57^x+1 and f^-1(x)=7x-75 are inverses Choose the ap.pdf
Show that f(x)=57^x+1 and f^-1(x)=7x-75 are inverses  Choose the ap.pdfShow that f(x)=57^x+1 and f^-1(x)=7x-75 are inverses  Choose the ap.pdf
Show that f(x)=57^x+1 and f^-1(x)=7x-75 are inverses Choose the ap.pdfsauravmanwanicp
 
Read the short article Horizontal Gene Transfer in E Coli and answer.pdf
Read the short article Horizontal Gene Transfer in E Coli and answer.pdfRead the short article Horizontal Gene Transfer in E Coli and answer.pdf
Read the short article Horizontal Gene Transfer in E Coli and answer.pdfsauravmanwanicp
 
Please select the best answer and click submit. The owners of fou.pdf
Please select the best answer and click submit. The owners of fou.pdfPlease select the best answer and click submit. The owners of fou.pdf
Please select the best answer and click submit. The owners of fou.pdfsauravmanwanicp
 
One function of the spliceosome is to (Select all that apply.)A. .pdf
One function of the spliceosome is to (Select all that apply.)A. .pdfOne function of the spliceosome is to (Select all that apply.)A. .pdf
One function of the spliceosome is to (Select all that apply.)A. .pdfsauravmanwanicp
 
Match the following organelles with their key functions in the cell .pdf
Match the following organelles with their key functions in the cell  .pdfMatch the following organelles with their key functions in the cell  .pdf
Match the following organelles with their key functions in the cell .pdfsauravmanwanicp
 
please choose the correct answerIn the ABO blood-type phenotype, w.pdf
please choose the correct answerIn the ABO blood-type phenotype, w.pdfplease choose the correct answerIn the ABO blood-type phenotype, w.pdf
please choose the correct answerIn the ABO blood-type phenotype, w.pdfsauravmanwanicp
 
mo Press Submit until all questions are colored green and you have do.pdf
mo Press Submit until all questions are colored green and you have do.pdfmo Press Submit until all questions are colored green and you have do.pdf
mo Press Submit until all questions are colored green and you have do.pdfsauravmanwanicp
 
matching chose the stage of the light reactions which the statement.pdf
matching chose the stage of the light reactions which the statement.pdfmatching chose the stage of the light reactions which the statement.pdf
matching chose the stage of the light reactions which the statement.pdfsauravmanwanicp
 
Let E be the set of all even integers, and let O be the set of all o.pdf
Let E be the set of all even integers, and let O be the set of all o.pdfLet E be the set of all even integers, and let O be the set of all o.pdf
Let E be the set of all even integers, and let O be the set of all o.pdfsauravmanwanicp
 
JavaIm not sure how to implement this code can someone help me .pdf
JavaIm not sure how to implement this code can someone help me .pdfJavaIm not sure how to implement this code can someone help me .pdf
JavaIm not sure how to implement this code can someone help me .pdfsauravmanwanicp
 

More from sauravmanwanicp (20)

Are hydrophobic interactions weak or strongSolutionHydrophobi.pdf
Are hydrophobic interactions weak or strongSolutionHydrophobi.pdfAre hydrophobic interactions weak or strongSolutionHydrophobi.pdf
Are hydrophobic interactions weak or strongSolutionHydrophobi.pdf
 
What is another word for account based forecast What is anothe.pdf
What is another word for account based forecast What is anothe.pdfWhat is another word for account based forecast What is anothe.pdf
What is another word for account based forecast What is anothe.pdf
 
What function can be used to return multiple numeric data such as in.pdf
What function can be used to return multiple numeric data such as in.pdfWhat function can be used to return multiple numeric data such as in.pdf
What function can be used to return multiple numeric data such as in.pdf
 
Answer the following questions in reference to Drosophila melanogast.pdf
Answer the following questions in reference to Drosophila melanogast.pdfAnswer the following questions in reference to Drosophila melanogast.pdf
Answer the following questions in reference to Drosophila melanogast.pdf
 
There are five feasible solutions to a three-objective optimization p.pdf
There are five feasible solutions to a three-objective optimization p.pdfThere are five feasible solutions to a three-objective optimization p.pdf
There are five feasible solutions to a three-objective optimization p.pdf
 
An air-filled parallel-plate capacitor has capacitance Co. If two ide.pdf
An air-filled parallel-plate capacitor has capacitance Co. If two ide.pdfAn air-filled parallel-plate capacitor has capacitance Co. If two ide.pdf
An air-filled parallel-plate capacitor has capacitance Co. If two ide.pdf
 
The incandescent lightbulb was actually invented by Humphry Davy in 1.pdf
The incandescent lightbulb was actually invented by Humphry Davy in 1.pdfThe incandescent lightbulb was actually invented by Humphry Davy in 1.pdf
The incandescent lightbulb was actually invented by Humphry Davy in 1.pdf
 
The fossil record provides little information about ancient mosses. D.pdf
The fossil record provides little information about ancient mosses. D.pdfThe fossil record provides little information about ancient mosses. D.pdf
The fossil record provides little information about ancient mosses. D.pdf
 
The DNA sequence below is a shortened version of the sequence on the .pdf
The DNA sequence below is a shortened version of the sequence on the .pdfThe DNA sequence below is a shortened version of the sequence on the .pdf
The DNA sequence below is a shortened version of the sequence on the .pdf
 
the conducting tissue found in vascular plants that functions in tra.pdf
the conducting tissue found in vascular plants that functions in tra.pdfthe conducting tissue found in vascular plants that functions in tra.pdf
the conducting tissue found in vascular plants that functions in tra.pdf
 
Show that f(x)=57^x+1 and f^-1(x)=7x-75 are inverses Choose the ap.pdf
Show that f(x)=57^x+1 and f^-1(x)=7x-75 are inverses  Choose the ap.pdfShow that f(x)=57^x+1 and f^-1(x)=7x-75 are inverses  Choose the ap.pdf
Show that f(x)=57^x+1 and f^-1(x)=7x-75 are inverses Choose the ap.pdf
 
Read the short article Horizontal Gene Transfer in E Coli and answer.pdf
Read the short article Horizontal Gene Transfer in E Coli and answer.pdfRead the short article Horizontal Gene Transfer in E Coli and answer.pdf
Read the short article Horizontal Gene Transfer in E Coli and answer.pdf
 
Please select the best answer and click submit. The owners of fou.pdf
Please select the best answer and click submit. The owners of fou.pdfPlease select the best answer and click submit. The owners of fou.pdf
Please select the best answer and click submit. The owners of fou.pdf
 
One function of the spliceosome is to (Select all that apply.)A. .pdf
One function of the spliceosome is to (Select all that apply.)A. .pdfOne function of the spliceosome is to (Select all that apply.)A. .pdf
One function of the spliceosome is to (Select all that apply.)A. .pdf
 
Match the following organelles with their key functions in the cell .pdf
Match the following organelles with their key functions in the cell  .pdfMatch the following organelles with their key functions in the cell  .pdf
Match the following organelles with their key functions in the cell .pdf
 
please choose the correct answerIn the ABO blood-type phenotype, w.pdf
please choose the correct answerIn the ABO blood-type phenotype, w.pdfplease choose the correct answerIn the ABO blood-type phenotype, w.pdf
please choose the correct answerIn the ABO blood-type phenotype, w.pdf
 
mo Press Submit until all questions are colored green and you have do.pdf
mo Press Submit until all questions are colored green and you have do.pdfmo Press Submit until all questions are colored green and you have do.pdf
mo Press Submit until all questions are colored green and you have do.pdf
 
matching chose the stage of the light reactions which the statement.pdf
matching chose the stage of the light reactions which the statement.pdfmatching chose the stage of the light reactions which the statement.pdf
matching chose the stage of the light reactions which the statement.pdf
 
Let E be the set of all even integers, and let O be the set of all o.pdf
Let E be the set of all even integers, and let O be the set of all o.pdfLet E be the set of all even integers, and let O be the set of all o.pdf
Let E be the set of all even integers, and let O be the set of all o.pdf
 
JavaIm not sure how to implement this code can someone help me .pdf
JavaIm not sure how to implement this code can someone help me .pdfJavaIm not sure how to implement this code can someone help me .pdf
JavaIm not sure how to implement this code can someone help me .pdf
 

Recently uploaded

Scopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS PublicationsScopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS PublicationsISCOPE Publication
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjMohammed Sikander
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppCeline George
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxAdelaideRefugio
 
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...Nguyen Thanh Tu Collection
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...EduSkills OECD
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMELOISARIVERA8
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽中 央社
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....Ritu480198
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFVivekanand Anglo Vedic Academy
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi RajagopalEADTU
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17Celine George
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...Gary Wood
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...Nguyen Thanh Tu Collection
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppCeline George
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxneillewis46
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文中 央社
 

Recently uploaded (20)

Scopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS PublicationsScopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS Publications
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge App
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDF
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 

I need help implementing a Stack with this java programming assignme.pdf

  • 1. I need help implementing a Stack with this java programming assignment: Given a doubly linked list (code provided below) implement a Stack. You must use the doubly link list class and utilize it as an object in your implementation of the stack. A Stack is a Last in First Out (LiFO) structure which translates to the processes of outputting the last element which was inputted in the collection. The Stack is based on the process of putting things on top of one another and taking the item on top (think of a stack of papers). Operations to implement: push (E): Add an element to the start of the sequence pop: Remove an element from the start of the sequence Peek: Return the first element of the sequence without removing it atIndex(x): Return the element at the given index (x) Or throw an exception if it is out of bound (if you can control the user input then do that instead) Size: Return the size of the Stack isEmpty: Boolean, returns true if the Stack is empty Empty: Empty the Stack Code Part 1: public class DoublyLinkedList{ private DNode head; private DNode tail; private int size; public DoublyLinkedList(){ // construct an empty list this.tail = new DNode (null, null, this.head); this.head = new DNode (null, this.tail, null); this.size = 0; } public DoublyLinkedList(DNode next){ // constructs a list // out of a single node this.tail = new DNode (null, null, next); this.head = new DNode (null, next, null); next.changeNext(this.tail); next.changePrev(this.head); this.size = 1; } public DoublyLinkedList(Object [] objectArray){ // construct a list out of
  • 2. // an array this.tail = new DNode (null, null, this.head); this.head = new DNode (null, this.tail, null); DNode temp = this.head; for (Object e : objectArray) { //Anonomus function new DNode (e, temp.getNext(),temp).insertBetweenNodes(temp, temp.getNext()); temp = temp.getNext(); this.size += 1; } } public void addToFrontofList(Object toAdd){ // Appends the begining // of the list DNode temp = new DNode (toAdd, this.head.getNext(), this.head); this.head.getNext().changePrev(temp); this.head.changeNext(temp); this.size += 1; } public void addToendofList (Object toAdd) // appends the end of the list // with a node { DNode temp = new DNode (toAdd, this.tail, this.tail.getPrev()); this.tail.getPrev().changeNext(temp); this.tail.changePrev(temp); this.size += 1; } public void insertAfterNode(DNode current, Object input)// Inserts a new // a new node after // current node { current.insertAfterNode(input); this.size += 1; } public int getSize() // returns the size of the list
  • 3. { return this.size; } public String toString() { String result = ""; for (DNode temp = this.head.getNext(); temp.hasNext(); temp = temp.getNext()) { result += temp.getValue(); result += " -> "; } result += "End of list"; return result; } } and code part2: public class DNode{ private Object e; // place holder for the type of object the //collection is storing private DNode next; // reference to the next node in the sequence // of nodes making up the linked list. Remember // that even though Java passes Objects by value // an object consisting of reference will behave // the same as an object passed by reference unless // a deep copy was made. private DNode prev; public DNode(Object e){ // Constructor for implementing a single node this.e = e; // The value of the element to be assigned to // this particular instance of node this.next = null; // An empty reference since there is no node // to follow. this.prev = null; }
  • 4. public DNode(Object e, DNode next, DNode prev){ // Constructor for //implementing a node that //comes after another node this.e = e; // The value of the element to be assigned to // this particular instance of node this.next = next; // reference to the subsequent node to follow this.prev = prev; } public void changeNext(DNode next){ // Changes the link to the next node this.next = next; } public void changePrev(DNode prev){ // Changes the link to the next node this.prev = prev; } public DNode getNext(){ // Returns the node next in the sequence return this.next; } public Object getValue(){ // Returns the value a node is holding return this.e; } public Boolean hasNext(){ // Returns a boolean determining regarding // the status of subsequent nodes after // the current node return !(this.next.getValue() == null || this.next == null); } public Boolean hasprev(){ // Returns a boolean determining regarding // the status of subsequent nodes after // the current node return !(this.prev.getValue() == null || this.prev == null); } public void insertAfterNode( Object input){ this.changeNext(new DNode (input, this.getNext(), this)); } public void insertAfterNode(DNode input){ this.changeNext(input);
  • 5. input.changeNext(this.next); input.changePrev(this); } public void insertbeforeNode(DNode input){ this.changeNext(input); } public void insertBetweenNodes(DNode before, DNode after){ this.changeNext(after); this.changePrev(before); before.changeNext(this); after.changePrev(this); } public DNode getPrev(){ return this.prev; } } Solution Following is the program that contain functions like push,pop,isEmpty etc. call this function with proper data. import java.util.NoSuchElementException; public class DoublyLinkedListImpl { private Node head; private Node tail; private int size; public DoublyLinkedListImpl() { size = 0; } // this class keeps track all element info private class Node { E element; Node next;
  • 6. Node prev; public Node(E element, Node next, Node prev) { this.element = element; this.next = next; this.prev = prev; } } // returns the size of the linked list public int size() { return size; } // return whether the list is empty or not public boolean isEmpty() { return size == 0; } // adds element at the starting of the linked list public void push(E element) { Node tmp = new Node(element, head, null); if(head != null ) {head.prev = tmp;} head = tmp; if(tail == null) { tail = tmp;} size++; System.out.println("adding: "+element); } // this method removes element from the start of the linked list public E pop() { if (size == 0) throw new NoSuchElementException(); Node tmp = head; head = head.next; head.prev = null; size--; System.out.println("deleted: "+tmp.element); return tmp.element; } public E Peek() {
  • 7. if (isEmpty()) throw new NoSuchElementException("Stack underflow"); return head.element; } public void remove() { head = null; } // Takes index as argument and return data at index public E GetNth(int index) { Node current = head; int count = 0; while (current != null) { if (count == index) return current.element; count++; current = current.next; } assert(false); return current.element; } public static void main(String a[]){ // Testing data to test the functions DoublyLinkedListImpl dll = new DoublyLinkedListImpl(); dll.remove(); dll.push(10); dll.push(34); dll.size(); dll.Peek(); dll.GetNth(2); dll.pop(); } }