Stack Queue Search Tejada
Stack Queue Search Tejada
CS102
C++ Stacks & Queues
Prof Tejada
1
Data Structures - CSCI 102
Stacks/Queues
Templated lists are good for storing generic sequences of
items, but they can be specialized to form other useful
structures
3
Copyright William C. Cheng
Data Structures - CSCI 102
Stacks
Stack: a list of items where insertion and removal only
occurs at one end of the list
Examples
A spring-loaded plate dispenser at a buffet
A stack of boxes where you have to move the top one
to get to ones farther down
A PEZ dispenser
4
Copyright William C. Cheng
Data Structures - CSCI 102
5
Copyright William C. Cheng
Data Structures - CSCI 102
The Stack ADT
Push Pop
6
Copyright William C. Cheng
Data Structures - CSCI 102
Stack Declaration
What does the interface for a Stack look like?
7
Copyright William C. Cheng
Data Structures - CSCI 102
Stack Declaration
How would you build a Linked List-based Stack?
Stack Declaration
How would you build a Linked List-based Stack?
You could also back the Stack with a vector
template <typename T>
class Stack
{
private:
T* date; //could also be vector<T>
int length;
public:
Stack();
~Stack();
int size() const;
void push(const T& value);
void pop();
T& top();
bool empty() const;
};
9
Copyright William C. Cheng
Data Structures - CSCI 102
Stack Examples
Reversing the letters in a string
int main() {
Stack<char> s;
string word;
cout << "Enter a word: ";
getline(cin,word);
12
Copyright William C. Cheng
Data Structures - CSCI 102
13
Copyright William C. Cheng
Data Structures - CSCI 102
The Call Stack
void C()
{ stack
int p=0; C()
cout << p; p 0
}
calls
B() x ?
void B(int x) n 0
{
int n=0; calls
x ?
C();
A()
y ?
}
m 0
calls
15
Copyright William C. Cheng
Data Structures - CSCI 102
17
Copyright William C. Cheng
Data Structures - CSCI 102
Queues
Queue: a list of items where insertion only occurs at the
back of the list and removal only occurs at the front of the
list
Like waiting in line for a cashier at a store
18
Copyright William C. Cheng
Data Structures - CSCI 102
19
Copyright William C. Cheng
Data Structures - CSCI 102
Push
(Enqueue)
Pop
(Dequeue)
20
Copyright William C. Cheng
Data Structures - CSCI 102
Queue Declaration
What does the interface for a Queue look like?
21
Copyright William C. Cheng
Data Structures - CSCI 102
Queue Declaration
What does a Linked List-based Queue look like?
template <typename T>
class Queue
{
private:
Node<T> *head, *tail;
int length;
public:
Queue();
~Queue();
int size() const;
void push(const T& value); //enqueue
void pop(); //dequeue
T& front();
T& back();
bool empty() const;
}; 22
Copyright William C. Cheng
Data Structures - CSCI 102
Queue Declaration
What does an array-based Queue look like?
You could also back the Queue with a vector
template <typename T>
class Queue
{
private:
T* data; //could also be vector<T>
int length;
public:
Queue();
~Queue();
int size() const;
void push(const T& value); //enqueue
void pop(); //dequeue
T& front();
T& back();
bool empty() const;
}; 23
Copyright William C. Cheng
Data Structures - CSCI 102
Queue Examples
How does a printer work?
Multiple print jobs are sent in
Click "Print" on the computer is much faster than
actually printing (build a backlog)
Each job is processed in the order its received (FIFO)
Why wouldnt you use a "Print Stack" instead of a
"Print Queue"?
24
Copyright William C. Cheng
Data Structures - CSCI 102
25
Copyright William C. Cheng
Data Structures - CSCI 102
26
Copyright William C. Cheng
Data Structures - CSCI 102
Deques
Deque: a combination of a Stack and a Queue where you
can insert or remove at either end of the list (but not the
middle)
Like books on a bookshelf
27
Copyright William C. Cheng
Data Structures - CSCI 102
The Deque ADT
28
Copyright William C. Cheng
Data Structures - CSCI 102
Implement Stack Using Deque
29
Copyright William C. Cheng
Data Structures - CSCI 102
Implement Queue Using Deque
30
Copyright William C. Cheng
Data Structures - CSCI 102
CS102
Searching
1
Copyright William C. Cheng
Data Structures - CSCI 102
Search
One of the best ways to understand Big O is by example
3
Copyright William C. Cheng
Data Structures - CSCI 102
4
Copyright William C. Cheng
Data Structures - CSCI 102
Linear Search
int linearSearch(const vector<int>& list,
const int& value) {
for(int i=0;
i < list.size();
i++) {
if (list[i] == value) {
return true;
}
}
return false;
}
5
Copyright William C. Cheng
Data Structures - CSCI 102
Linear Search
What is the best case scenario?
7
Copyright William C. Cheng
Data Structures - CSCI 102
Linear Search
What is the best case scenario?
Search item is first in list = O(1)
8
Copyright William C. Cheng
Data Structures - CSCI 102
Linear Search
What data types does this work for?
Arrays
Vectors
Linked Lists
9
Copyright William C. Cheng
Data Structures - CSCI 102
Linear Search
Search is a very recursive problem. How would we write a
recursive linear search?
10
Copyright William C. Cheng
Data Structures - CSCI 102
Linear Search
int recursiveSearch(const vector<int>& list,
const int& value) {
recSearchHelper(list, value, 0);
}
11
Copyright William C. Cheng
Data Structures - CSCI 102
Search
If we know nothing else about the data in the list were
searching, is there a better way?
12
Copyright William C. Cheng
Data Structures - CSCI 102
Binary Search
Assumption: List elements are sorted in ascending order
13
Copyright William C. Cheng
Data Structures - CSCI 102
Binary Search
int binarySearch(const vector<int>& list,
const int& value) {
int first=0, last=list.size()-1;
while (first <= last) {
int mid=(first+last)/2;
if (list[mid] == value) {
return true;
} else if (list[mid] > value) {
last = mid-1;
} else {
first = mid+1;
} first mid last
}
return false;
}
14
Copyright William C. Cheng
Data Structures - CSCI 102
Binary Search
What is the best case scenario?
15
Copyright William C. Cheng
Data Structures - CSCI 102
Binary Search
What is the best case scenario?
log n
n
Value is in the middle of the list = O(1)
1
2
What is the worst case scenario?
2
4
3
8
Value is not in the list = O(log(N))
4
16
a
5
32
if a
= log2 n, then 2
=
xn
6
64
therefore, x = log2 2
7
128
as n grows geometrically/exponentially,
8
256
log2 n grows linearly
9
512
the base of logarithm is usually omitted
10 1024
11 2048
logb n = logx n / logx b for any base x
12 4096
logb n = log2 n / log2 b = c log2 n
16
Copyright William C. Cheng
Data Structures - CSCI 102
Binary Search
What is the best case scenario?
Value is in the middle of the list = O(1)
What is the worst case scenario?
Value is not in the list = O(log(N))
17
Copyright William C. Cheng
Data Structures - CSCI 102
Binary Search
What data types does this work for?
Arrays
Vectors
Linked Lists (will not work)
18
Copyright William C. Cheng
Data Structures - CSCI 102
Linear Search
O(N)
T(N)
Binary Search
O(log(N))
19
Copyright William C. Cheng
Data Structures - CSCI 102
Stuff to Read
Stack Overflow Question: "Plain English explanation of Big O"
http://stackoverflow.com/questions/487258/plain-english-
explanation-of-big-o
Check out the accepted answer
20
Copyright William C. Cheng