• No results found

Linked Lists in Action

N/A
N/A
Protected

Academic year: 2021

Share "Linked Lists in Action"

Copied!
48
0
0

Loading.... (view fulltext now)

Full text

(1)

1

Chapter 5 introduces the often-

used data structure of linked lists.

This presentation shows how to

implement the most common

operations on linked lists.

Linked Lists in Action

(2)

2

For this presentation, each node in the

linked list is a struct, as shown here.

data lin k

10

data lin k

15

data lin k

7

n

u

ll

struct Node

{

typedef int Item;

Item data;

Node *link;

};

(3)

3

data lin

k

7

The data portion of each node is a type

called

Item

, defined by a typedef.

li n k

n

u

ll

struct Node

{

typedef int Item;

Item data;

Node *link;

};

data lin

k

15

Declarations for Linked Lists

data

(4)

4

Each Node also contains a link field

which is a pointer to another Node.

data

15

data

7

struct Node

{

typedef int Item;

Item data;

Node *link;

};

Declarations for Linked Lists

(5)

5

Declarations for Linked Lists

A program can keep track of the front

node by using a pointer variable such

as

head_ptr

in this example.

Notice that head_ptr is not a Node --

it is a pointer

to a Node.

(6)

6

Declarations for Linked Lists

A program can keep track of the front

node by using a pointer variable such

as

head_ptr

.

Notice that head_ptr is not a Node --

it is a pointer to a Node.

We represent the empty list by storing

null

in the Head pointer.

head_ptr

n

u

(7)

7

void list_head_insert(Node*& head_ptr, const Node::Item& entry);

Inserting a Node at the Front

We want to add a new entry, 13,

to the

front

of the linked list

(8)

8

Inserting a Node at the Front

Create a new node, pointed to

by a local variable

insert_ptr

.

1 0 1 5 7 null head_ptr entry 1 3 insert_ptr

(9)

9

Inserting a Node at the Front

insert_ptr = new Node;

1 0 1 5 7 null head_ptr entry 1 3 insert_ptr

(10)

10

Inserting a Node at the Front

insert_ptr = new Node;

Place the data in the new

node's data field.

1 0 1 5 7 null head_ptr entry 1 3 insert_ptr 1 3

(11)

11

Inserting a Node at the Front

insert_ptr = new Node;

insert_ptr->data = entry;

1 0 1 5 7 null head_ptr entry 1 3 insert_ptr 1 3

(12)

12

Inserting a Node at the Front

insert_ptr = new Node;

insert_ptr->data = entry;

1 0 1 5 7 null head_ptr entry 1 3 insert_ptr 1 3

(13)

13

Inserting a Node at the Front

1 0 1 5 7 null head_ptr entry 1 3 insert_ptr 1 3

insert_ptr = new Node;

insert_ptr->data = entry;

Connect the new node to the

front of the list.

(14)

14

Inserting a Node at the Front

1 0 1 5 7 null head_ptr entry 1 3 insert_ptr 1 3

insert_ptr = new Node;

insert_ptr->data = entry;

insert_ptr->link = head_ptr;

(15)

15

Inserting a Node at the Front

1 0 1 5 7 null head_ptr entry 1 3 insert_ptr 1 3

insert_ptr = new Node;

insert_ptr->data = entry;

insert_ptr->link = head_ptr;

(16)

16

Inserting a Node at the Front

1 0 1 5 7 null head_ptr entry 1 3 insert_ptr 1 3

insert_ptr = new Node;

insert_ptr->data = entry;

insert_ptr->link = head_ptr;

Make the head_ptr point to the

new head of the linked list.

(17)

17

Inserting a Node at the Front

1 0 1 5 7 null head_ptr entry 1 3 insert_ptr 1 3

insert_ptr = new Node;

insert_ptr->data = entry;

insert_ptr->link = head_ptr;

head_ptr = insert_ptr;

(18)

18

Inserting a Node at the Front

insert_ptr = new Node;

insert_ptr->data = entry;

insert_ptr->link = head_ptr;

head_ptr = insert_ptr;

1 0 1 5 7 null head_ptr 1 3

When the function returns, the

linked list has a new node at the

front, containing 13.

(19)

19

void list_head_insert(Node*& head_ptr, const Node::Item& entry) { Node *insert_ptr;

insert_ptr = new Node;

insert_ptr->data = entry; insert_ptr->link = head_ptr; head_ptr = insert_ptr; }

(20)

20

void list_head_insert(Node*& head_ptr, const Node::Item& entry) { Node *insert_ptr;

insert_ptr = new Node;

insert_ptr->data = entry; insert_ptr->link = head_ptr; head_ptr = insert_ptr; }

(21)

21

void list_head_insert(Node*& head_ptr, const Node::Item& entry) { Node *insert_ptr;

insert_ptr = new Node;

insert_ptr->data = entry; insert_ptr->link = head_ptr; head_ptr = insert_ptr; }

Inserting a Node at the Front

head_ptr

entry

1

3

(22)

22

void list_head_insert(Node*& head_ptr, const Node::Item& entry) { Node *insert_ptr;

insert_ptr = new Node;

insert_ptr->data = entry; insert_ptr->link = head_ptr; head_ptr = insert_ptr; }

Inserting a Node at the Front

(23)

23

void list_head_insert(Node*& head_ptr, const Node::Item& entry) { Node *insert_ptr;

insert_ptr = new Node;

insert_ptr->data = entry;

insert_ptr->link = head_ptr;

head_ptr = insert_ptr; }

Inserting a Node at the Front

(24)

24

void list_head_insert(Node*& head_ptr, const Node::Item& entry) { Node *insert_ptr;

insert_ptr = new Node;

insert_ptr->data = entry; insert_ptr->link = head_ptr;

head_ptr = insert_ptr;

}

Inserting a Node at the Front

(25)

25

void list_head_insert(Node*& head_ptr, const Node::Item& entry) { Node *insert_ptr;

insert_ptr = new Node;

insert_ptr->data = entry; insert_ptr->link = head_ptr; head_ptr = insert_ptr; }

Inserting a Node at the Front

head_ptr

1

3

null

When the function

returns, the linked list

(26)

26

C

au

ti

on

!

Always make sure that

your linked list

functions work

correctly with an

empty list.

(27)

27

Pseudocode for Inserting Nodes

Nodes are often inserted at places other than the

front of a linked list.

(28)

28

Pseudocode for Inserting Nodes

(29)

29

Pseudocode for Inserting Nodes

Determine whether the new node will be the first node in

the linked list. If so, then there is only one step:

The function we already wrote

head_insert

(30)

30

Pseudocode for Inserting Nodes

Determine whether the new node will be the first node in

the linked list. If so, then there is only one step:

head_insert(

head_ptr

, entry);

(31)

31

Pseudocode for Inserting Nodes

Determine whether the new node will be the first node in

the linked list. If so, then there is only one step:

head_insert(head_ptr,

entry

);

(32)

32

Pseudocode for Inserting Nodes

Otherwise (if the new node will not be first):

Start by setting a pointer named

previous_ptr

to point to the

node which is just

before

(33)

33

Pseudocode for Inserting Nodes

1 5 1 0 7 null head_ptr

Otherwise (if the new node will not be first):

Start by setting a pointer named

previous_ptr

to point to the

node which is just

before

the new node's position.

In this example, the

new node will be

the second node

(34)

34

Pseudocode for Inserting Nodes

1 5 1 0 7 null head_ptr

Otherwise (if the new node will not be first):

Start by setting a pointer named

previous_ptr

to point to the

node which is just before the new node's position

Look at the pointer

which is

in the node

previous_ptr^

(35)

35

Pseudocode for Inserting Nodes

1 5 1 0 7 null head_ptr

Otherwise (if the new node will not be first):

Start by setting a pointer named

previous_ptr

to point to the

node which is just before the new node's position

This pointer is called

previous_ptr->link

(36)

36

Pseudocode for Inserting Nodes

1 5 1 0 7 null head_ptr

Otherwise (if the new node will not be first):

Start by setting a pointer named

previous_ptr

to point to the

node which is just before the new node's position

previous_ptr->link

points to the head

of a small linked

list, with 10 and 7

(37)

37

Pseudocode for Inserting Nodes

1 5 1 0 7 null head_ptr

Otherwise (if the new node will not be first):

Start by setting a pointer named

previous_ptr

to point to the

node which is just before the new node's position.

The new node must

be inserted at the

front of this small

linked list.

1

3

(38)

38

Pseudocode for Inserting Nodes

1 5 1 0 7 null head_ptr

Otherwise (if the new node will not be first):

Start by setting a pointer named

previous_ptr

to point to the

node which is just before the new node's position.

1

3

previous_ptr

(39)

39

Pseudocode for Inserting Nodes

Determine whether the new node will be the first node in

the linked list. If so, then there is only one step:

head_insert(head_ptr, entry);

Otherwise (if the new node will not be first):

Set a pointer named

previous_ptr

to point to the node

which is just before the new node's position.

(40)

40

Pseudocode for Inserting Nodes

(41)

41

Pseudocode for Removing Nodes

Nodes often need to be removed from a linked list.

As with insertion, there is a technique for removing

a node from the front of a list, and a technique for

removing a node from elsewhere.

(42)

42

Removing the Head Node

1 0 1 5 7 null head_ptr 1 3

Start by setting up a temporary pointer named

remove_ptr

to the head node.

(43)

43

Removing the Head Node

(44)

44

Removing the Head Node

(45)

45

Removing the Head Node

Set up remove_ptr and BeforePtr . ➋ head_ptr = remove_ptr->link; ❸

delete remove_ptr; // Return the node's memory to heap.

(46)

46

Removing the Head Node

Here’s what the linked list looks like after the removal finishes.

(47)

47

It is easy to insert a node at the front of a list.

The linked list toolkit also provides a function for

inserting a new node elsewhere

It is easy to remove a node at the front of a list.

The linked list toolkit also provides a function for

removing a node elsewhere--you should read

about this function and the other functions of the

too

lk

it

.

(48)

48

T

HE

E

ND

Presentation copyright 1997, Addison Wesley Longman, For use with

Data Structures and Other Objects Using C++

by Michael Main and Walter Savitch. Some artwork in the presentation is used with permission from Presentation Task Force (copyright New Vision Technologies Inc) and Corel Gallery Clipart Catalog (copyright Corel Corporation, 3G Graphics Inc, Archive Arts, Cartesia Software, Image Club Graphics Inc, One Mile Up Inc, TechPool Studios, Totem Graphics Inc). Students and instructors who use

Data Structures and Other Objects Using C++

are welcome

to use this presentation however they see fit, so long as this copyright notice remains inta

c

References

Related documents

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

Comparing the performance of life insurance funds in countries which impose caps on risky asset class investments with those which do not, we find evidence of a large

A consequence of this perspective of woman directors being appointed as part of the symbolic management of the independence of the board is that, when these directors lose

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

A doubly linked list is like a linked list, but each node also has a previous field, which points to the nodes predecessor..

The specified sequence according to a node after filling data field contains the list is inside the last data to find a linked list without.. Notice that we should always check

Further, by showing that v τ is a modular unit over Z we give a new proof of the fact that the singular values of v τ are units at all imaginary quadratic arguments and obtain

Using a very stable DMM as the transfer standard, Sandia hopes to be able to characterize its own calibrator against one of the NIST reference calibrators, which are fully