arrays lists stacks queues - users.encs.concordia.casthiel/coen352/01b_arrays_lists... · arrays...
TRANSCRIPT
Arrays Lists StacksQueues
S. Thiel
ImplementingSequences
Sequences
Arrays
Lists
Stacks
Queues
Dictionaries
Analysis
References
1/32
Arrays Lists Stacks Queues
S. Thiel1
1Department of Computer Science & Software EngineeringConcordia University
June 26, 2019
Arrays Lists StacksQueues
S. Thiel
ImplementingSequences
Sequences
Arrays
Lists
Stacks
Queues
Dictionaries
Analysis
References
2/32
Outline
Implementing SequencesSequencesArraysListsStacksQueuesDictionariesAnalysis
References
Arrays Lists StacksQueues
S. Thiel
ImplementingSequences
Sequences
Arrays
Lists
Stacks
Queues
Dictionaries
Analysis
References
3/32
Implementing a Sequence Structure 1
I Recall the properties of a sequence
I Elements can be duplicated
I Different permutations constitute a different sequences
I How could we store this information structurally
Arrays Lists StacksQueues
S. Thiel
ImplementingSequences
Sequences
Arrays
Lists
Stacks
Queues
Dictionaries
Analysis
References
4/32
Implementing a Sequence Structure 2
I What is in the squence?
I How many items does it have?
I Where are we in the sequence?
Arrays Lists StacksQueues
S. Thiel
ImplementingSequences
Sequences
Arrays
Lists
Stacks
Queues
Dictionaries
Analysis
References
5/32
Implementing a Sequence Behaviour 1
I What behavior should it have?
I Is it empty?
I What is the first element?
I What is the last element?
I How many elements are there?
Arrays Lists StacksQueues
S. Thiel
ImplementingSequences
Sequences
Arrays
Lists
Stacks
Queues
Dictionaries
Analysis
References
6/32
Sequences and Order
I We say that sequences have an orderI We mean that a different permutation constitutes a
different sequence
I But there is also the idea that the elements can have anorder with respect to each other
I They can actually have many. . .I But we usually want to know if the sequence is sorted
before doing something with itI is this the permutation where each element is ordered
with respect to a given order (e.g. by size, cost,alphabetic. . . some ordinal measure)
I When we consider such orderings, we consider the valueof the element to be what we are comparing.
I Can an item have more than one value?
Arrays Lists StacksQueues
S. Thiel
ImplementingSequences
Sequences
Arrays
Lists
Stacks
Queues
Dictionaries
Analysis
References
7/32
Does Sorting Affect ADT?
I How we sort a data structure will be specific to the datastructure.
I Should the data structure care whether or not it issorted?
I Can we use data structures differently if we know theyare sorted?
Arrays Lists StacksQueues
S. Thiel
ImplementingSequences
Sequences
Arrays
Lists
Stacks
Queues
Dictionaries
Analysis
References
8/32
Implementing an Array
I A Sequence as adjacent values in memory
I This is called an Array
I P = 〈4, 12, 12, 18〉I If they are adjacent values, we can access by index
I What if we access an index bigger than the size?
I Is the size fixed? How can we “fix” this?
I How do we add and remove?
I How do we add in the middle?
I Does the idea of a current element make sense if wecan index?
I Maybe the idea of a current element is “external” to anArray’s implementation? Does it have to be?
Arrays Lists StacksQueues
S. Thiel
ImplementingSequences
Sequences
Arrays
Lists
Stacks
Queues
Dictionaries
Analysis
References
9/32
Implementing a List
I A Sequence as a value in memory and a pointer to thenext value.
I This is called a List
I P = 〈4, 12, 12, 18〉I Is there a concept of index?
I Is there a concept of current element?
I Is the size fixed?
I How do we add and remove?
I How do we add in the middle?
Arrays Lists StacksQueues
S. Thiel
ImplementingSequences
Sequences
Arrays
Lists
Stacks
Queues
Dictionaries
Analysis
References
10/32
Types of List
I We said value in memory, and a pointer to the nextvalue
I Could we have a list of these pointers? What do welose?
I Can we keep these pointers with the value?
I If we point to the next value, we need to know the first
I What if we want to go to the previous element? Can weadd another pointer?
I Singly and Doubly-Linked Lists.
Arrays Lists StacksQueues
S. Thiel
ImplementingSequences
Sequences
Arrays
Lists
Stacks
Queues
Dictionaries
Analysis
References
11/32
Stack Basics
I Like a list
I add and remove from one end
I no direct access, except from that end
I Simple
I Last-In, First-Out (LIFO)
Arrays Lists StacksQueues
S. Thiel
ImplementingSequences
Sequences
Arrays
Lists
Stacks
Queues
Dictionaries
Analysis
References
12/32
Where to Use a Stack
I Freelists from last lectureI Call stack in running code
I Sometimes replace recurions with iteration
Arrays Lists StacksQueues
S. Thiel
ImplementingSequences
Sequences
Arrays
Lists
Stacks
Queues
Dictionaries
Analysis
References
13/32
Stack Operations
I Peek
I Pop
I Push
I . . . some others, like size/clear
Arrays Lists StacksQueues
S. Thiel
ImplementingSequences
Sequences
Arrays
Lists
Stacks
Queues
Dictionaries
Analysis
References
14/32
Stack Implementations
I Array-Based
I List-Based
I What are the respective advantages?
Arrays Lists StacksQueues
S. Thiel
ImplementingSequences
Sequences
Arrays
Lists
Stacks
Queues
Dictionaries
Analysis
References
15/32
Double Stack Implementation
I If items will get passed between two stacks
I Stick them in the same array
Arrays Lists StacksQueues
S. Thiel
ImplementingSequences
Sequences
Arrays
Lists
Stacks
Queues
Dictionaries
Analysis
References
16/32
Stack Examples
I Call Stack (that’s what your Stacktrace is!)
I Dijkstra’s two-stack expression evaluation
Arrays Lists StacksQueues
S. Thiel
ImplementingSequences
Sequences
Arrays
Lists
Stacks
Queues
Dictionaries
Analysis
References
17/32
Queue Basics
I Like a list
I add from one end, remove from the other
I no direct access, except at the ends (usually just front)
I Simple
I First-In, First-Out (FIFO)
Arrays Lists StacksQueues
S. Thiel
ImplementingSequences
Sequences
Arrays
Lists
Stacks
Queues
Dictionaries
Analysis
References
18/32
Where to Use a Queue
I Playlists
I Waiting lists
I Generally when you know the max n
I or when dequeue will happen more
Arrays Lists StacksQueues
S. Thiel
ImplementingSequences
Sequences
Arrays
Lists
Stacks
Queues
Dictionaries
Analysis
References
19/32
Queue Operations
I frontvalue
I enqueue
I dequeue
I . . . some others, like size/clear
Arrays Lists StacksQueues
S. Thiel
ImplementingSequences
Sequences
Arrays
Lists
Stacks
Queues
Dictionaries
Analysis
References
20/32
Queue Implementations
I Array-BasedI circular implementation: yesI empty or full?I keep explicit sizeI or use an array of size n+1
I List-Based
I What are the respective advantages?
Arrays Lists StacksQueues
S. Thiel
ImplementingSequences
Sequences
Arrays
Lists
Stacks
Queues
Dictionaries
Analysis
References
21/32
Circular Queue Implementation
I Can use an array if we never have too many elements
I If two queues in one circular array?
Arrays Lists StacksQueues
S. Thiel
ImplementingSequences
Sequences
Arrays
Lists
Stacks
Queues
Dictionaries
Analysis
References
22/32
Dictionary Basics
I Slightly more complicated
I computers store and retrieve data
I Look something up using a key
I Know if two keys are different, generally have order
I Resulting data is not (or is more) than the key
Arrays Lists StacksQueues
S. Thiel
ImplementingSequences
Sequences
Arrays
Lists
Stacks
Queues
Dictionaries
Analysis
References
23/32
Dictionary Operations
I Clear
I Record
I Remove
I FindI RemoveAny?
I Seems unnatural, just to iterate...
Arrays Lists StacksQueues
S. Thiel
ImplementingSequences
Sequences
Arrays
Lists
Stacks
Queues
Dictionaries
Analysis
References
24/32
Dictionary Implementations 1
I We can call the underlying data the Database
I Array-Based?
I List-Based?
I Other?
Arrays Lists StacksQueues
S. Thiel
ImplementingSequences
Sequences
Arrays
Lists
Stacks
Queues
Dictionaries
Analysis
References
24/32
Dictionary Implementations 1
I We can call the underlying data the Database
I Array-Based?
I List-Based?
I Other?
Arrays Lists StacksQueues
S. Thiel
ImplementingSequences
Sequences
Arrays
Lists
Stacks
Queues
Dictionaries
Analysis
References
25/32
Dictionary Implementations 2
I Different Dictionaries for Different key types?
I Can different things have the same key?
Arrays Lists StacksQueues
S. Thiel
ImplementingSequences
Sequences
Arrays
Lists
Stacks
Queues
Dictionaries
Analysis
References
26/32
Array Operations
I add Θ (1)
I insert Θ (n)
I delete Θ (n)
I look up by index Θ (1)
I What about when Add is too big?
Arrays Lists StacksQueues
S. Thiel
ImplementingSequences
Sequences
Arrays
Lists
Stacks
Queues
Dictionaries
Analysis
References
27/32
LinkedList Operations
I add Θ (1)
I insert Θ (1) or Θ (n)?
I delete Θ (1) or Θ (n)?
I why qualify insert and delete?
I look up by index Θ (n)
I How do LinkedLists deal with the add problem fromarrays? Freelists?
Arrays Lists StacksQueues
S. Thiel
ImplementingSequences
Sequences
Arrays
Lists
Stacks
Queues
Dictionaries
Analysis
References
28/32
Stack Operations
I push Θ (1)
I pop Θ (1)
I how to do it wrong. . .
Arrays Lists StacksQueues
S. Thiel
ImplementingSequences
Sequences
Arrays
Lists
Stacks
Queues
Dictionaries
Analysis
References
29/32
Queue Operations
I enqueue Θ (1)
I dequeue Θ (1)
I how to do it wrong. . .
Arrays Lists StacksQueues
S. Thiel
ImplementingSequences
Sequences
Arrays
Lists
Stacks
Queues
Dictionaries
Analysis
References
30/32
Dictionary Operations
I clear Θ (1) ?I record
I unsorted Θ (1)I sorted Θ (n)
I remove Θ (n)I find
I unsorted Θ (n)I sorted
I Array Θ (log n)I LinkedList Θ (n)
I RemoveAny? Θ (1)
Arrays Lists StacksQueues
S. Thiel
ImplementingSequences
Sequences
Arrays
Lists
Stacks
Queues
Dictionaries
Analysis
References
31/32
Dictionary Operation Costs
I What is the cost of Binary Search in a LinkedList
I Can we fudge that?
I or move away from linear structures
Arrays Lists StacksQueues
S. Thiel
ImplementingSequences
Sequences
Arrays
Lists
Stacks
Queues
Dictionaries
Analysis
References
31/32
Dictionary Operation Costs
I What is the cost of Binary Search in a LinkedList
I Can we fudge that?
I or move away from linear structures
Arrays Lists StacksQueues
S. Thiel
ImplementingSequences
Sequences
Arrays
Lists
Stacks
Queues
Dictionaries
Analysis
References
32/32
References I