0% found this document useful (0 votes)
2 views3 pages

Class LinkedStack

The document provides an implementation of a stack using a linked list in Java, defining a class 'LinkedStack' that includes methods for pushing, popping, and peeking at elements. It features an inner class 'Node' for creating nodes in the linked list, and includes a copy constructor for duplicating stacks. The stack operations throw exceptions when attempting to access elements from an empty stack.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Class LinkedStack

The document provides an implementation of a stack using a linked list in Java, defining a class 'LinkedStack' that includes methods for pushing, popping, and peeking at elements. It features an inner class 'Node' for creating nodes in the linked list, and includes a copy constructor for duplicating stacks. The stack operations throw exceptions when attempting to access elements from an empty stack.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import [Link].

NoSuchElementException;
/** * Implementation of the interface StackInt using a linked [Link]
top element of the stack is in the first (front) node of the linked list.
* @param <E>
*/
public class LinkedStack<E> implements StackInt<E>
{

/** Node: inner class to create nodes for linked list based stack. */
private static class Node<E>
{
// Data Fields
private E data;
private Node<E> next; // The reference to the next node.

// Constructors
/**
* Creates a new node with a null next field.
* @param dataItem The data to be stored in the node
*/
private Node(E dataItem)
{
data = dataItem;
next = null;
}

/**
* Creates a new node that references another node.
* @param dataItem The data to be stored in the node
* @param nodeRef The node referenced by new node
*/
private Node(E dataItem, Node<E> nodeRef)
{
data = dataItem;
next = nodeRef;
}
} //End of class Node

// Data Field: The reference to the first node of the stack.


private Node<E> topOfStack;

// Constructor
public LinkedStack()
{
topOfStack = null; // Initially the stack is empty
}
// copy constructor
public LinkedStack(LinkedStack<E> other)
{
if([Link]())
1
topOfStack=null;
else
{
Node<E> newNode = new Node<> ([Link]);
topOfStack = newNode;
Node<E> ptr= [Link];
Node<E> ptr1 = topOfStack;
while (ptr != null)
{
newNode = new Node<>([Link]);
[Link]=newNode;
ptr = [Link];
ptr1=[Link];
}
}
}

/**
* Insert a new item on top of the stack.
* @post The new item is the top item on the stack.
* @param obj The item to be inserted
* @return The item that was inserted
*/

@Override
public E push(E obj)
{
topOfStack = new Node<>(obj, topOfStack);
return obj;
}

/**
* Remove and return the top item on the stack.
* @pre The stack is not empty.
* @post The top item on the stack has been removed and
* the stack is one item smaller.
* @return The top item on the stack
* @throws NoSuchElementException, if the stack is empty
*/

@Override
public E pop()
{
if (isEmpty()) {
throw new NoSuchElementException();
}
else {
E result = [Link];
topOfStack = [Link];
return result;
}
2
}

/**
* Return the top item on the stack.
* @pre The stack is not empty.
* @post The stack remains unchanged.
* @return The top item on the stack
* @throws NoSuchElementException if the stack is empty
*/
@Override
public E peek()
{
if (isEmpty()) {
throw new NoSuchElementException();
}
else {
return [Link];
}
}

/**
* See whether the stack is empty.
* @return true if the stack is empty
*/

@Override
public boolean isEmpty()
{
return (topOfStack == null);
}

} // End of class LinkedStack

You might also like