DSA - STACK & QUEUES Flashcards
Define Stack:
Is a Last-In-First-Out abstract data type
Define Abstract Data Type
Implements data Structures using other data types
What does the command Push do?
Puts value x onto the top of the stack.
Theoretically can push as many values onto a stack, however we will get an exception when memory memory runs out
What does the command Pop do?
Removes (sometimes returns) value on top of the stack.
What does the command isEmpty() do?
Says whether stack is empty:
- length of stack is 0
- Top pointer is 0
Rises an EmptyStackException ERROR
What is the Top END of Stack?
First Element to be executed / completed and where new values are pushed in
What is the Bottom End of Stack?
Last Element to be removed/completed
What does push(x) followed by is_empty give?
FALSE
What does push(x) followed by push() gives?
x
What does the command top(stack) do?
Returns the value at top of stack without editing the stack
What does the command push(element, Stack)?
Pushes an element on top of chosen stack
What does the command pop (stack) do?
Removes & Returns top element of the stack
What does EmptyStack do?
returns an empty Stack
Why does top at the beginning the ideal scenario for a Stack as a Linked List?
- Inserting & Deleting from the beginning of a linked list is constant:
- push = insert_beg
- pop = delete_beg
-is_empty = is_empty
Every operations on Stacks takes constant time
Why does having top at end is not ideal for a Stack as a Linked List?
- delete_end has a linear time due to having to traverse through the entire list to find the end and delete the node.
- insert_end will be fast
How do you Implement Stack as an Array?
stack = new int[MAXSTACK];
stack_size = 0;
Why is MAXSTACK useful than using allocate_memory?
MAXSTACK is useful because it allocates memory as pre-requisite before implementing a stack.
Allocate_memory automatically allocates memory but takes time.
Where is the bottom in an array Stack?
position 0
Where is the top in an array Stack?
position stack_size-1