Question

In: Computer Science

I've already worked on isEmpty, so please check that and solve the other parts to the...

I've already worked on isEmpty, so please check that and solve the other parts to the question as well. Will rate as soon an I see that it is answered.

In this section, you will implement a generic Stack class implemented using linked list. Assume the linked list node class is already defined as below:
public class LLNode<T> {
public LLNode<T> link;

public T info;

public LLNode(T in) { info = in; link = null; }

}

Note that both class variables are public so any outside class can access them directly. Also assume that class StackUnderflowException has been defined that inherits Java’s Exception class. Your task is to implement four methods in the generic class LinkedListStack<T>.

public class LinkedListStack<T> {

private LLNode<T> head; // head of linked list, also stack top pointer

public LinkedListStack() { head = null; } // constructor

public boolean isEmpty() { // [1 pts]

// TODO: return true if stack is empty, false otherwise

// NO MORE THAN 1 LINE OF CODE!

if(head.link == null) return true;

}

public void push(T element) { // [2 pts]

// TODO: push an element to the stack

// NO MORE THAN 3 LINES of CODE!

}

public T peek() throws StackUnderflowException { // [2 pts]

// TODO: return the top element of the stack (but do NOT
// remove it). NO MORE THAN 4 LINES of CODE!

}

public T pop() throws StackUnderflowException { // [3 pts]

// TODO: remove and return the top element of the stack

// It throws StackUnderflowException if stack is empty

// NO MORE THAN 6 LINES of CODE!

}

Solutions

Expert Solution

public class LinkedListStack<T> {

private LLNode<T> head; // head of linked list, also stack top pointer

public LinkedListStack() { head = null; } // constructor

public boolean isEmpty() { // [1 pts]
   return (head==null); // RETURNS TRUE IF LIST EMPTY
}

public void push(T element) { // [2 pts]
   LLNode<T> p = new LLNode<T>(element);// create node
   if(isEmpty()){ // check stack is empty
       head = p; // assign head is first node
   }else{
       p.link = head;
       head = p;
   }

}

public T peek() throws StackUnderflowException { // [2 pts]
   if (head == null) {
throw new StackUnderFlowException("The stack is empty.");
}
return head.info;

}

public T pop() throws StackUnderflowException { // [3 pts]

   if (head == null) {
throw new StackUnderflowException("The stack is empty.");
}

T output = head.info;
head = head.link;

return output;
}

/* PLEASE UPVOTE */


Related Solutions

I've already worked out the vertical and horizontal components so far for this question. Vertical component=16.34...
I've already worked out the vertical and horizontal components so far for this question. Vertical component=16.34 and horizontal component=25.16 but I got no idea what to do from here. A polo ball is hit from the ground at an angle of 33 degrees upwards from the horizontal. If it has a release velocity of 30 m/s and lands on the ground. Based on the initial release parameters, what will the polo ball's vertical and horizontal velocity components be at the...
Please answer this question and do steps 1-7. I've already posted this question and they did...
Please answer this question and do steps 1-7. I've already posted this question and they did not complete the answer. PB2-3    Recording Transactions (in a Journal and T-Accounts); Preparing and Interpreting the Balance Sheet (LO 2-2, 2-3, 2-4, 2-5 and a little Chapter 1) Starbuck$ is a coffee company – a big coffee company. During a 10-year period, the number of Starbucks locations grew from 165 to over 8,500 stores in 50 countries. The following is adapted from Starbucks’s annual...
Please finish parts E and F from from this problem (parts A-D are already finished and...
Please finish parts E and F from from this problem (parts A-D are already finished and the data is there): https://www.chegg.com/homework-help/questions-and-answers/transactions-rock-medical-company-service-company-organized-corporation-month-march-record-q26222839 (e) Prepare closing entries and enter the transactions in the T-accounts (from part (b); journal entries can be handwritten or typed in Word/Excel; put on a separate page(s). (f) Prepare a post-closing trial balance in EXCEL; use formulas to total the debits and credit
Please check it out within 30 minutes. Please provide explanation or working so I can check...
Please check it out within 30 minutes. Please provide explanation or working so I can check with my answer. Question: In a study of the system, Cl2(g) + Br2(g) 2BrCl(g), several different reaction mixtures were prepared, placed in a 15.00 × 10–3 m3 container, and allowed to reach equilibrium at 350 K. Despite having different starting compositions, three of the four mixtures had an identical composition at equilibrium. Which one of the systems has a different equilibrium composition than the...
Please Double Check answers I've recived 3 wrong answers on three diffrent questions today thank you...
Please Double Check answers I've recived 3 wrong answers on three diffrent questions today thank you A leading magazine (like Barron's) reported at one time that the average number of weeks an individual is unemployed is 17 weeks. Assume that for the population of all unemployed individuals the population mean length of unemployment is 17 weeks and that the population standard deviation is 2 weeks. Suppose you would like to select a random sample of 98 unemployed individuals for a...
Please Double Check answers I've recived 3 wrong answers on three diffrent questions today thank you...
Please Double Check answers I've recived 3 wrong answers on three diffrent questions today thank you CNNBC recently reported that the mean annual cost of auto insurance is 1006 dollars. Assume the standard deviation is 245 dollars. You take a simple random sample of 73 auto insurance policies. Find the probability that a single randomly selected value is less than 973 dollars. P(X < 973) = Find the probability that a sample of size n = 73 is randomly selected...
Please explain clear and different to the answers that are already in other websites. Thank you...
Please explain clear and different to the answers that are already in other websites. Thank you 1. An interesting mutation in lacI results in repressors with 110-fold increased binding to both operator and nonoperator DNA. These repressors display a “reverse” induction curve, allowing β-galactosidase synthesis in the absence of an inducer (IPTG) but partly repressing β-galactosidase expression in the presence of IPTG. How can you explain this? (Note that, when IPTG binds a re- pressor, it does not completely destroy...
I am doing a project on cancerous cell types and I've already identified 20 kinds, the...
I am doing a project on cancerous cell types and I've already identified 20 kinds, the tissues they effect, and the exact disease they cause. There are multiple other steps to the project that I am struggling with. This involves finding biochemicals (such as lipids, proteins, etc) associated with specific cancer cell types, the metabolic pathways they effect, membrane proteins (such as growth factors) and membrane protein transporters associated with the cancer cell type. Could someone give me an idea...
I've did most of the work i just need someone to check if my work is...
I've did most of the work i just need someone to check if my work is correct Maryam and Hanadi are planning to open a shop to sell female shoes in Abu Dhabi. The venue of the shop is decided to be a rented premises in Marina Mall. They wish to start their operations from 01 July, 2020. The following information is available about the company’s operations. i. The company predicts a sale price of AED 350 per unit. Sales...
PLEASE CHECK MY GRAMMAR: In the Battle of The Buldge, The 101st Airborne and several other...
PLEASE CHECK MY GRAMMAR: In the Battle of The Buldge, The 101st Airborne and several other untits, took on a much larger group of Ferman soliders with heavy artillery. They inflicited more damage to the enemy but did take heavy losses. They held the area while defenses could be arranged around Bastogne. They fought and held the area from December 16 to December in terrible conditions. They did not have the needed medicdal supplies or ammunitions. They were survinging the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT