• No results found

Linked List Operations If you could be any animal, what would you be?

N/A
N/A
Protected

Academic year: 2021

Share "Linked List Operations If you could be any animal, what would you be?"

Copied!
187
0
0

Loading.... (view fulltext now)

Full text

(1)

Linked List Operations

If you could be any animal,

(2)

vectors + grids stacks + queues sets + maps

Programming

arrays

dynamic memory management

linked data structures

algorithmic analysis testing

recursive problem-solving

Roadmap

Life after

CS106B!

C++ basics

Diagnostic

real-world algorithms

Core Tools

User/client Implementation

(3)

vectors + grids stacks + queues sets + maps

Programming

Roadmap

C++ basics

real-world User/client

arrays

dynamic memory management

linked data structures

Implementation

(4)

Today’s

question examine and manipulate

the structure of linked lists?

(5)

Today’s

topics

1. Review

2. Linked List Traversal

(6)

Review

[intro to linked lists]

(7)

How is our data organized?

What stores our data? (arrays, linked lists)

Structures

Data Organization Strategies

Fundamental C++ Data Storage

Pointers

move us

(8)

Levels of abstraction

How is our data organized?

What stores our data? (arrays, linked lists)

How is data represented electronically? (RAM)

Structures

Data Organization Strategies

Fundamental C++ Data Storage

Computer Hardware

These are

built on top

of pointers!

(9)

How is our data organized?

What stores our data? (arrays, linked lists)

Structures

Data Organization Strategies

Fundamental C++ Data Storage

(10)

A linked list is a chain of nodes, used to store a sequence of data.

Each node contains two pieces of information:

○ Some piece of data that is stored in the sequence

○ A link to the next node in the list

● We can traverse the list by starting at the first node and repeatedly following its link.

● The end of the list is marked with some special indicator.

(11)

Data Link

0xfca0b000

Data Link

Data Link

PT R

(12)

struct Node {

string data;

Node* next;

}

(13)

0xfca0b000

list

Node*

"someData" PT

R

(14)

0xfca0b000

list

Node*

Node* list = new Node("someData", nullptr);

The Node struct also has a

conveniently defined

constructor that allows us to

accomplish this in one line.

"someData" PT

R

(15)

Traversal

○ How do we walk through all elements in the linked list?

Rewiring

○ How do we rearrange the elements in a linked list?

Insertion

(16)

● A linked list can be the fundamental data storage backing for an ADT in much the same the same way an array can.

● We saw that linked lists function great as a way of implementing a stack!

● Three operations:

push() – List insertion and list rewiring

pop() – List deletion and list rewiring

Destructor – List traversal and list deletion

(17)

● Linked lists are chains of Node structs, which are connected by pointers.

Since the memory is not contiguous, they allow for fast rewiring between nodes (without moving all the other Nodes like an array might).

● Common traversal strategy

While loop with a pointer that starts at the front of your list

Inside the while loop, reassign the pointer to the next node

(18)

Linked List Operations

Revisited

(19)

How can we write code to

examine and manipulate the

(20)

● On Monday, we saw linked lists in the context of classes, where we used a linked list as the data storage underlying an implementation of a Stack.

(21)

● On Monday, we saw linked lists in the context of classes, where we used a linked list as the data storage underlying an implementation of a Stack.

● However, linked lists are not limited only to use within classes. In fact, the next assignment will ask you to implement "standalone" linked list functions that operate on provided linked lists, outside the context of a class.

(22)

● On Monday, we saw linked lists in the context of classes, where we used a linked list as the data storage underlying an implementation of a Stack.

● However, linked lists are not limited only to use within classes. In fact, the next assignment will ask you to implement "standalone" linked list functions that operate on provided linked lists, outside the context of a class.

● This is the paradigm that we will work under for the next two days. In doing so, we'll gain a little more flexibility to get practice with many different linked list operations and build our linked list toolbox!

(23)

Linked List Traversal

(24)

Printing a Linked List

(25)

● Being able to "see" the contents of a linked list is a really helpful debugging tool!

(26)

● Being able to "see" the contents of a linked list is a really helpful debugging tool!

● There are two main ways to do so: using the debugger and printing to the console

(27)

● Being able to "see" the contents of a linked list is a really helpful debugging tool!

● There are two main ways to do so: using the debugger and printing to the console

(28)

● Being able to "see" the contents of a linked list is a really helpful debugging tool!

● There are two main ways to do so: using the debugger and printing to the console

● First attempt: What is the result of the following code? (Poll) /* Creates a list with contents "Hello" -> "World" -> nullptr */ Node* list = createList();

cout << list << endl; Answer: Some memory address is

printed! We can't predict the exact value.

(29)

● Being able to "see" the contents of a linked list is a really helpful debugging tool!

● There are two main ways to do so: using the debugger and printing to the console

(30)

printList()

Let's code it!

(31)
(32)

Node* list = readList(); printList(list);

/* other list things happen... */ }

(33)

Node* list = readList(); printList(list);

/* other list things happen... */ }

(34)

0xab40

list

Node*

"Kylie" "Trip" PT

R

"Nick"

Node* list = readList(); printList(list);

/* other list things happen... */ }

(35)

Node* list = readList(); printList(list);

/* other list things happen... */ }

0xab40

list

Node*

(36)

Node* list = readList(); printList(list);

/* other list things happen... */ }

0xab40

list

Node*

"Kylie" "Trip" PT

R

"Nick"

void printList(Node* list) { while (list != nullptr) {

cout << list->data << endl; list = list->next;

} }

list

Node*

0xab40

(37)

Node* list = readList(); printList(list);

/* other list things happen... */ }

0xab40

list

Node*

void printList(Node* list) { while (list != nullptr) {

cout << list->data << endl; list = list->next;

} }

Node*

0xab40

(38)

Node* list = readList(); printList(list);

/* other list things happen... */ }

0xab40

list

Node*

"Kylie" "Trip" PT

R

"Nick"

void printList(Node* list) { while (list != nullptr) {

cout << list->data << endl; list = list->next;

} }

list

Node*

0xab40

(39)

Node* list = readList(); printList(list);

/* other list things happen... */ }

0xab40

list

Node*

void printList(Node* list) { while (list != nullptr) {

cout << list->data << endl; list = list->next;

} }

Node*

0xab40

Nick

(40)

Node* list = readList(); printList(list);

/* other list things happen... */ }

0xab40

list

Node*

"Kylie" "Trip" PT

R

"Nick"

void printList(Node* list) { while (list != nullptr) {

cout << list->data << endl; list = list->next;

} }

list

Node*

0xab40

Nick

(41)

Node* list = readList(); printList(list);

/* other list things happen... */ }

0xab40

list

Node*

void printList(Node* list) { while (list != nullptr) {

cout << list->data << endl; list = list->next;

} }

Node*

0xbc70

Nick

(42)

Node* list = readList(); printList(list);

/* other list things happen... */ }

0xab40

list

Node*

"Kylie" "Trip" PT

R

"Nick"

void printList(Node* list) { while (list != nullptr) {

cout << list->data << endl; list = list->next;

} }

list

Node*

0xbc70

Nick

(43)

Node* list = readList(); printList(list);

/* other list things happen... */ }

0xab40

list

Node*

void printList(Node* list) { while (list != nullptr) {

cout << list->data << endl; list = list->next;

} }

Node*

0xbc70

Nick

(44)

Node* list = readList(); printList(list);

/* other list things happen... */ }

0xab40

list

Node*

"Kylie" "Trip" PT

R

"Nick"

void printList(Node* list) { while (list != nullptr) {

cout << list->data << endl; list = list->next;

} }

list

Node*

0xbc70

Nick

Kylie

(45)

Node* list = readList(); printList(list);

/* other list things happen... */ }

0xab40

list

Node*

void printList(Node* list) { while (list != nullptr) {

cout << list->data << endl; list = list->next;

} }

Node*

0xbc70

Nick

Kylie

(46)

Node* list = readList(); printList(list);

/* other list things happen... */ }

0xab40

list

Node*

"Kylie" "Trip" PT

R

"Nick"

void printList(Node* list) { while (list != nullptr) {

cout << list->data << endl; list = list->next;

} }

list

Node*

0x40f0

Nick

Kylie

(47)

Node* list = readList(); printList(list);

/* other list things happen... */ }

0xab40

list

Node*

void printList(Node* list) { while (list != nullptr) {

cout << list->data << endl; list = list->next;

} }

Node*

0x40f0

Nick

Kylie

(48)

Node* list = readList(); printList(list);

/* other list things happen... */ }

0xab40

list

Node*

"Kylie" "Trip" PT

R

"Nick"

void printList(Node* list) { while (list != nullptr) {

cout << list->data << endl; list = list->next;

} }

list

Node*

0x40f0

Nick

Kylie

(49)

Node* list = readList(); printList(list);

/* other list things happen... */ }

0xab40

list

Node*

void printList(Node* list) { while (list != nullptr) {

cout << list->data << endl; list = list->next;

} }

Node*

0x40f0

Nick

Kylie

Trip

(50)

Node* list = readList(); printList(list);

/* other list things happen... */ }

0xab40

list

Node*

"Kylie" "Trip" PT

R

"Nick"

void printList(Node* list) { while (list != nullptr) {

cout << list->data << endl; list = list->next;

} }

list

Node*

0x40f0

Nick

Kylie

Trip

(51)

Node* list = readList(); printList(list);

/* other list things happen... */ }

0xab40

list

Node*

void printList(Node* list) { while (list != nullptr) {

cout << list->data << endl; list = list->next;

} }

Node*

nullptr

Nick

Kylie

Trip

(52)

Node* list = readList(); printList(list);

/* other list things happen... */ }

0xab40

list

Node*

"Kylie" "Trip" PT

R

"Nick"

void printList(Node* list) { while (list != nullptr) {

cout << list->data << endl; list = list->next;

} }

list

Node*

nullptr

Nick

Kylie

Trip

(53)

Node* list = readList(); printList(list);

/* other list things happen... */ }

0xab40

list

Node*

Nick

Kylie

Trip

(54)

Node* list = readList(); printList(list);

/* other list things happen... */ }

0xab40

list

Node*

"Kylie" "Trip" PT

R

"Nick"

Nick

Kylie

Trip

(55)

Measuring a Linked

List

(56)

● Similar to arrays, a linked list does not have the capability to automatically report back its own "size."

● The following code is NOT valid, since list is simply a pointer Node* list = readList();

cout << list.size() << endl; // WRONG! BAD!

● Let's write a function that allows us to calculate the number of nodes in a linked list!

(57)

lengthOf()

Let's code it!

(58)

Freeing a Linked List

(59)

● Linked lists are built out of many different nodes, each of which have been dynamically allocated. This means that when we're done using a list, it is always good practice to free the memory associated with all the nodes!

(60)

● Linked lists are built out of many different nodes, each of which have been dynamically allocated. This means that when we're done using a list, it is always good practice to free the memory associated with all the nodes!

● Freeing all the nodes requires traversing the list while safely freeing everything along the way.

(61)

● Linked lists are built out of many different nodes, each of which have been dynamically allocated. This means that when we're done using a list, it is always good practice to free the memory associated with all the nodes!

● Freeing all the nodes requires traversing the list while safely freeing everything along the way.

(62)

● Linked lists are built out of many different nodes, each of which have been dynamically allocated. This means that when we're done using a list, it is always good practice to free the memory associated with all the nodes!

● Freeing all the nodes requires traversing the list while safely freeing everything along the way.

● We've actually seen how to do this already! The IntStack destructor that we coded up together was responsible for cleaning up all the list memory.

● Let's revisit how to (and how not to) accomplish this task!

(63)

Freeing Linked Lists,

the Wrong Way

(64)

/* WRONG WRONG WRONG WRONG WRONG */

while (list != nullptr) { delete list;

list = list->next; }

} 0xab40

list

Node*

"Kylie" "Trip" PT

R

"Nick"

(65)

/* WRONG WRONG WRONG WRONG WRONG */

while (list != nullptr) { delete list;

list = list->next; }

} 0xab40

list

Node*

(66)

/* WRONG WRONG WRONG WRONG WRONG */

while (list != nullptr) { delete list;

list = list->next; }

} 0xab40

list

Node*

"Kylie" "Trip" PT

R

"Nick"

(67)

/* WRONG WRONG WRONG WRONG WRONG */

while (list != nullptr) { delete list;

list = list->next; }

} 0xab40

list

Node*

delete

(68)

/* WRONG WRONG WRONG WRONG WRONG */

while (list != nullptr) { delete list;

list = list->next; }

} 0xab40

list

Node*

"Kylie" "Trip" PT

R

(69)

/* WRONG WRONG WRONG WRONG WRONG */

while (list != nullptr) { delete list;

list = list->next; }

} 0xab40

list

Node*

(70)

/* WRONG WRONG WRONG WRONG WRONG */

while (list != nullptr) { delete list;

list = list->next; }

} 0xab40

list

Node*

"Kylie" "Trip" PT

R

(71)

/* WRONG WRONG WRONG WRONG WRONG */

while (list != nullptr) { delete list;

list = list->next; }

} 0xab40

list

Node*

Undefined

Behavior!

(72)

Freeing Linked Lists,

the Right Way

(73)

while (list != nullptr) {

delete list;

list = list->next; }

} 0xab40

list

Node*

(74)

while (list != nullptr) {

Node* next = list->next; delete list;

list = list->next; }

} 0xab40

list

Node*

"Kylie" "Trip" PT

R

"Nick"

(75)

while (list != nullptr) {

Node* next = list->next; delete list;

list = next; }

} 0xab40

list

Node*

(76)

while (list != nullptr) {

Node* next = list->next; delete list;

list = next; }

} 0xab40

list

Node*

"Kylie" "Trip" PT

R

"Nick"

(77)

while (list != nullptr) {

Node* next = list->next; delete list;

list = next; }

} 0xab40

list

Node*

(78)

while (list != nullptr) {

Node* next = list->next; delete list;

list = next; }

} 0xab40

list

Node*

"Kylie" "Trip" PT

R

"Nick"

(79)

while (list != nullptr) {

Node* next = list->next; delete list;

list = next; }

} 0xab40

list

Node*

next

0xbc70

Node*

(80)

while (list != nullptr) {

Node* next = list->next; delete list;

list = next; }

} 0xab40

list

Node*

"Kylie" "Trip" PT

R

"Nick"

next

0xbc70

Node*

(81)

while (list != nullptr) {

Node* next = list->next; delete list;

list = next; }

} 0xab40

list

Node*

next

0xbc70

Node*

(82)

while (list != nullptr) {

Node* next = list->next; delete list;

list = next; }

} 0xab40

list

Node*

"Kylie" "Trip" PT

R

next

0xbc70

Node*

(83)

while (list != nullptr) {

Node* next = list->next; delete list;

list = next; }

} 0xbc70

list

Node*

next

0xbc70

Node*

(84)

while (list != nullptr) {

Node* next = list->next; delete list;

list = next; }

} 0xbc70

list

Node*

"Kylie" "Trip" PT

R

next

0xbc70

Node*

(85)

while (list != nullptr) {

Node* next = list->next; delete list;

list = next; }

} 0xbc70

list

Node*

(86)

while (list != nullptr) {

Node* next = list->next; delete list;

list = next; }

} 0xbc70

list

Node*

"Kylie" "Trip" PT

R

(87)

while (list != nullptr) {

Node* next = list->next; delete list;

list = next; }

} 0xbc70

list

Node* Node*

(88)

while (list != nullptr) {

Node* next = list->next; delete list;

list = next; }

} 0xbc70

list

Node*

"Kylie" "Trip" PT

R

next

Node*

0x40f0

(89)

while (list != nullptr) {

Node* next = list->next; delete list;

list = next; }

} 0xbc70

list

Node*

next

Node*

0x40f0

(90)

while (list != nullptr) {

Node* next = list->next; delete list;

list = next; }

} 0xbc70

list

Node*

"Trip" PT

R

next

Node*

0x40f0

(91)

while (list != nullptr) {

Node* next = list->next; delete list;

list = next; }

} 0xbc70

list

Node*

next

Node*

0x40f0

(92)

while (list != nullptr) {

Node* next = list->next; delete list;

list = next; }

} 0x40f0

list

Node*

"Trip" PT

R

next

Node*

0x40f0

(93)

while (list != nullptr) {

Node* next = list->next; delete list;

list = next; }

} 0x40f0

list

Node* Node*

0x40f0

(94)

while (list != nullptr) {

Node* next = list->next; delete list;

list = next; }

} 0x40f0

list

Node*

"Trip" PT

R

Node*

0x40f0

(95)

while (list != nullptr) {

Node* next = list->next; delete list;

list = next; }

} 0x40f0

list

Node*

next

Node*

nullptr

(96)

while (list != nullptr) {

Node* next = list->next; delete list;

list = next; }

} 0x40f0

list

Node*

"Trip" PT

R

next

Node*

nullptr

(97)

while (list != nullptr) {

Node* next = list->next; delete list;

list = next; }

} 0x40f0

list

Node*

next

Node*

nullptr

(98)

while (list != nullptr) {

Node* next = list->next; delete list;

list = next; }

} 0x40f0

list

Node*

PT R

next

Node*

nullptr

(99)

while (list != nullptr) {

Node* next = list->next; delete list;

list = next; }

} nullptr

list

Node*

next

Node*

nullptr

(100)

while (list != nullptr) {

Node* next = list->next; delete list;

list = next; }

} nullptr

list

Node*

PT R

Node*

nullptr

(101)

All memory

freed!

(102)

Linked Lists and

Recursion

(103)

● On Monday, we mentioned that the Node struct that defined the contents of a linked list was define recursively.

(104)

● On Monday, we mentioned that the Node struct that defined the contents of a linked list was define recursively.

struct Node {

string data;

Node* next;

}

(105)

● On Monday, we mentioned that the Node struct that defined the contents of a linked list was define recursively.

struct Node {

string data;

Node* next;

(106)

Schwarz

(107)
(108)

Schwarz

(109)
(110)

void printList(Node* list) { while (list != nullptr) {

cout << list->data << endl; list = list->next;

} }

(111)

void printList(Node* list) { while (list != nullptr) {

cout << list->data << endl; list = list->next;

void printListRec(Node* list) {

/* Base Case: There's nothing to print if the list is empty. */

if (list == nullptr) return;

/* Recursive Case: Print the first node,

(112)

● Recursion can be a really elegant way to write code for a list traversal! However, recursion is not always the optimal problem-solving strategy…

(113)

● Recursion can be a really elegant way to write code for a list traversal! However, recursion is not always the optimal problem-solving strategy…

● Note that the recursive solution generates one recursive call for every

element in the list, meaning that a list with n elements would require n stack frames.

(114)

● Recursion can be a really elegant way to write code for a list traversal! However, recursion is not always the optimal problem-solving strategy…

● Note that the recursive solution generates one recursive call for every

element in the list, meaning that a list with n elements would require n stack frames.

● What is the stack frame limit on most computers?

You explored this on assignment 3 – for most computers it is somewhere in the range of 16-64K

(115)

Recursion can be a really elegant way to write code for a list traversal! However, recursion is not always the optimal problem-solving strategy…

Note that the recursive solution generates one recursive call for every element in the list, meaning that a list with n elements would require n stack frames.

(116)

Recursion can be a really elegant way to write code for a list traversal! However, recursion is not always the optimal problem-solving strategy…

Note that the recursive solution generates one recursive call for every element in the list, meaning that a list with n elements would require n stack frames.

What is the stack frame limit on most computers?

You explored this on assignment 3 – for most computers it is somewhere in the range of 16-64K

With a recursive strategy, the size of the list we're able to process is limited by the stack frame capacity – we can't process lists longer than 16-64K elements!

Takeaway: Any linked list

operations involving traversal of the

whole list are better done

iteratively! This holds especially

true on the assignment – don't try to

implement any of the list helper

functions recursively!

(117)

● Temporary pointers into lists are very helpful!

When processing linked lists iteratively, it’s common to introduce pointers that point to cells in multiple spots in the list.

This is particularly useful if we’re destroying or rewiring existing lists.

Using a while loop with a condition that checks to see if the current pointer is

(118)

Announcements

(119)

Assignment 4 is due tonight at 11:59pm PDT. The submission link is now open on Paperless.

Assignment 5 will be released tomorrow morning and will be due on Tuesday, August 2 at 11:59pm PDT.

(120)

Linked List Insertion

(121)

Insertion at the front

(prepend)

(122)

● Suppose we wanted to write a function to insert an element at the front of a linked list.

(123)

● Suppose we wanted to write a function to insert an element at the front of a linked list.

(124)

● Suppose we wanted to write a function to insert an element at the front of a linked list.

0xab40

list

Node*

"Kylie" "Trip" PT

R

"Nick"

"Julie"

(125)

● Suppose we wanted to write a function to insert an element at the front of a linked list.

(126)

● Suppose we wanted to write a function to insert an element at the front of a linked list.

This is similar to the push() function we implemented on Thursday, but now we're writing a standalone function to do this on an arbitrary list. Let's code it!

0x26b0

list

Node*

"Kylie" "Trip" PT

R

"Nick"

"Julie"

(127)

prependTo()

Let's code it!

(128)

What went wrong?

(129)

Node* list = nullptr; prependTo(list, "Trip"); prependTo(list, "Kylie"); prependTo(list, "Nick"); return 0;

}

(130)

Node* list = nullptr; prependTo(list, "Trip"); prependTo(list, "Kylie"); prependTo(list, "Nick"); return 0;

}

(131)

Node* list = nullptr; prependTo(list, "Trip"); prependTo(list, "Kylie"); prependTo(list, "Nick"); return 0;

}

nullptr

list

Node*

(132)

Node* list = nullptr; prependTo(list, "Trip"); prependTo(list, "Kylie"); prependTo(list, "Nick"); return 0;

}

nullptr

list

Node*

PT R

(133)

Node* list = nullptr; prependTo(list, "Trip"); prependTo(list, "Kylie"); prependTo(list, "Nick"); return 0;

}

nullptr

list

Node*

void prependTo(Node* list, string data) { Node* newNode = new Node;

newNode->data = data; newNode->next = list; list = newNode;

}

nullptr

Node* "Trip"

string

(134)

Node* list = nullptr; prependTo(list, "Trip"); prependTo(list, "Kylie"); prependTo(list, "Nick"); return 0;

}

nullptr

list

Node*

PT R

void prependTo(Node* list, string data) { Node* newNode = new Node;

newNode->data = data; newNode->next = list; list = newNode;

}

nullptr

list

Node* "Trip"

data

string

(135)

Node* list = nullptr; prependTo(list, "Trip"); prependTo(list, "Kylie"); prependTo(list, "Nick"); return 0;

}

nullptr

list

Node*

void prependTo(Node* list, string data) { Node* newNode = new Node;

newNode->data = data; newNode->next = list; list = newNode;

}

nullptr

Node* "Trip"

string

0x40f0

Node*

(136)

Node* list = nullptr; prependTo(list, "Trip"); prependTo(list, "Kylie"); prependTo(list, "Nick"); return 0;

}

nullptr

list

Node*

PT R

void prependTo(Node* list, string data) { Node* newNode = new Node;

newNode->data = data; newNode->next = list; list = newNode;

}

nullptr

list

Node* "Trip"

data

string

0x40f0

newNode

Node*

(137)

Node* list = nullptr; prependTo(list, "Trip"); prependTo(list, "Kylie"); prependTo(list, "Nick"); return 0;

}

nullptr

list

Node*

void prependTo(Node* list, string data) { Node* newNode = new Node;

newNode->data = data; newNode->next = list; list = newNode;

}

nullptr

Node* "Trip"

string

0x40f0

Node*

(138)

Node* list = nullptr; prependTo(list, "Trip"); prependTo(list, "Kylie"); prependTo(list, "Nick"); return 0;

}

nullptr

list

Node*

PT R

void prependTo(Node* list, string data) { Node* newNode = new Node;

newNode->data = data; newNode->next = list; list = newNode;

}

nullptr

list

Node* "Trip"

data

string

0x40f0

newNode

Node*

"Trip"

(139)

Node* list = nullptr; prependTo(list, "Trip"); prependTo(list, "Kylie"); prependTo(list, "Nick"); return 0;

}

nullptr

list

Node*

void prependTo(Node* list, string data) { Node* newNode = new Node;

newNode->data = data; newNode->next = list; list = newNode;

}

nullptr

Node* "Trip"

string

0x40f0

Node*

(140)

Node* list = nullptr; prependTo(list, "Trip"); prependTo(list, "Kylie"); prependTo(list, "Nick"); return 0;

}

nullptr

list

Node*

PT R

void prependTo(Node* list, string data) { Node* newNode = new Node;

newNode->data = data; newNode->next = list; list = newNode;

}

nullptr

list

Node* "Trip"

data

string

0x40f0

newNode

Node*

"Trip"

(141)

Node* list = nullptr; prependTo(list, "Trip"); prependTo(list, "Kylie"); prependTo(list, "Nick"); return 0;

}

nullptr

list

Node*

void prependTo(Node* list, string data) { Node* newNode = new Node;

newNode->data = data; newNode->next = list; list = newNode;

}

0x40f0

Node* "Trip"

string

0x40f0

Node*

(142)

Node* list = nullptr; prependTo(list, "Trip"); prependTo(list, "Kylie"); prependTo(list, "Nick"); return 0;

}

nullptr

list

Node*

PT R

"Trip"

(143)

Node* list = nullptr; prependTo(list, "Trip"); prependTo(list, "Kylie"); prependTo(list, "Nick"); return 0;

}

nullptr

list

Node*

yeeted into the land of leaked memory...

(144)

● Unless specified otherwise, function arguments in C++ are passed by value – this includes pointers!

● A function that takes a pointer as an argument gets a copy of the pointer.

● We can change where the copy points, but not where the original pointer points.

(145)

Pointers by Reference

(146)

● To solve our earlier problem, we can pass the linked list pointer by reference.

(147)

● To solve our earlier problem, we can pass the linked list pointer by reference.

● Our new function:

void prependTo(Node*& list, string data) { Node* newNode = new Node;

(148)

● To solve our earlier problem, we can pass the linked list pointer by reference.

● Our new function:

void prependTo(Node*& list, string data) { Node* newNode = new Node;

newNode->data = data; newNode->next = list; list = newNode;

}

(149)

● To solve our earlier problem, we can pass the linked list pointer by reference.

● Our new function:

void prependTo(Node*& list, string data) { Node* newNode = new Node;

This is a reference to a

(150)

Node* list = nullptr; prependTo(list, "Trip"); prependTo(list, "Kylie"); prependTo(list, "Nick"); return 0;

}

(151)

Node* list = nullptr; prependTo(list, "Trip"); prependTo(list, "Kylie"); prependTo(list, "Nick"); return 0;

}

(152)

Node* list = nullptr; prependTo(list, "Trip"); prependTo(list, "Kylie"); prependTo(list, "Nick"); return 0;

}

nullptr

Node*

PT R

list

(153)

Node* list = nullptr; prependTo(list, "Trip"); prependTo(list, "Kylie"); prependTo(list, "Nick"); return 0;

}

nullptr

Node*

list

(154)

Node* list = nullptr; prependTo(list, "Trip"); prependTo(list, "Kylie"); prependTo(list, "Nick"); return 0;

}

nullptr

list

Node*

PT R

void prependTo(Node*& list, string data) { Node* newNode = new Node;

newNode->data = data; newNode->next = list; list = newNode;

}

"Trip" data

string

(155)

Node* list = nullptr; prependTo(list, "Trip"); prependTo(list, "Kylie"); prependTo(list, "Nick"); return 0;

}

nullptr

Node*

void prependTo(Node*& list, string data) { Node* newNode = new Node;

newNode->data = data; newNode->next = list; list = newNode;

}

nullptr

Node* "Trip"

string

list

(156)

Node* list = nullptr; prependTo(list, "Trip"); prependTo(list, "Kylie"); prependTo(list, "Nick"); return 0;

}

nullptr

Node*

PT R

void prependTo(Node*& list, string data) { Node* newNode = new Node;

newNode->data = data; newNode->next = list; list = newNode;

}

nullptr

Node* "Trip"

data

string

0x40f0

newNode

Node*

list

References

Related documents

Such a collegiate cul- ture, like honors cultures everywhere, is best achieved by open and trusting relationships of the students with each other and the instructor, discussions

Neosporno je da su graditelji hramova promišljeno odabrali najbolje lokacije te orijentaciju hrama, ali ono što neki autori, s pravom, zamjećuju, jest problem

Teenagers reported mixed experiences of church engagement with climate change: 63% had seen church leaders taking practical steps to help the congregation respond to climate

[r]

Globally distributed software development Project Cloud Source code Bug tracking Requirements Management Reporting Testing ….?. IDE IDE SVN Email

It is fundamental to recognise that the media needs to make a greater contribution to the advancement of women’s rights (Matos, 2017), and in this absence of

Including tokyo city as osaka guide looks great side trip to find the local and from.. Clearly demonstrates the time and transfer to set up