Sei sulla pagina 1di 13

Programming C# 4

PCS4
Feb 2019

1
Overview
Wee Subject Book
k
1 Delegates & Events Nothing
2 RFID Reader & Sorting (Selection § 18.3.1
sort)
3 Recursion & Binary search § 7.15 & §18.2
4 Passing arguments by: § 7.16
value/reference
“ref” & “out” parameters
5 Linked Lists, Stacks, Queues § 19.4
6 Binary trees § 19.7
7 Repetition (Mock exam)
8 Exam

2
Content
• Topics
– Data structures
– Linked lists
– Queues and Stacks
• Learning objectives
– Understanding of data structures
– Can implement a linked list
– You can make use of data structure using FIFO
– You can make use of a data structure using LIFO

3
Repetition week 4

Ref parameter Out parameter


Must be initialized on method Doesn’t have to be initialized
call on method call
(if not, compiler error)
Value on method call can be Value on method call can’t be
used used
Value can be changed in the Value must be assigned in the
method method
(if not, compiler error)

4
Data types and data structures
• A data type is a type of information we can store.
• A data structure is a structure to store/adjust/retrieve this data.

5
Linked lists
• Knows its neighbor
– Next, previous of both
• Precise size of memory
– Grows dynamically
– Shrinks dynamically
• Searching takes longer than normal arrays!

Sixth
Element

First Second Third Fourth Fifth


Element Element Element Element Element

6
Types of Linked lists
• Singly
10| 20| 30|
null
next next next

head
• Doubly

prev|10| prev|20| prev|30|


nul null
next next next
l

head

7
Linked list implementation
• How to program the structure?
• What operations will we be doing on it?
– Get first element
– Get last element
– Get element at
– Add first
– Add last
– Add i’th
– Remove first
– Remove last
– Remove i’th

8
Linked list example

9
Linked list example

10
Queue, First In First Out(FIFO)
• Can only add to the back, and remove/read from the front!
– Enqueue=> (list.Add equivalent to stack)
– Dequeue => (list.Remove)
• Also returns the element!!!

Fifth Fourth Third Second First


Element Element Element Element Element

11
Stack, Last In First Out(LIFO)
• Can only add and delete to front of stack!
– Push => (list.Add equivalent of stack)
– Pop => (list.Remove equivalent of stack)
• Also returns the element!!!
Fourth
Fifth
Element
Third
Element
Second
Element
First
Element

12
13

Potrebbero piacerti anche