Describe the characteristics and applications of a stack.
Construct algorithms using the access methods of a stack.
A Stack is a data structure that stores data elements in such a way that the value stored last will be retrieved first.
The Stack data structure is also called LIFO - Last-In-First-Out.
A Stack structure has a top, similar to a pile or stack of plates in the cafeteria. The last one you put on a pile or stack is the top.
A Stack has two standard operations that can be performed on the items in a stack, and only the top item of a stack can be processed.
pop() - is a stack function/operation/method to remove the top item of the stack.
push() - is a stack function/operation/method to add item on top of the stack.
other Stack operation include (isEmpty(), size(), makeEmpty())
running recursive processes,
return memory addresses
A Stack can be implemented either through an array or a linked list. What identifies the data structure as a stack in either case is not the implementation but the interface, which means that the user is only allowed to pop or push items onto the array or linked list, with few other helper operations.[reference link]
To reverse a word. You push a given word to stack - letter by letter - and then pop letters from the stack.
An "undo" mechanism in text editors; this operation is accomplished by keeping all text changes in a stack.
Undo/Redo stacks in Excel or Word.
Language processing :
space for parameters and local variables is created internally using a stack.
compiler's syntax check for matching braces is implemented by using stack.
A stack of plates/books in a cupboard.
A garage that is only one car wide. To remove the first car in we have to take out all the other cars in after it.
Wearing/Removing Bangles.
Back/Forward stacks on browsers.
Support for recursion
Activation records of method calls.
public class StackInArray {
private final int[] data;
private int top;
/**
* constructor
* pre: none
* post: An empty stack that can hold up to maxItems has been created.
* @param maxItems
*/
public StackInArray(int maxItems) {
data = new int[maxItems];
top = -1; //no items in the array
}
/**
* @return the top item without removing it from the stack.
* pre: StackInArray contains at least one item.
* post: The top item has been returned while leaving it on the stack.
*/
public int top() {
return (data[top]);
}
/**
* @return Removes the top item from the stack and returns it.
* pre: StackInArray contains at least one item.
* post: The top item of the stack has been removed and returned.
*/
public int pop() {
top = top - 1;
return (data[top + 1]);
}
/**
* Adds an item to the top of the stack if there is room.
* pre: none
* post: A new item has been added to the top of the stack.
* @param num
*/
public void push(int num) {
if (top < data.length - 1) {
top = top + 1;
data[top] = num;
}
}
/**
* Determines if there are items on the stack.
* pre: none
* post: true returned if there are items on the stack, false returned otherwise.
* @return true if there are items, false otherwise
*/
public boolean isEmpty() {
if (top == -1) {
return true;
} else {
return false;
}
}
/**
* Returns the number of items in the stack.
* pre: none
* post: The number of items in the stack is returned.
* @return number of items in stack
*/
public int size() {
if (isEmpty()) {
return 0;
} else {
return top + 1;
}
}
/**
* Empties the stack.
* pre: none
* post: There are no items in the stack.
*/
public void makeEmpty() {
top = -1;
}
}
public class StackInArrayList<E> {
private ArrayList<E> data;
public StackInArrayList() {
data = new ArrayList<E>();
}
public void push(E item) {
data.add(item);
}
public E pop() {
if (!isEmpty()) {
return data.remove(size() - 1);
} else {
throw new EmptyStackException();
}
}
public boolean isEmpty() {
return (data.size() == 0);
}
public E peek() {
if (!isEmpty()) {
return data.get(size() - 1);
} else {
throw new EmptyStackException();
}
}
public int size() {
return data.size();
}
@Override
public String toString() {
return "MyStack [data=" + data.toString() + "]";
}
}
https://www.quora.com/What-are-some-real-world-applications-of-a-stack-data-structure