1/ 124 cop 3503 fall 2012 shayan javed lecture 18 programming fundamentals using java 1
TRANSCRIPT
![Page 1: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/1.jpg)
1/ 124
1
COP 3503 FALL 2012SHAYAN JAVED
LECTURE 18
Programming Fundamentals using Java
![Page 2: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/2.jpg)
2/ 124
Data Structures
![Page 3: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/3.jpg)
3/ 124
So far...
Looked at Arrays and ArrayLists as our data structures
![Page 4: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/4.jpg)
4/ 124
So far...
Looked at Arrays and ArrayLists as our data structures
Very useful, but not always the best options
![Page 5: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/5.jpg)
5/ 124
So far...
Looked at Arrays and ArrayLists as our data structures
Very useful, but not always the best options
Going to look at some other data structures
![Page 6: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/6.jpg)
6/ 124
Data Structures
Stack Queue Linked List Trees Graph Hashtable etc.
![Page 7: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/7.jpg)
7/ 124
Data Structures
Stack Queue Linked List Trees Graph Hashtable etc.
![Page 8: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/8.jpg)
8/ 124
Stack
Last-In-First-Out (LIFO) Data Structure
![Page 9: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/9.jpg)
9/ 124
Stack
Last-In-First-Out (LIFO) Data Structure
Basically...a stack of data.
![Page 10: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/10.jpg)
10/ 124
Stack
Last-In-First-Out (LIFO) Data Structure
Basically...a stack of data.
Used when you want the oldest added data first.
![Page 11: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/11.jpg)
11/ 124
Stack
Last-In-First-Out (LIFO) Data Structure
Basically...a stack of data.
Used when you want the oldest added data first.
Abstract data type (can store any kind of data).
![Page 12: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/12.jpg)
12/ 124
Stack
Three Operations:
![Page 13: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/13.jpg)
13/ 124
Stack
Three Operations:
push(Object): Pushes an object on top of a stack
![Page 14: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/14.jpg)
14/ 124
Stack
Three Operations:
push(Object): Pushes an object on top of a stack
pop(): Returns the object at the top of the stack and removes it
![Page 15: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/15.jpg)
15/ 124
Stack
Three Operations:
push(Object): Pushes an object on top of a stack
pop(): Returns the object at the top of the stack and removes it
peek(): Returns the object at the top without removing it
![Page 16: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/16.jpg)
16/ 124
Stack
Some uses:
![Page 17: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/17.jpg)
17/ 124
Stack
Some uses:
“Stack Frame” for method calls.
![Page 18: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/18.jpg)
18/ 124
Stack
Some uses:
“Stack Frame” for method calls.
Used for evaluating arithmetic expressions: (prefix, postfix, infix)
![Page 19: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/19.jpg)
19/ 124
Stack
Java provides a Stack class (java.util.Stack)
![Page 20: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/20.jpg)
20/ 124
Stack
Java provides a Stack class (java.util.Stack)
Has all the operations
![Page 21: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/21.jpg)
21/ 124
Stack
Java provides a Stack class (java.util.Stack)
Has all the operations
But how does it actually store the data? (What’s the “back-end”?)
![Page 22: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/22.jpg)
22/ 124
Stack
Java provides a Stack class (java.util.Stack)
Has all the operations
But how does it actually store the data? (What’s the “back-end”?)
Uses the java.util.Vector class (similar to ArrayList)
![Page 23: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/23.jpg)
23/ 124
Queue
A First-In-First-Out (FIFO) data structure.
![Page 24: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/24.jpg)
24/ 124
Queue
A First-In-First-Out (FIFO) data structure.
Used when you want the oldest added data first.
![Page 25: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/25.jpg)
25/ 124
Queue
A First-In-First-Out (FIFO) data structure.
Used when you want the oldest added data first.
Often used for scheduling (handle first in line)
![Page 26: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/26.jpg)
26/ 124
Queue
Three Operations:
enqueue(Object): Adds an object to the queue
dequeue(): Returns the object at the front of the queue and removes it
peek(): Returns the object at the front without removing it
![Page 27: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/27.jpg)
27/ 124
Queue
Java provides a Queue interface (java.util.Queue)
![Page 28: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/28.jpg)
28/ 124
Queue
Java provides a Queue interface (java.util.Queue)
Has all the operations enqueue = add dequeue = poll
![Page 29: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/29.jpg)
29/ 124
Queue
Java provides a Queue interface (java.util.Queue)
Has all the operations enqueue = add dequeue = poll
Interface implemented in classes like LinkedList
![Page 30: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/30.jpg)
30/ 124
Queue
Java provides a Queue interface (java.util.Queue)
Has all the operations enqueue = add dequeue = poll
Interface implemented in classes like LinkedList
Queue<Integer> queue = new LinkedList<Integer>();
![Page 31: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/31.jpg)
31/ 124
Arrays and ArrayLists
Work well in a lot of cases.
![Page 32: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/32.jpg)
32/ 124
Arrays and ArrayLists
Work well in a lot of cases.
Can use as “back-end” data structures
![Page 33: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/33.jpg)
33/ 124
Arrays and ArrayLists
Work well in a lot of cases.
Can use as “back-end” data structures
But where do they fall short?
![Page 34: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/34.jpg)
34/ 124
Arrays and ArrayLists
Work well in a lot of cases.
Can use as “back-end” data structures
But where do they fall short? Data retrieval – excellent O(1)
![Page 35: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/35.jpg)
35/ 124
Arrays and ArrayLists
Work well in a lot of cases.
Can use as “back-end” data structures
But where do they fall short? Data retrieval – excellent O(1) Adding data
![Page 36: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/36.jpg)
36/ 124
Arrays and ArrayLists
Work well in a lot of cases.
Can use as “back-end” data structures
But where do they fall short? Data retrieval – excellent O(1) Adding data – inefficient O(n) in some cases
![Page 37: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/37.jpg)
37/ 124
Arrays and ArrayLists
Work well in a lot of cases.
Can use as “back-end” data structures
But where do they fall short? Data retrieval – excellent O(1) Adding data – inefficient O(n) in some cases
![Page 38: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/38.jpg)
38/ 124
Arrays and ArrayLists
Work well in a lot of cases.
Can use as “back-end” data structures
But where do they fall short? Data retrieval – excellent O(1) Adding data – inefficient O(n) in some cases Fixed size – have to shift/copy to new array
![Page 39: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/39.jpg)
39/ 124
Arrays and ArrayLists
What if your application requires a lot of adds but not retrievals?
![Page 40: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/40.jpg)
40/ 124
Arrays and ArrayLists
What if your application requires a lot of adds but not retrievals?
Use Linked Lists
![Page 41: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/41.jpg)
41/ 124
Linked List
A list – a collection of linked nodes.
![Page 42: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/42.jpg)
42/ 124
Linked List
A list – a collection of linked nodes.
Dynamic data structure – size is not fixed, and memory is not contiguous (unlike an array)
![Page 43: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/43.jpg)
43/ 124
Linked List
A list – a collection of linked nodes.
Dynamic data structure – size is not fixed, and memory is not contiguous (unlike an array)
Insertion to the list is very fast – O(1) in most cases
![Page 44: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/44.jpg)
44/ 124
Linked List
A list – a collection of linked nodes.
Dynamic data structure – size is not fixed, and memory is not contiguous (unlike an array)
Insertion to the list is very fast – O(1) in most cases
Indexing is slow. Random access also not possible
![Page 45: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/45.jpg)
45/ 124
Linked List
Many variations:
Singly-Linked List (can only traverse forward) Doubly-Linked List (can traverse in reverse too) Circular Linked Lists Multi-Linked Lists etc.
![Page 46: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/46.jpg)
46/ 124
Node
Basic structure – collection of linked nodes make a linked list.
![Page 47: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/47.jpg)
47/ 124
Node
Basic structure – collection of linked nodes make a linked list.
Stores some data and a link to the next Node.
![Page 48: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/48.jpg)
48/ 124
Node
Basic structure – collection of linked nodes make a linked list.
Stores some data and a link to the next Node.
int null
![Page 49: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/49.jpg)
49/ 124
Node
Basic structure – collection of linked nodes make a linked list.
Stores some data and a link to the next Node.
int int null
![Page 50: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/50.jpg)
50/ 124
Singly-Linked List (SLL)
Nodes connected to each other
![Page 51: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/51.jpg)
51/ 124
Singly-Linked List (SLL)
Nodes connected to each other
int int nullint
![Page 52: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/52.jpg)
52/ 124
Singly-Linked List (SLL)
Nodes connected to each other
SLL only stores links to two nodes:
int int nullint
![Page 53: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/53.jpg)
53/ 124
Singly-Linked List (SLL)
Nodes connected to each other
SLL only stores links to two nodes: Head node (front)
int int nullint
head
![Page 54: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/54.jpg)
54/ 124
Singly-Linked List (SLL)
Nodes connected to each other
SLL only stores links to two nodes: Head node (front) Tail node (end)
int int nullint
head tail
![Page 55: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/55.jpg)
55/ 124
Singly-Linked List (SLL)
Operations: LinkedList(): create an empty list
![Page 56: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/56.jpg)
56/ 124
Singly-Linked List (SLL)
Operations: LinkedList(): create an empty list append(Object): add to the end
![Page 57: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/57.jpg)
57/ 124
Singly-Linked List (SLL)
Operations: LinkedList(): create an empty list append(Object): add to the end insert(Object, index): add at a certain index
![Page 58: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/58.jpg)
58/ 124
Singly-Linked List (SLL)
Operations: LinkedList(): create an empty list append(Object): add to the end insert(Object, index): add at a certain index remove(index): remove Object at index remove(Object): remove Object
![Page 59: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/59.jpg)
59/ 124
Singly-Linked List (SLL)
Operations: LinkedList(): create an empty list append(Object): add to the end insert(Object, index): add at a certain index remove(index): remove Object at index remove(Object): remove Object get(Object): return index of Object get(index): return Object at index
![Page 60: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/60.jpg)
60/ 124
Singly-Linked List (SLL)
Operations: LinkedList(): create an empty list append(Object): add to the end insert(Object, index): add at a certain index remove(index): remove Object at index remove(Object): remove Object get(Object): return index of Object get(index): return Object at index size(): return size of the list
![Page 61: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/61.jpg)
61/ 124
Singly-Linked List (SLL)
Operations: LinkedList(): create an empty list append(Object): add to the end insert(Object, index): add at a certain index remove(index): remove Object at index remove(Object): remove Object get(Object): return index of Object get(index): return Object at index size(): return size of the list clear(): empty the list
![Page 62: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/62.jpg)
62/ 124
Singly-Linked List (SLL)
Create an Empty SLL:
![Page 63: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/63.jpg)
63/ 124
Singly-Linked List (SLL)
Create an Empty SLL:
head tail
null
![Page 64: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/64.jpg)
64/ 124
Singly-Linked List (SLL)
Create an Empty SLL:
public SLL() {
head = null;
tail = null;
}
head tail
null
![Page 65: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/65.jpg)
65/ 124
Singly-Linked List (SLL)
append(Object):
![Page 66: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/66.jpg)
66/ 124
Singly-Linked List (SLL)
append(Object):
Create a Node with that Object
Obj null
![Page 67: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/67.jpg)
67/ 124
Singly-Linked List (SLL)
append(Object):
Create a Node with that Object
Obj null
head tail
![Page 68: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/68.jpg)
68/ 124
Singly-Linked List (SLL)
append(Object): append another object
Obj null
head tail
![Page 69: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/69.jpg)
69/ 124
Singly-Linked List (SLL)
append(Object): append another object
Create a Node with that Object
Obj null
head tail
Obj null
![Page 70: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/70.jpg)
70/ 124
Singly-Linked List (SLL)
append(Object): append another object
Create a Node with that Object Make tail point to it
Obj
head tail
Obj null
![Page 71: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/71.jpg)
71/ 124
Singly-Linked List (SLL)
append(Object): append another object
Create a Node with that Object Make tail point to it Update tail Node
Obj
head tail
Obj null
![Page 72: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/72.jpg)
72/ 124
Singly-Linked List (SLL)
size():
![Page 73: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/73.jpg)
73/ 124
Singly-Linked List (SLL)
size():
Obj
head tail
Obj nullObj Obj
![Page 74: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/74.jpg)
74/ 124
Singly-Linked List (SLL)
size():
Obj
head tail
Obj nullObj Obj
temp
![Page 75: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/75.jpg)
75/ 124
Singly-Linked List (SLL)
size():
Obj
head tail
Obj nullObj Obj
temp
![Page 76: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/76.jpg)
76/ 124
Singly-Linked List (SLL)
size():
Obj
head tail
Obj nullObj Obj
temp
![Page 77: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/77.jpg)
77/ 124
Singly-Linked List (SLL)
size():
Obj
head tail
Obj nullObj Obj
temp
![Page 78: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/78.jpg)
78/ 124
Singly-Linked List (SLL)
size():
Obj
head tail
Obj nullObj Obj
temp
![Page 79: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/79.jpg)
79/ 124
Singly-Linked List (SLL)
size():
Keep incrementing until you reach “null”.
Obj
head tail
Obj nullObj Obj
temp
![Page 80: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/80.jpg)
80/ 124
Singly-Linked List (SLL)
get(Object): Returns index of first Node with that object
![Page 81: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/81.jpg)
81/ 124
Singly-Linked List (SLL)
get(Object): Returns index of first Node with that object
You should know how to traverse
![Page 82: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/82.jpg)
82/ 124
Singly-Linked List (SLL)
get(Object): Returns index of first Node with that object
You should know how to traverse
Compare object with equals() method
![Page 83: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/83.jpg)
83/ 124
Singly-Linked List (SLL)
get(Object): Returns index of first Node with that object
You should know how to traverse
Compare object with equals() method Return index if found -1 if not found
![Page 84: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/84.jpg)
84/ 124
Singly-Linked List (SLL)
remove(Object):
Obj
head tail
Obj nullObj Obj
![Page 85: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/85.jpg)
85/ 124
Singly-Linked List (SLL)
remove(Object):
Obj
head tail
Obj nullObj Obj
![Page 86: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/86.jpg)
86/ 124
Singly-Linked List (SLL)
remove(Object):
Obj
head tail
Obj nullObj Obj
![Page 87: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/87.jpg)
87/ 124
Singly-Linked List (SLL)
remove(Object):
Obj
head tail
Obj nullObj
Obj
![Page 88: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/88.jpg)
88/ 124
Singly-Linked List (SLL)
remove(Object):
Obj
head tail
Obj nullObj
![Page 89: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/89.jpg)
89/ 124
Singly-Linked List (SLL)
remove(Object):
remove(index) - remove at a certain index remove(Node) - remove a certain Node
Work the same way
Obj
head tail
Obj nullObj
![Page 90: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/90.jpg)
90/ 124
Singly-Linked List (SLL)
So now we know how to: append, prepend, insert
![Page 91: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/91.jpg)
91/ 124
Singly-Linked List (SLL)
So now we know how to: append, prepend, insert find the size
![Page 92: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/92.jpg)
92/ 124
Singly-Linked List (SLL)
So now we know how to: append, prepend, insert find the size find the index of a specific element
![Page 93: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/93.jpg)
93/ 124
Singly-Linked List (SLL)
So now we know how to: append, prepend, insert find the size find the index of a specific element remove an object/Node
![Page 94: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/94.jpg)
94/ 124
Singly-Linked List (SLL)
So now we know how to: append, prepend, insert find the size find the index of a specific element remove an object/Node
What about sorting?
![Page 95: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/95.jpg)
95/ 124
Singly-Linked List (SLL)
Sorting:
Bubble Sort Selection Sort Insertion Sort
![Page 96: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/96.jpg)
96/ 124
Singly-Linked List (SLL)
Sorting:
Bubble Sort Selection Sort Insertion Sort
How many of these can you implement for Linked Lists?
![Page 97: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/97.jpg)
97/ 124
Singly-Linked List (SLL)
Sorting: We should swap nodes
![Page 98: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/98.jpg)
98/ 124
Singly-Linked List (SLL)
Sorting: We should swap nodes
Obj
head tail
Obj nullObj Obj
![Page 99: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/99.jpg)
99/ 124
Singly-Linked List (SLL)
Sorting: We should swap nodes
Obj
head tail
Obj nullObj Obj
![Page 100: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/100.jpg)
100/ 124
Singly-Linked List (SLL)
Sorting: We should swap nodes
Obj
head tail
Obj null
Obj
Obj
![Page 101: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/101.jpg)
101/ 124
Singly-Linked List (SLL)
Sorting: We should swap nodes
Obj
head tail
Obj null
Obj
Obj
![Page 102: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/102.jpg)
102/ 124
Singly-Linked List (SLL)
Sorting: We should swap nodes
Obj
head tail
Obj null
Obj
Obj
![Page 103: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/103.jpg)
103/ 124
Singly-Linked List (SLL)
Sorting: We should swap nodes
Obj
head tail
Obj nullObjObj
![Page 104: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/104.jpg)
104/ 124
Singly-Linked List (SLL)
Sorting: We should swap nodes
How many nodes were changed?
Obj
head tail
Obj nullObjObj
![Page 105: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/105.jpg)
105/ 124
Singly-Linked List (SLL)
Sorting: We should swap nodes
How many nodes were changed? 3
Obj
head tail
Obj nullObjObj
![Page 106: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/106.jpg)
106/ 124
Singly-Linked List (SLL)
Swap:
Obj
head tail
Obj nullObj Obj Obj
![Page 107: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/107.jpg)
107/ 124
Singly-Linked List (SLL)
Swap:
Obj
head tail
Obj nullObj Obj Obj
![Page 108: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/108.jpg)
108/ 124
Singly-Linked List (SLL)
Swap:
Also have to modify the ones before them
Obj
head tail
Obj nullObj Obj Obj
![Page 109: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/109.jpg)
109/ 124
Singly-Linked List (SLL)
Swap:
Also have to modify the ones before them
Obj
head tail
Obj nullObj Obj Obj
node1 node2
![Page 110: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/110.jpg)
110/ 124
Singly-Linked List (SLL)
Swap:
Also have to modify the ones before them
Obj
head tail
Obj nullObj Obj Obj
node1 node2
node1P node2P
![Page 111: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/111.jpg)
111/ 124
Singly-Linked List (SLL)
Swap: RESULT:
Also have to modify the ones before them We have now seen two cases
Obj
head tail
Obj nullObj Obj Obj
node2 node1
node1P node2P
![Page 112: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/112.jpg)
112/ 124
Singly-Linked List (SLL)void swap(Node node1, Node node2, Node node1P, Node node2P) {
if (node1.next == node2) { // side-by-side (1st case)
node1.next = node2.next;
node2.next = node1;
}
else { // 2nd case
Node temp = node1.next;
node1.next = node2.next;
node2.next = temp;
node2P.next = node1;
}
if (node1P != null) // why?
node1P.next = node2;
// what about the head and tail nodes?
// update them accordingly
}
![Page 113: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/113.jpg)
113/ 124
Singly-Linked List (SLL)void swap(Node node1, Node node2, Node node1P, Node node2P) {
if (node1.next == node2) { // side-by-side (1st case)
node1.next = node2.next;
node2.next = node1;
}
else { // 2nd case
Node temp = node1.next;
node1.next = node2.next;
node2.next = temp;
node2P.next = node1;
}
if (node1P != null) // why?
node1P.next = node2;
// what about the head and tail nodes?
// update them accordingly
}
![Page 114: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/114.jpg)
114/ 124
Singly-Linked List (SLL)void swap(Node node1, Node node2, Node node1P, Node node2P) {
if (node1.next == node2) { // side-by-side (1st case)
node1.next = node2.next;
node2.next = node1;
}
else { // 2nd case
Node temp = node1.next;
node1.next = node2.next;
node2.next = temp;
node2P.next = node1;
}
if (node1P != null) // why?
node1P.next = node2;
// what about the head and tail nodes?
// update them accordingly
}
![Page 115: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/115.jpg)
115/ 124
Singly-Linked List (SLL)void swap(Node node1, Node node2, Node node1P, Node node2P) {
if (node1.next == node2) { // side-by-side (1st case)
node1.next = node2.next;
node2.next = node1;
}
else { // 2nd case
Node temp = node1.next;
node1.next = node2.next;
node2.next = temp;
node2P.next = node1;
}
if (node1P != null) // why?
node1P.next = node2;
// what about the head and tail nodes?
// update them accordingly
}
![Page 116: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/116.jpg)
116/ 124
Singly-Linked List (SLL)void swap(Node node1, Node node2, Node node1P, Node node2P) {
if (node1.next == node2) { // side-by-side (1st case)
node1.next = node2.next;
node2.next = node1;
}
else { // 2nd case
Node temp = node1.next;
node1.next = node2.next;
node2.next = temp;
node2P.next = node1;
}
if (node1P != null) // why?
node1P.next = node2;
// what about the head and tail nodes?
// update them accordingly
}
![Page 117: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/117.jpg)
117/ 124
Singly-Linked List (SLL)void swap(Node node1, Node node2, Node node1P, Node node2P) {
if (node1.next == node2) { // side-by-side (1st case)
node1.next = node2.next;
node2.next = node1;
}
else { // 2nd case
Node temp = node1.next;
node1.next = node2.next;
node2.next = temp;
node2P.next = node1;
}
if (node1P != null) // why?
node1P.next = node2;
// what about the head and tail nodes?
// update them accordingly
}
![Page 118: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/118.jpg)
118/ 124
Singly-Linked List (SLL)void swap(Node node1, Node node2, Node node1P, Node node2P) {
if (node1.next == node2) { // side-by-side (1st case)
node1.next = node2.next;
node2.next = node1;
}
else { // 2nd case
Node temp = node1.next;
node1.next = node2.next;
node2.next = temp;
node2P.next = node1; // the node before node2
}
if (node1P != null) // why?
node1P.next = node2;
// what about the head and tail nodes?
// update them accordingly
}
![Page 119: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/119.jpg)
119/ 124
Singly-Linked List (SLL)void swap(Node node1, Node node2, Node node1P, Node node2P) {
if (node1.next == node2) { // side-by-side (1st case)
node1.next = node2.next;
node2.next = node1;
}
else { // 2nd case
Node temp = node1.next;
node1.next = node2.next;
node2.next = temp;
node2P.next = node1; // the node before node2
}
if (node1P != null) // why?
node1P.next = node2;
// what about the head and tail nodes?
// update them accordingly
}
![Page 120: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/120.jpg)
120/ 124
Singly-Linked List (SLL)void swap(Node node1, Node node2, Node node1P, Node node2P) {
if (node1.next == node2) { // side-by-side (1st case)
node1.next = node2.next;
node2.next = node1;
}
else { // 2nd case
Node temp = node1.next;
node1.next = node2.next;
node2.next = temp;
node2P.next = node1; // the node before node2
}
if (node1P != null) // why?
node1P.next = node2;
// what about the head and tail nodes?
// update them accordingly
}
![Page 121: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/121.jpg)
121/ 124
Singly-Linked List (SLL)
Sorting: So swap is not straightforward
![Page 122: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/122.jpg)
122/ 124
Singly-Linked List (SLL)
Sorting: So swap is not straightforward
Once you figure out swap, you can implement Bubble and Selection Sort
![Page 123: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/123.jpg)
123/ 124
Singly-Linked List (SLL)
Sorting: So swap is not straightforward
Once you figure out swap, you can implement Bubble and Selection Sort
Try to implement the Sorting Methods for Linked Lists
![Page 124: 1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1](https://reader030.vdocuments.net/reader030/viewer/2022032414/56649ee65503460f94bf61a8/html5/thumbnails/124.jpg)
124/ 124
Summary
Arrays/ArrayLists: useful data structures but not always optimal. Retrieval very quick, insertion can be slow
Stacks/Queues: Abstract data types useful for LIFO/FIFO storage. Implemented using arrays/SLLs
Linked Lists: Alternative to arrays – insertion is fast but retrieval is slow.