Title: COMP104 Review Session
1COMP104 Review Session
2Objectives
- Review concepts and common mistakes in COMP104
materials (after mid-term) - Pointers
- Linked lists
- Classes and ADTs
- Stacks
- NO hints on final examination questions and
coverage! - Materials NOT in this review session will still
be examined!
3Pointers
- Variables which store data as addresses/memory
references - Every variable is stored in memory which has its
own address - gt We have pointers to every variable types
- primitive types e.g. int
- structs, classes
- even pointer to int, pointer to pointer to int,
etc
4Pointers
- Syntax
- and is used in some other ways in C
- pass-by-reference, bitwise AND
- logical AND
- multiply
int a 3 // variable integer int b //
variable pointer to an integer variable b
a // assign the address of integer variable a
to b int c c b // assign the value where b
points to
5Pointers
int a 3 int b b a int c c
b int d b d 7
NOT changed!
6More on Pointers
- Can we do more on pointers?
- Yes. Dynamic memory, pointer arithmetic, linked
lists, etc - Without pointers, all the variables are static
- They are allocated in compilation time, e.g. we
cannot change the size of array (int b10) in
runtime
7Dynamic Memory
int a new int // variable pointer to an
int int b new int4 // variable pointer to
an array of int int c new int2 //
variable pointer to an array of // pointer
to int c0 new int3 c1 new int3 //
valid assignments a 3 b0 4 c01
5 // deallocation delete a // deallocate an
int delete b // deallocate an array of
int delete c0 // deallocate an array of
pointers to int delete c1 delete c
8Dynamic Memory
int a new int int b new int4 int c
new int2 c0 new int3 c1 new
int3 // valid assignments a 3 b0
4 c01 5
1060-106B
1050-105B
1060
1044
c1
1050
1040
c0
1008-1017
Heap memory
9Pointer Arithmetic
- Array variable is a pointer to the base element,
i.e. 0 - (a 1) is equivalent to a1
- ((a 1) 2) is equivalent to a12
10Notes on Pointers
- Usually the LHS and RHS of assignment operator
are of the same type! - Usually, how many times you have new the
objects, you need to delete the same number of
objects
11Linked List
- You have learnt many examples (single, circular
and doubly) in lectures! - They share some common characteristics
- Advantages over dynamic array incremental
enlargement is possible - Start from basics
- A linear sequence of nodes connected together
- Concept of head and tail
Head
Tail
12Basic Operations on Linked List
Operations with NO modification to the linked list
- Traversal
- Walk along the list from head to tail, e.g.
printing - Searching
- Similar to traversal but operation will stop
immediately when some criteria are met - Different behaviors on unsorted and sorted linked
lists - Unsorted linked list Stop when
- target is found or
- the end of the list is reached
- Sorted linked list Stop when
- target is found or
- the end of the list is reached or
- the remaining nodes must not contain such target,
usually larger or smaller than the target
13Basic Operations on Linked List
Operations with modification to the linked list
- Insertion
- Add one more node to the linked list
- Deletion
- Remove a node from the linked list
14Cases to Handle in Linked List
- With different implementation of linked list
(single, circular, doubly, with/without dummy
head, etc), exact cases to be handled varies - But there is a general rule Another case exists
because the behavior is DIFFERENT
15Cases to Handle in Linked List
- Generally speaking
- Traversal, searching
- Empty list, non-empty list
- Insertion
- Empty -gt non-empty
- Insert as new head, new tail or in-between node
- Deletion
- Non-empty -gt empty
- Remove head, tail or in-between node
- Removal item may not exist in the linked list
16Single Linked List (SLL)
Head
Tail
Head
- Traversal and searching
- Start from head node, i.e. Head pointer
- End when reaching the tail node, i.e. NULL
pointer - Unless other criteria are met in searching
- Empty and non-empty case are the same
17Traversal and Searching in SLL
Head
Tail
prev
cur
Head
// search for proper insertion/deletion //
location in SLL NodePtr prev NULL, cur
Head while (cur!NULL item gt
cur-gtdata) prev cur cur cur-gtnext
18Insertion in SLL
Handled all the cases?
What if the list is originally empty?
In-between
New tail
new
Head
cur
prev
New head
19Deletion in SLL
What if the target item does not exist? Or there
are only one element originally?
Handled all the cases?
X
prev
Head
cur
In-between
X
Head
prev
cur
Tail
X
Head
cur
prev
Head
20Summary on SLL
- Traversal and searching
- Empty and non-empty cases are the same
- Insertion
- In-between node and tail node cases are the same
- Head node and empty list cases are the same
- Deletion
- In-between node and tail node cases are the same
- Head node and single-element cases are the same
- Special handling on non-existing target item
21Circular Linked List (CLL)
- Traversal and searching
- Start from head node, i.e. Rear-gtnext
- End when reaching the tail node, i.e. Rear
- Empty and non-empty cases are DIFFERENT
Head
Tail
Rear
22Traversal and Searching in CLL
Head
Tail
prev
Rear
cur
// search for proper insertion/deletion //
location in CLL if (Rear ! NULL) NodePtr prev
Rear, cur Rear-gtnext do if(item lt
Cur-gtdata) break Prev Cur Cur
Cur-gtnext while(Cur ! Rear-gtnext)
23Insertion in CLL
Handled all the cases?
What if the list is originally empty?
new
Rear
prev
cur
In-between
new
Rear
prev
New tail
cur
Rear
cur
new
prev
New head
24Deletion in CLL
What if the target item does not exist? Or there
are only one element originally?
Handled all the cases?
X
Rear
prev
cur
In-between
X
Rear
prev
cur
Tail
X
Rear
cur
prev
Head
25Summary on CLL
- Traversal and searching
- Empty and non-empty cases are DIFFERENT
- Insertion
- In-between node and head node cases are the same
- Special handling on tail node case
- Special handling on empty list case
- Deletion
- In-between node and head node cases are the same
- Special handling on tail node case
- Special handling on single-element case
- Special handling on non-existing target item
26Doubly Linked List (DLL) with Dummy Head
- Traversal and searching
- Start from head node, i.e. Head-gtnext
- End when reaching the tail node, i.e. Head-gtprev
- Empty and non-empty cases are the same
Dummy
Head
Tail
Head
27Doubly Linked List (DLL) with Dummy Head
Dummy
Head
Tail
Head
cur
// search for proper insertion/deletion //
location in DLL NodePtr Cur Head-gtnext while(Cu
r ! Head) if(Cur-gtdata item) return
Cur if(Cur-gtdata lt item) Cur
Cur-gtnext else break
28Insertion in DLL
Handled all the cases?
What if the list is originally empty?
Dummy
new
In-between
Head
cur
Dummy
new
Head
cur
New tail
Dummy
new
Head
cur
New head
29Deletion in DLL
What if the target item does not exist? Or there
are only one element originally?
Handled all the cases?
Dummy
X
In-between
Head
cur
Dummy
X
Head
cur
Tail
Dummy
X
Head
cur
Head
30Summary on DLL
- Traversal and searching
- Empty and non-empty cases are the same
- Insertion
- In-between node, head node, tail node and empty
list case are the same - Deletion
- In-between node, head node, tail node and
single-element cases are the same - Special handling on non-existing target item
31Notes on Linked List
- To avoid errors in modifying pointers, try
- Always start with the pointers of newly added
node first, i.e. newNode-gtnext and newNode-gtprev - Then modify pointers in previous and next node
- Last but not lest, a diagram
- Beware of NULL pointer
- Always check if the variable is NULL or not
before using -gt and . operators
32Notes on Linked List
- CLL will have NULL pointers ONLY when it is empty
33Classes and ADTs
- Special user-defined type which consist of
functions and data components - Provide access control on data components as well
as functions (public, private) - Maintain the data components to be in their valid
states
34Components of Classes
class Lift public Lift() //
default-value constructor // explicit-value
constructor Lift(int iFloor, int iNumPeople
0) Lift(const Lift obj) // copy
constructor void moveUp() // mutator
function void moveDown() // mutator
function void print() const // inspector
function Lift() // destructor private
int floor int numPeople
35Components of Classes
- Constructors (initialize the data members in
construction) - Default-value constructor
- Empty parameter list
- Will be supplied by compiler if NO other
constructor is defined - Explicit-value constructor
- Positive number of parameters in parameter list
- Copy constructor
- Copy the data members from another one to myself
- Could have const modifier or not
- Used in
- Lift a Lift b a
- Lift a Lift b(a)
- Pass-by-value void functionA(Lift a)
36Components of Classes
- Destructor (usually deallocate dynamic memory)
- Will be supplied by compiler if no destructor is
defined - Mutator functions
- Functions which change the data components
- Inspector functions
- Functions which do NOT change the data components
- Usually have a const modifier after (), but NOT a
must
37Example functions
// default constructor LiftLift()
numPeople 0 floor 10 // inspector
function void Liftprint() const cout ltlt
Floor ltlt floor ltlt endl cout ltlt Num of
people ltlt numPeople ltlt endl
38Access Control in Classes
- Provide better control on what the class could do
and who can use those function/data members - Public components
- Can be accessed by both member functions and
non-member functions, e.g. main() - Private components
- Can ONLY be accessed by member functions
39Notes on Classes and ADTs
- You must have semi-colon () after the class
definition (the same as struct) - You may have dynamic objects (e.g. linked list,
dynamic array) as data members in your class - DO remember to deallocate their memory in
destructor - Destructor will be called automatically
- Have NO return types for constructors and
destructors
40Stacks
- Common data structure (another example is linked
list) - First-in-last-out (FILO)
- Example
- Trays at LG7
- Assume the trays are put one by one
- The earlier the tray is put, the later the tray
will be taken
41Basic Operations in Stacks
- Pop()
- Return the top element from the stack
- Push(object)
- Insert the object over the top of the stack, i.e.
become the new top element
X
Original top
New top
new
New top
Original top
42Possible Implementation of Stack
- Use a single linked list to store the elements of
the stack - However, insertion and deletion will always occur
at head