DSA : Stack
application of stack
- evaluating balanced symbols -undo operation in text editors -parsing arithmetic expression
isFullStack code segment ex
//if stackTop is equal to maxStackSize template <class Type> bool stackType<Type>:: isFullStack() const { return (stackTop == maxStackSize); }//end isFullStack
isEmptyStack code segment ex
//if value return 0 = stack is empty template <class Type> bool stackType<Type>:: isEmptyStack() const { return (stackTop == 0); }//end isEmptyStack
isFullStack
If the stack is full, return the value true
initializeStack
Initializes stack to an empty state
The ____ operation is used to add an element onto the stack. a.push b.pop c.add d. insert
a. push
The addition and deletion of elements occurs only at one end of a stack, called the ____ of the stack. a.head b.top c.bottom d.tail
b. top
We can perform the operation ____ to retrieve the top element of the stack. a.head b.top c.first d.peek
b. top
The ____ operation is used to remove the top element from the stack. a.push b.pop c.add d. insert
b.pop
A stack is a ____ data structure. a.FIFO b.FILO c.LIFO d.LILO
c. LIFO
The ____ operation checks whether the stack is empty. a. isFullStack b. peek c. isEmptyStack d. pop
c. isEmptyStack
Top code segment ex
template <class Type> Type stackType<Type>:: top() const { assert(stackTop != 0); //if stack is empty, terminate the program return list[stackTop -1]; //return the element of the stack //indicated by stackTop -1 }//end top
Pop (remove) code segment ex
template <class Type> void stackType<Type>:: pop() { if(!isEmptyStack()) { stackTop--; //decrement stackTop else cout << "cannot removeto a empty stack" << endl; }//end pop
Push (add) code segment ex
template <class Type> void stackType<Type>:: push(const Type& newItem) { if(!isFullStack()) { list[stackTop] = newItem; //add new item at the top stackTop++; //increment stackTop } else cout << "cannot add to a full stack" << endl; }//end push
Initialize Stack code segment ex
template <class Type> void stackType<Type>::initializeStack() { stackTop = 0; }//end initializeStack