CS32 - Week 2 Umut Oztok July 1, 2016 Umut Oztok CS32 - Week 2 - - PowerPoint PPT Presentation

cs32 week 2
SMART_READER_LITE
LIVE PREVIEW

CS32 - Week 2 Umut Oztok July 1, 2016 Umut Oztok CS32 - Week 2 - - PowerPoint PPT Presentation

CS32 - Week 2 Umut Oztok July 1, 2016 Umut Oztok CS32 - Week 2 Arrays A basic data structure (commonly used). Organize data in a sequential way. Umut Oztok CS32 - Week 2 Arrays A basic data structure (commonly used). Organize data in a


slide-1
SLIDE 1

CS32 - Week 2

Umut Oztok July 1, 2016

Umut Oztok CS32 - Week 2

slide-2
SLIDE 2

Arrays

A basic data structure (commonly used). Organize data in a sequential way.

Umut Oztok CS32 - Week 2

slide-3
SLIDE 3

Arrays

A basic data structure (commonly used). Organize data in a sequential way. Pros:

Umut Oztok CS32 - Week 2

slide-4
SLIDE 4

Arrays

A basic data structure (commonly used). Organize data in a sequential way. Pros:

Easy access to elements, i.e., array[5] gets 6th element. Easy to debug.

Cons:

Umut Oztok CS32 - Week 2

slide-5
SLIDE 5

Arrays

A basic data structure (commonly used). Organize data in a sequential way. Pros:

Easy access to elements, i.e., array[5] gets 6th element. Easy to debug.

Cons:

Fixed size. Not easy to insert new elements to the front/middle.

Umut Oztok CS32 - Week 2

slide-6
SLIDE 6

Linked Lists

Another data structure. Like arrays, organizes data in a sequential way. In general, linked lists are strong when arrays are weak, and vice versa.

Umut Oztok CS32 - Week 2

slide-7
SLIDE 7

Linked Lists

Another data structure. Like arrays, organizes data in a sequential way. In general, linked lists are strong when arrays are weak, and vice versa. Basic component is a node: value *next

Umut Oztok CS32 - Week 2

slide-8
SLIDE 8

Linked Lists

Another data structure. Like arrays, organizes data in a sequential way. In general, linked lists are strong when arrays are weak, and vice versa. Basic component is a node: value *next Self-referential structures:

typedef int DataType; struct Node { DataType value; Node* next; };

Umut Oztok CS32 - Week 2

slide-9
SLIDE 9

Linked Lists

A linked list is a series of nodes, each pointing to next one. Node* head 1 5 3 Nil Head pointer to point the first element. Last node’s next pointer is NULL.

Umut Oztok CS32 - Week 2

slide-10
SLIDE 10

Linked Lists

Some operations: Insert:

To the front. To the end. In the middle.

Delete. Search. Traverse.

Umut Oztok CS32 - Week 2

slide-11
SLIDE 11

Traversal

How to traverse the nodes of a linked list? head 1 5 3 Nil

Umut Oztok CS32 - Week 2

slide-12
SLIDE 12

Traversal

How to traverse the nodes of a linked list? head 1 5 3 Nil

void print() {

Umut Oztok CS32 - Week 2

slide-13
SLIDE 13

Traversal

How to traverse the nodes of a linked list? head 1 5 3 Nil

void print() { Node* cur = head;

Umut Oztok CS32 - Week 2

slide-14
SLIDE 14

Traversal

How to traverse the nodes of a linked list? head 1 5 3 Nil

void print() { Node* cur = head; while(cur != nullptr) {

Umut Oztok CS32 - Week 2

slide-15
SLIDE 15

Traversal

How to traverse the nodes of a linked list? head 1 5 3 Nil

void print() { Node* cur = head; while(cur != nullptr) { cout << cur->value << " ";

Umut Oztok CS32 - Week 2

slide-16
SLIDE 16

Traversal

How to traverse the nodes of a linked list? head 1 5 3 Nil

void print() { Node* cur = head; while(cur != nullptr) { cout << cur->value << " "; cur = cur->next;

Umut Oztok CS32 - Week 2

slide-17
SLIDE 17

Traversal

How to traverse the nodes of a linked list? head 1 5 3 Nil

void print() { Node* cur = head; while(cur != nullptr) { cout << cur->value << " "; cur = cur->next; } }

Output: 1 5 3

Umut Oztok CS32 - Week 2

slide-18
SLIDE 18

Insertion [To the front]

How to insert a node with value 6 to the front of the list? From: head 1 5 3 Nil To: head 6 1 5 3 Nil Steps:

Umut Oztok CS32 - Week 2

slide-19
SLIDE 19

Insertion [To the front]

How to insert a node with value 6 to the front of the list? From: head 1 5 3 Nil To: head 6 1 5 3 Nil Steps: Create a new node. Point the node’s next pointer to head. Point head to the node.

Umut Oztok CS32 - Week 2

slide-20
SLIDE 20

Insertion [To the front]

How to insert a node with value 6 to the front of the list? From: head 1 5 3 Nil To: head 6 1 5 3 Nil Steps: Create a new node. Point the node’s next pointer to head. Point head to the node.

void insertToFront(int val) { Node* p = new Node; p->value = val; p->next = head: head = p; }

Umut Oztok CS32 - Week 2

slide-21
SLIDE 21

Insertion [To the end]

How to insert a node with value 6 to the end of the list? From: head 1 5 3 Nil To: head 1 5 3 6 Nil Steps:

Umut Oztok CS32 - Week 2

slide-22
SLIDE 22

Insertion [To the end]

How to insert a node with value 6 to the end of the list? From: head 1 5 3 Nil To: head 1 5 3 6 Nil Steps: Create a new node. Go to the end of the list. Point last node’s next pointer to the new node.

Umut Oztok CS32 - Week 2

slide-23
SLIDE 23

Insertion [To the end]

1

void insertToEnd(int val) {

Umut Oztok CS32 - Week 2

slide-24
SLIDE 24

Insertion [To the end]

1

void insertToEnd(int val) {

2

Node* p = new Node;

3

p->value = val;

4

p->next = nullptr;

5 6

Node* cur = head;

Umut Oztok CS32 - Week 2

slide-25
SLIDE 25

Insertion [To the end]

1

void insertToEnd(int val) {

2

Node* p = new Node;

3

p->value = val;

4

p->next = nullptr;

5 6

Node* cur = head;

7

if(cur == nullptr) head = p;

8

else {

9

while(cur->next != nullptr)

10

cur = cur->next;

11

cur->next = p;

12

}

13

}

Umut Oztok CS32 - Week 2

slide-26
SLIDE 26

Insertion [To the Kth slot]

How to insert a node with value 6 to the 1st slot? (Node with value 1 is located at the 0th slot.) From: head 1 5 3 Nil To: head 1 6 5 3 Nil Steps:

Umut Oztok CS32 - Week 2

slide-27
SLIDE 27

Insertion [To the Kth slot]

How to insert a node with value 6 to the 1st slot? (Node with value 1 is located at the 0th slot.) From: head 1 5 3 Nil To: head 1 6 5 3 Nil Steps: Find the node N at K − 1st slot. Create a new node. "Add" the node to Kth slot. That is:

Make new node’s next pointer point to the node at Kth slot. Make N’s next pointer point to the new node.

Umut Oztok CS32 - Week 2

slide-28
SLIDE 28

Insertion [To the Kth slot]

How to insert a node with value 6 to the 1st slot? (Node with value 1 is located at the 0th slot.) From: head 1 5 3 Nil To: head 1 6 5 3 Nil Steps: Find the node N at K − 1st slot. Create a new node. "Add" the node to Kth slot. That is:

Make new node’s next pointer point to the node at Kth slot. Make N’s next pointer point to the new node.

What if K = 0?

Umut Oztok CS32 - Week 2

slide-29
SLIDE 29

Insertion [To the Kth slot]

1

void insertToKth(int val, int index) {

Umut Oztok CS32 - Week 2

slide-30
SLIDE 30

Insertion [To the Kth slot]

1

void insertToKth(int val, int index) {

2

if(head == nullptr || index == 0) insertToFront(val);

3

else {

Umut Oztok CS32 - Week 2

slide-31
SLIDE 31

Insertion [To the Kth slot]

1

void insertToKth(int val, int index) {

2

if(head == nullptr || index == 0) insertToFront(val);

3

else {

4

Node* cur = head;

5

while(cur->next != nullptr) {

6

if(--index == 0) break;

7

cur = cur->next;

8

}

Umut Oztok CS32 - Week 2

slide-32
SLIDE 32

Insertion [To the Kth slot]

1

void insertToKth(int val, int index) {

2

if(head == nullptr || index == 0) insertToFront(val);

3

else {

4

Node* cur = head;

5

while(cur->next != nullptr) {

6

if(--index == 0) break;

7

cur = cur->next;

8

}

9

//cur points to either the last element

10

//or (index-1)st element

11

Node* p = new Node;

12

p->value = val;

13

p->next= cur->next;

14

cur->next = p;

15

}

16

}

Umut Oztok CS32 - Week 2

slide-33
SLIDE 33

Deletion

How to delete the first node containing 5? From: head 1 5 3 Nil To: head 1 3 Nil Steps: (Let N denote the node we want to delete.)

Umut Oztok CS32 - Week 2

slide-34
SLIDE 34

Deletion

How to delete the first node containing 5? From: head 1 5 3 Nil To: head 1 3 Nil Steps: (Let N denote the node we want to delete.) Find the node just before N. Have its next pointer point to the node just after N. Delete N. Watch out for special cases.

List is empty. List contains a single node. List doesn’t have a node containing the given value.

Umut Oztok CS32 - Week 2

slide-35
SLIDE 35

Deletion

1

void delete(int val) {

Umut Oztok CS32 - Week 2

slide-36
SLIDE 36

Deletion

1

void delete(int val) {

2

if(head == nullptr) return;

Umut Oztok CS32 - Week 2

slide-37
SLIDE 37

Deletion

1

void delete(int val) {

2

if(head == nullptr) return;

3

if(head->value == val) {

4

Node* p = head;

5

head = p->next;

6

delete p;

7

}

8

else {

Umut Oztok CS32 - Week 2

slide-38
SLIDE 38

Deletion

1

void delete(int val) {

2

if(head == nullptr) return;

3

if(head->value == val) {

4

Node* p = head;

5

head = p->next;

6

delete p;

7

}

8

else {

9

Node* cur = head;

10

while(cur->next != nullptr) {

11

if(cur->next->value == val) break;

12

cur = cur->next;

13

}

Umut Oztok CS32 - Week 2

slide-39
SLIDE 39

Deletion

1

void delete(int val) {

2

if(head == nullptr) return;

3

if(head->value == val) {

4

Node* p = head;

5

head = p->next;

6

delete p;

7

}

8

else {

9

Node* cur = head;

10

while(cur->next != nullptr) {

11

if(cur->next->value == val) break;

12

cur = cur->next;

13

}

14

if(cur->next == nullptr) return;

15

Node* p = cur->next;

16

cur->next = p->next;

17

delete p;

18

}}

Umut Oztok CS32 - Week 2

slide-40
SLIDE 40

Exercise 1

Append one linked list to another. Given two linked lists: head 1 5 3 Nil head2 8 4 Nil Append the second one to the first one: head 1 5 3 8 4 Nil

Umut Oztok CS32 - Week 2

slide-41
SLIDE 41

Exercise 1

Append one linked list to another. Given two linked lists: head 1 5 3 Nil head2 8 4 Nil Append the second one to the first one: head 1 5 3 8 4 Nil Any volunteers? void append(Node* head2) { ... }

Umut Oztok CS32 - Week 2

slide-42
SLIDE 42

Exercise 1

void append(Node* head2) {

Umut Oztok CS32 - Week 2

slide-43
SLIDE 43

Exercise 1

void append(Node* head2) { if(head == nullptr) head = head2;

Umut Oztok CS32 - Week 2

slide-44
SLIDE 44

Exercise 1

void append(Node* head2) { if(head == nullptr) head = head2; Node* cur = head;

Umut Oztok CS32 - Week 2

slide-45
SLIDE 45

Exercise 1

void append(Node* head2) { if(head == nullptr) head = head2; Node* cur = head; while(cur->next != nullptr) cur = cur->next;

Umut Oztok CS32 - Week 2

slide-46
SLIDE 46

Exercise 1

void append(Node* head2) { if(head == nullptr) head = head2; Node* cur = head; while(cur->next != nullptr) cur = cur->next; //cur pointing to the last node of the first list cur->next = head2; }

Umut Oztok CS32 - Week 2

slide-47
SLIDE 47

Exercise 2

Reverse a linked list. From: head 1 5 3 Nil To: head 3 5 1 Nil

Umut Oztok CS32 - Week 2

slide-48
SLIDE 48

Exercise 2

Reverse a linked list. From: head 1 5 3 Nil To: head 3 5 1 Nil Any volunteers? void reverse() { ... }

Umut Oztok CS32 - Week 2

slide-49
SLIDE 49

Exercise 2

void reverse() {

Umut Oztok CS32 - Week 2

slide-50
SLIDE 50

Exercise 2

void reverse() { if (head == nullptr) return;

Umut Oztok CS32 - Week 2

slide-51
SLIDE 51

Exercise 2

void reverse() { if (head == nullptr) return; if (head->next == nullptr) return;

Umut Oztok CS32 - Week 2

slide-52
SLIDE 52

Exercise 2

void reverse() { if (head == nullptr) return; if (head->next == nullptr) return; Node* p = head; Node* q = p->next;

Umut Oztok CS32 - Week 2

slide-53
SLIDE 53

Exercise 2

void reverse() { if (head == nullptr) return; if (head->next == nullptr) return; Node* p = head; Node* q = p->next; p->next = NULL;

Umut Oztok CS32 - Week 2

slide-54
SLIDE 54

Exercise 2

void reverse() { if (head == nullptr) return; if (head->next == nullptr) return; Node* p = head; Node* q = p->next; p->next = NULL; while (q != NULL) { Node* r = q->next; q->next = p; p = q; q = r; }

Umut Oztok CS32 - Week 2

slide-55
SLIDE 55

Exercise 2

void reverse() { if (head == nullptr) return; if (head->next == nullptr) return; Node* p = head; Node* q = p->next; p->next = NULL; while (q != NULL) { Node* r = q->next; q->next = p; p = q; q = r; } head = p; }

Umut Oztok CS32 - Week 2

slide-56
SLIDE 56

Check List

Things to be careful about while playing with linked lists: Nullity of a pointer before deref’ing it. Empty linked lists. Linked lists with one element.

Umut Oztok CS32 - Week 2

slide-57
SLIDE 57

Tail Pointer

To insert an element at the end of a list, you need to go over each node, which may be quite inefficient. If you always keep a pointer pointing to the last element, inserting an element at the end can be done immediately. That’s what a "tail pointer" is. head 6 1 5 3 Nil tail Note that you have to maintain tail pointer in most functions.

Umut Oztok CS32 - Week 2

slide-58
SLIDE 58

Doubly Linked Lists

Linked lists are asymmetric in the sense that you can traverse the list in only one direction. Doubly linked lists allow you to traverse in both directions. Basically, every node keeps track of the elements coming just before and after itself. As you have to deal with more pointers, it is harder to maintain.

Umut Oztok CS32 - Week 2

slide-59
SLIDE 59

Dummy node

A method to simplify your code. Create a dummy node when you initialize a linked list. Keep the dummy node until destructing the linked list. So, the head pointer always points to the dummy node. No special case checking for empty linked lists. Be careful, the first actual element is head->next.

Umut Oztok CS32 - Week 2

slide-60
SLIDE 60

Linked List Properties

Dynamic allocated memory - flexible.

Comes with a cost?

Umut Oztok CS32 - Week 2

slide-61
SLIDE 61

Linked List Properties

Dynamic allocated memory - flexible.

Comes with a cost? Memory of pointers.

Umut Oztok CS32 - Week 2

slide-62
SLIDE 62

Linked List Properties

Dynamic allocated memory - flexible.

Comes with a cost? Memory of pointers.

Fast insertion (to the front or in the middle).

Umut Oztok CS32 - Week 2

slide-63
SLIDE 63

Linked List Properties

Dynamic allocated memory - flexible.

Comes with a cost? Memory of pointers.

Fast insertion (to the front or in the middle).

Also to the end with tail pointers.

Umut Oztok CS32 - Week 2

slide-64
SLIDE 64

Linked List Properties

Dynamic allocated memory - flexible.

Comes with a cost? Memory of pointers.

Fast insertion (to the front or in the middle).

Also to the end with tail pointers.

Fast deletion (no need to shift elements compared to array).

Umut Oztok CS32 - Week 2

slide-65
SLIDE 65

Linked List Properties

Dynamic allocated memory - flexible.

Comes with a cost? Memory of pointers.

Fast insertion (to the front or in the middle).

Also to the end with tail pointers.

Fast deletion (no need to shift elements compared to array). Search is almost the same as array.

Umut Oztok CS32 - Week 2

slide-66
SLIDE 66

Linked List Properties

Dynamic allocated memory - flexible.

Comes with a cost? Memory of pointers.

Fast insertion (to the front or in the middle).

Also to the end with tail pointers.

Fast deletion (no need to shift elements compared to array). Search is almost the same as array. Random access is the weakest point of linked lists.

Umut Oztok CS32 - Week 2

slide-67
SLIDE 67

Slides

Slides will be available at http://www.cs.ucla.edu/~umut/cs32

Umut Oztok CS32 - Week 2