Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
expand_more
expand_more
format_list_bulleted
Question
The ADT stack lets you peek at its top entry without removing it. For some applications of stacks, you
also need to peek at the entry beneath the top entry without removing it. We will call such an operation
peekNxt. If the stack has more than one entry, peekNxt returns the second entry from the top without altering the stack. If the stack has fewer than two entries, peekNxt throws an exception. Write a linked
implementation of a stack class call LinkedStack.java that includes a method peekNxt.
Implement p
import java.util.EmptyStackException;
import java.util.NoSuchElementException;
public final class LinkedStack<T> implements StackInterface<T>
{
private Node topNode;
public YourFirst_YourLast_LinkedStack()
{
topNode = null;
}
public void push(T newEntry)
{
Node newNode = new Node(newEntry, topNode);
topNode = newNode;
} // end push
public T peek()
{
if (isEmpty())
throw new EmptyStackException();
else
return topNode.getData();
} // end peek
public T peekNxt() // Code here
{
import java.util.EmptyStackException;
import java.util.NoSuchElementException;
public final class LinkedStack<T> implements StackInterface<T>
{
private Node topNode;
public YourFirst_YourLast_LinkedStack()
{
topNode = null;
}
public void push(T newEntry)
{
Node newNode = new Node(newEntry, topNode);
topNode = newNode;
} // end push
public T peek()
{
if (isEmpty())
throw new EmptyStackException();
else
return topNode.getData();
} // end peek
public T peekNxt() // Code here
{
}
public T pop()
{
T top = peek();
topNode = topNode.getNextNode();
return top;
}
public boolean isEmpty()
{
return topNode == null;
}
public void clear()
{
topNode = null;
}
private class Node
{
private T data;
private Node next;
private Node(T dataPortion)
{
this(dataPortion, null);
}
private Node(T dataPortion, Node linkPortion)
{
data = dataPortion;
next = linkPortion;
}
private T getData()
{
return data;
} // end getData
private void setData(T newData)
{
data = newData;
}
private Node getNextNode()
{
return next;
}
private void setNextNode(Node nextNode)
{
next = nextNode;
}
}
}
--------------------------------------
public interface StackInterface<T>
{
/** Adds a new entry to the top of this stack.
@param newEntry An object to be added to the stack. */
public void push(T newEntry);
/** Removes and returns this stack's top entry.
@return The object at the top of the stack.
@throws EmptyStackException if the stack is empty before the operation. */
public T pop();
/** Retrieves this stack's top entry.
@return The object at the top of the stack.
@throws EmptyStackException if the stack is empty. */
public T peek();
/** Retrieves the entry just below this stack's top entry.
@return The object just below the top of the stack.
@throws EmptyStackException if the stack is empty.
@throws NoSuchElementException if the stack contains only one entry.
*/
public T peekNxt();
/** Detects whether this stack is empty.
@return True if the stack is empty. */
public boolean isEmpty();
/** Removes all entries from this stack. */
public void clear();
}
this(dataPortion, null);
}
private Node(T dataPortion, Node linkPortion)
{
data = dataPortion;
next = linkPortion;
}
private T getData()
{
return data;
} // end getData
private void setData(T newData)
{
data = newData;
}
private Node getNextNode()
{
return next;
}
private void setNextNode(Node nextNode)
{
next = nextNode;
}
}
}
--------------------------------------
public interface StackInterface<T>
{
/** Adds a new entry to the top of this stack.
@param newEntry An object to be added to the stack. */
public void push(T newEntry);
/** Removes and returns this stack's top entry.
@return The object at the top of the stack.
@throws EmptyStackException if the stack is empty before the operation. */
public T pop();
/** Retrieves this stack's top entry.
@return The object at the top of the stack.
@throws EmptyStackException if the stack is empty. */
public T peek();
/** Retrieves the entry just below this stack's top entry.
@return The object just below the top of the stack.
@throws EmptyStackException if the stack is empty.
@throws NoSuchElementException if the stack contains only one entry.
*/
public T peekNxt();
/** Detects whether this stack is empty.
@return True if the stack is empty. */
public boolean isEmpty();
/** Removes all entries from this stack. */
public void clear();
}
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution
Trending nowThis is a popular solution!
Step by stepSolved in 2 steps
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- Create a programme that sorts a stack so that the smallest elements appear on top. You can use a second temporary stack, but you can't transfer the components into another data structure (such an array). The following operations are supported by the stack: push, pop, peek, and is Empty.arrow_forwardI have already done number 1. I just need help with number 2. Implement Stack<E> with an internal LineNodePlus<E>. You have been given the code for the StackADT<E> interface and all of the List files needed. You must come up with a new Stack.java that implements the StackADT interface and uses a LineNodesPlus internally to store the stack. Construct test cases for the Stack (StackTest.java) to ensure it works correctly. StackADT.java public interface StackADT<E>{ // Stack class ADT /** * Clears the stack. */ public void clear(); /** * Pushes argument it onto the stack. Argument is stored in the top of the * stack. Returns false if the stack is out of space. * * @param it value to be pushed onto the stack * @return true if the value is stored */ public boolean push(E it); /** * Pop and return the value at the top of the stack. * * @return value stored at the top of the stack */ public E…arrow_forwardPP 13.3 Create a new version of the LinkedStack class that makes use of a dummy record at the head of the list. PP 13.4 Create a simple graphical application that will allow a user to perform push, pop, and peek operations on a stack, and display the resulting stack (using toString) in a text area.arrow_forward
- PP 13.3 Make a new version of the LinkedStack class that includes a fake record at the beginning of the list. PP 13.4 Create a basic graphical programme that allows a user to execute push, pop, and peek operations on a stack and displays the resulting stack in a text area (using toString).arrow_forwardJAVA please Given main() in the ShoppingList class, define an insertAtEnd() method in the ItemNode class that adds an element to the end of a linked list. DO NOT print the dummy head node. Ex. if the input is: 4 Kale Lettuce Carrots Peanuts where 4 is the number of items to be inserted; Kale, Lettuce, Carrots, Peanuts are the names of the items to be added at the end of the list. The output is: Kale Lettuce Carrots Peanuts Code provided in the assignment ItemNode.java:arrow_forwardExplore the stack's capabilities when allowed to be itself.arrow_forward
- The ADT stack lets you peek at its top entry without removing it. For some applications of stacks, you also need to peek at the entry beneath the top entry without removing it. We will call such an operation peek2. If the stack has more than one entry, peek2 returns the second entry from the top without altering the stack. If the stack has fewer than two entries, peek2 throws an exception. Write a linked implementation of a stack class call First_Last_LinkStack.java that includes a method peek2arrow_forwardstacks and queues. program must be able to handle all test cases without causing an exception Note that this problem does not require recursion to solve (though you can use that if you wish).arrow_forwardProject Overview: This project is for testing the use and understanding of stacks. In this assignment, you will be writing a program that reads in a stream of text and tests for mismatched delimiters. First, you will create a stack class that stores, in each node, a character (char), a line number (int) and a character count (int). This can either be based on a dynamic stack or a static stack from the book, modified according to these requirements. I suggest using a stack of structs that have the char, line number and character count as members, but you can do this separately if you wish.Second, your program should start reading in lines of text from the user. This reading in of lines of text (using getline) should only end when it receives the line “DONE”.While the text is being read, you will use this stack to test for matching “blocks”. That is, the text coming in will have the delimiters to bracket information into blocks, the braces {}, parentheses (), and brackets [ ]. A string…arrow_forward
- What is the operation that adds items to a stack? Get Set Pop Pusharrow_forwardOCaml Code: The goal of this project is to understand and build an interpreter for a small, OCaml-like, stackbased bytecode language. Make sure that the code compiles correctly and provide the code with the screenshot of the output. Make sure to have the following methods below: -Push integers, strings, and names on the stack -Push booleans -Pushing an error literal or unit literal will push :error: or :unit:onto the stack, respectively -Command pop removes the top value from the stack -The command add refers to integer addition. Since this is a binary operator, it consumes the toptwo values in the stack, calculates the sum and pushes the result back to the stack - Command sub refers to integer subtraction -Command mul refers to integer multiplication -Command div refers to integer division -Command rem refers to the remainder of integer division -Command neg is to calculate the negation of an integer -Command swap interchanges the top two elements in the stack, meaning that the…arrow_forwardWrite a method to reverse the content of a stack. Inside the method, you may create exactly one temporary container -- either a stack or a queue. It is alright to create variables of primitive data types (such as, an integer), if required. You must use the following header. public void reverse(Stack s){arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education