Stacks and Queues Flashcards
What is a queue?
A queue is a group of items all of the same type where items are added to the back of the queue and removed from the front.
What is the principle of a Queue
FIFO
first-in first-out
What are the methods of a Queue and what do they do?
add(x) = adds item x to the queue remove() = removes and returns front item peek() = returns the front item with no remove size() = returns the # of the items in the queue isEmpty() = checks to see if the queue is empty
What are the methods of a Priority Queue and what do they do?
add(x) = adds item x to the pQueue remove() = removes and returns min priority item peek() = returns the min item with no remove size() = returns the # of items in the pQueue isEmpty() = checks to see if the pQueue is empty
What is the difference between the pQueue and queue?
pQueue orders them based on the ASCII table while queue does not order at all.
What is a Stack
A stack is a group of items all of the same type where items are added to the top of the stack and removed from the top.
What is the principle of Stacks?
LIFO
last-in first-out
What are the methods of a Stack and what do they do?
push(x) = adds item x to the stack add(x) = adds item x to the stack pop() = removes and returns an item peek() = returns the top item with no remove size() = returns the # of items in the stack isEmpty() = checks to see if the stack is empty
What are the packages needed to call the data structures?
Queue = import java.util.Queue;
Stack = import java.util.Stack;
Both = import java.util.*;
How does the principles work in data Structures.
As an example, the last-in first-in principle means that “the last element added is the first to be taken out”.