Title: MIS 215 Module 1
1MIS 215 Module 1 Unordered Lists
2Where are we?
MIS215
Basic Algorithms
Introduction
List Structures
Advanced structures
Search Techniques
Intro to Java, Course
Sorting Techniques
Java lang. basics
Hashtables
Linked Lists
Binary Search
Graphs, Trees
Stacks, Queues
Arrays
Bubblesort
Fast Sorting algos (quicksort, mergesort)
Newbie
Programmers
Developers
Professionals
Designers
3Todays buzzwords
- Unordered Lists
- Lists that are not REQUIRED to be ordered to be
functional - That does not mean that they cannot be ordered if
necessary - Linked Lists
- A data structure where the lists are implemented
using a dynamically allocated structure, with
nodes and links to the next nodes. - Java References
- A reference in Java is simply a variable of an
object type - All objects are dynamically allocated at runtime
(with new) - Its like a pointer, but still not like a pointer ?
4Overview
- List introduction
- List specifications
- List implementations
- Contiguous
- Simply Linked
- Simply Linked with Position Pointer
- Doubly Linked
- Strings
- Application Text Editor
- Linked Lists in Arrays
- Application Generating Permutations
5Unordered Lists
- Lists that do not have to be ordered to be
functional - Can be ordered if necessary
- Examples?
6The Specifications of General Lists
- Basic operations clear, isListEmpty, isListFull,
size, traverse - List operations insert, remove, find.
The Insert operation insert(Itemtype x) OR
insert(int p, Itemtype
x) Pre The list has been created, and is not
full, x is a valid list entry, and 0ltpltn,
where n is the number of entries in list. Post
x has been inserted into position p in list
the entry formerly in position p (provided pltn)
and all later entries have their position numbers
increased by 1.
7Our List Skeleton (for now)
- /
- Our first List class. Notice the Javadoc style
comments - /
- public class MyList
- /
- The MyList Constructor - we may need
variants later - /
- public MyList()
- /
- The clear operation - removes all entries
from the list - /
- public void clear()
- /
- Check if the list is empty
- /
- public boolean isListEmpty()
- /
- Check if the list is full
- /
8The Implementations of General lists
- Contiguous implementation
- using arrays
- insertions and deletions are done with movement
of data entries - Linked implementations
- singly linked lists
- allocating memory dynamically
- insertions and deletions are done by changing
references - doubly linked lists
- using two references for each entry
9Lets try arrays first!
- What variables should MyList need?
- What possible variations of the constructor?
- What would be useful to know when the list is
being created? - What would you allow your users to set once for
the life of the instance?
10Implementing List as Array
- CFC on the MyList class
- How do we implement clear?
- How do we implement isListEmpty?
- How do we implement isListFull?
11Now for the List operations
12A singly linked list
- Each node in the list contains...
- data (well use only integers, as usual)
- a link to the next node
- in Java, well call this a reference
- (if you know C, its a pointer)
- Heres a picture....
13Links (contd.)
data
next
node
?
14Class Design
Node int Item Node Next
LinkedList Node First
0..M
15Insertion on a Linked List
16Deletions on a Linked List
Stacks
structures
are
simple
but
data
important
17Doubly Linked List
18Comparison of the Implementations
- In processing a continuous list with n entries
- insert and remove require O(n) time
- clear, isListEmpty, isListFull, size operate in
constant time. - We call this O(1) complexity
- In processing a linked list with n entries
- Complexity of insert?
- Complexity of remove?
- Complexity of size?
- Complexity of isListEmpty?
- Complexity of isListFull?
19Complexity Analysis
- Complexity of _____________
- O(1) i.e., Constant time
- O(n) i.e., proportional to items
- More than O(n)?
20Build this table for unordered lists
Operation Array implementation Linked List Implementation
create/constructor O(1) O(1)
size
clear
traverse
isListFull
isListEmpty
insert
remove
find
findAt
21Quick Quiz Accessing items in the chain
- In an array, how do we access an item in the
array (say, the 4th item)? - If we have a chain (i.e., linked list), how do we
get to the 4th item in the chain?
22Analysis of the Implementations
- Contiguous storage is generally preferable
- when the structures are individually small
- when the size of the list is known when the
program is written - when fewer insertions or deletions need to be
made except at the end of the list - when random access is important.
- Linked storage proves superior
- when the structures are large
- when the size of the list is not known in advance
- when flexibility is needed in inserting, deleting
and re-arranging the entries.