Sei sulla pagina 1di 11

CSC251 Study Guide

Midterm Melodrama
Index
Queue Properties
overview
dention
diagram
conceptual implementation
operations
Stack Properties
overview
dention
diagram
conceptual implementation
operations
Stack Code & Algorithms
uses arrays for implementation
is stack empty
is stack full
add to stack
delete from stack
print in order
nd an element in stack
Comparing Stacks & Queues
overview
advantages
disadvantages
examples of how they can be used
Queue Algorithms
uses arrays for implementation
shift queue
circular queues
Basic Dentions
Queue Properties
overview
example of a collection SDT
operations: enqueue (add), dequeue (delete)
dention
data structure of ordered items that can be inserted only at end
(rear) and removed at other end (front)
diagram
conceptual implementation
First In, First Out (FIFO)
add to rear and remove from front
operations
enqueue - adds item to queue
dequeue - removes item from queue
first - looks at top item in stack - so front
size - returns # of elements in queue by examining top
isEmpty - tests for emptiness by examining top
Stack Properties
overview
example of a collection SDT
since stacks are lists, any list implementation will do
dention
data structure of ordered items such that items can be inserted
and removed only at 1 end (called the top )
diagram
conceptual implementation
First In, First Out (FIFO)
add to rear and remove from front
operations
push - adds item to stack
pop - deletes item from stack
peek - looks at top item in stack
size - returns # of elements in stack by examining top
isEmpty - tests for emptiness in stack by examining top
Stack Code & Algorithms
isEmpty
int top = -1; //or 0
public void isEmpty () {
return (top == -1);
}
isFull
public void isFull() {
return (top == size-1);
}
add to stack
public void add (int element) {
if (isFull() == true)
System.out.println("Stack full, cannot add element.");
else
stack[++top] = element;
}
1. check if stack is not full
2. set top position of array to element being added to stack
3. increment values of top and count (if partially lled array)
delete from stack
public int delete () {
if (isEmpty() == true)
System.out.println("Stack empty, cannot delete
element.");
else
return stack[top--];
}
1. check if stack is not empty
2. decrement top
3. set return value to top position of array
print in order
public void printOrder() {
while(!isEmpty()) {
int value = remove();
System.out.print(value + " ");
}
}
nd an element in stack
public void add (int element) {
System.out.print("Please enter element to find: ");
element = keyboard.nextInt();
for (int i = 0; i < size; i++) {
if (stack[i] == element)
return true;
}
return false;
}
Comparing Stacks & Queues
overview
stacks and queues are similar data structures
theyre different only by how an item is removed rst
both can be implemented as arrays or linked lists
advantages
implementing a stack as an array is easy, but implementing a
queue as an array is more difcult because you have to
dequeue from front and enqueue at end
disadvantages
main limitations of arrays is that they cannot be extended or
shrunk so you want to use an array when you know your max
upper limit
examples of stacks
CD storage case
cafeteria tray
batteries in a ashlight
cars in a single car garage
Towers of Hanoi
examples of queues
waiting line for a roller coaster
hamburger processing line at Mickey Ds
vehicles on a toll bridge
luggage checking machine
phone answering system for most big tech companies
Queue Algorithms
shift queues
enqueue operation
save value at rst index to item
size++
if array lls up, expensive. all n elements have to be
copied to a new, larger array
dequeue operation
1. save value at rst index
2. shift all elements left
3. decrement size
circular queues
advance the queue indexes front (to delete an item) and back
(to insert an item) by moving them both clockwise around array
Basic Denitions
abstractions
ignores or hides certain details at certain times, e.g. collection is
abstraction where details of implementation are hidden
abstract in abstract data type
means there are many different ways of implementing data type
data structure
memory structure with associated representation mapping;
collection of programming constructions used to implement a
collection
TO NOTE
Although stack and queues have been implemented using an array
- these ADTs are not accessed as an array.
No actual code for a queue on this test; but you should be able to
write an algorithm to demonstrate the concept of a queue.
Tips or Tricks?
Contact me @fvcproductions

Potrebbero piacerti anche