– List ADT Tutorial 4

advertisement
Tutorial 4 – List ADT
List ADT
• List:
– A collection of homogeneous items
ordered in linear fashion, e.g.
• List of names, phone numbers, integers, etc
• List ADT
– An ADT to support such data structure,
with some basic operations, e.g.
• add(idx), get(idx), remove(idx), del_last, size, etc
2
List ADT Implementation
• Basically there are at least two ways:
– Use Vector (i.e. array that can resize itself)
• Part of the implementation is discussed in Q1 & Q4
– Use Linked List
• Discussed in Q3
• In Q2, we uses list ADT, which can be
either using vector or linked list!
3
Linked List: Revision
•
A Linked List Node has 2 parts:
–
–
•
•
See the right hand side
Best is to use STL <list> (Slide 70-72)
–
A bug-free, ready-to-use Linked List 
•
Can visit the tail very fast 
Cannot delete the tail easily… 
In lecture note: not highlighted
(revisited in Queue data structure later)
Double Linked List (Slide 46-56)
–
–
–
–
There are Linked List Variations
–
•
Chain of linked list nodes
Traversal: start from head to tail
Usually: insert from head (ok for Stack)
Linked List with Tail Pointer
–
–
–
Item/Value/Content
Pointers to immediate neighbors
Single Linked List
–
–
–
•
•
Two pointers: forward/backward
Can go backwards
Can delete tail easily! 
Extra pointer = more overhead 
Circular Linked List (Slide 57-60)
–
–
Remember Tail only, Head = Tail.Next
Can visit all node from any node 
Student Presentation
•
1.
2.
3.
4.
•
Yan Xun
Yong Meng
Joseph Ling
Min Qi
T10:
1.
2.
3.
4.
•
•
T5:
Song Yih
Lixun
Peidong
Muhammad Faizal
T9:
1.
2.
3.
4.
•
Lam Woon Cherk
Mah Chun How
Muhammad Daren Meisa
Ngoo Cheng Han
T17:
1.
2.
3.
4.
Claudine
Guochen
Agrawal
Jack
T13:
1.
2.
3.
4.
Jingui
Chee Chung
Guolong
Ee Chan
5
Q1 – Additional Issues
1. Additional methods, try to implement these:
A. add(idx, i)
•
•
This is an overloaded methods: same name as add(i)
It adds integer i in position idx, not just at the back
B. del(idx)
•
–
It deletes an item at position idx, not just at the back
How will you implement them in IntVector?
2. If your size() is O(n), make it O(1)!
–
–
O(n) – one single pass
O(1) – constant step(s)
Q2 – Additional Issues
1. What is the weakness of
A. addPoint(Point p)?
B. findNearest(Point refPt, Point& ansPt)?
Q3 – Additional Issues
1. A “better” Linked List
A. How to access last node in O(1)?
B. How to insert item after last node in O(1)?
C. How to delete last node in O(1)?
Q4 – Additional Issues
1. How to know the runtime details of the methods
in STL?
Next Week
• ThisWeek.Next is recess week
– Use your recess week carefully!
• Revise up to Stack/Queue
– We will discuss stack/queue in Tutorial 5
– CS1102C midterm test is ThisWeek.Next.Next
• Main topic will be List ADT (vector/linked list)
and Stack/Queue
• Although C++ is not the main topic, it is usually
asked as part of the solution
– e.g. write C++ code to modify this Linked List, etc
Download