data structure & algorithms

58
DATA STRUCTURE & ALGORITHMS CHAPTER 2: LINKED LIST

Upload: jania

Post on 11-Feb-2016

57 views

Category:

Documents


0 download

DESCRIPTION

DATA STRUCTURE & ALGORITHMS. CHAPTER 2: LINKED LIST. What is linked list?. A method of organizing stored data in a computers’ memory or on storage medium based on the logical order of the data - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: DATA STRUCTURE & ALGORITHMS

DATA STRUCTURE & ALGORITHMS

CHAPTER 2: LINKED LIST

Page 2: DATA STRUCTURE & ALGORITHMS

What is linked list?• A method of organizing stored data in a

computers’ memory or on storage medium based on the logical order of the data

• All stored data records are assigned a physical address in memory that that the computer uses to locate the information

• A linked list arranges the data by logic rather than by physical address

Page 3: DATA STRUCTURE & ALGORITHMS

What is linked list?• Linked list consists of a sequence of nodes,

each containing arbitrary data fields and one or two references ("links") pointing to the next and/or previous nodes.

Data fields Links

Node

Page 4: DATA STRUCTURE & ALGORITHMS

What is linked list?• The last node has a reference to null• The entry point into a linked list is called the

head of the list

Page 5: DATA STRUCTURE & ALGORITHMS

What is linked list?Address of

recordID Name Phone No. Next Name

0000 1111 Aziz 0199001234 5500

5500 3333 Johan 012444555 6000

6000 4444 Suraya 013909090 8200

8200 5555 Malik 0178000011 eof

Table 1

Page 6: DATA STRUCTURE & ALGORITHMS

What is linked list?• In the Table 1, each data record is assigned a

memory address• The first field holds the physical memory

address of the record and the last field holds the physical memory address of the next logical record

• The list is said linked because each record is linked to the next based on the last field

Page 7: DATA STRUCTURE & ALGORITHMS

What is linked list?• What happen if new record is added?

Assumed with a numerical ID of “2222”.• When this record added to the list, the list

change reflect the new linking logic, based on numerical ID

• The “Next Name” field changes for ID 1111 to accommodate the added record with ID 2222 as in Table 2

Page 8: DATA STRUCTURE & ALGORITHMS

What is linked list?Address of

recordID Name Phone No. Next Name

0000 1111 Aziz 0199001234 9672

9672 2222 Jenab 0157771111 5500

5500 3333 Johan 012444555 6000

6000 4444 Suraya 013909090 8200

8200 5555 Malik 0178000011 eof

Table 2

Page 9: DATA STRUCTURE & ALGORITHMS

What is linked list?• In the Table 3, the same data is organized

numerically by the ID number• The linked list still connects each record to

the next using the “Next Name” field.

Page 10: DATA STRUCTURE & ALGORITHMS

What is linked list?Address of

recordID Name Phone No. Next Name

0000 1111 Aziz 0199001234 9672

9672 2222 Jenab 0157771111 5500

5500 3333 Johan 012444555 8200

8200 5555 Malik 0178000011 6000

6000 4444 Suraya 013909090 eof

Table 3

Page 11: DATA STRUCTURE & ALGORITHMS

Linked List vs ArrayAdvantages Disadvantages

Linked list The number of nodes can grow and shrink on demand

Not allowed direct access to the individual elementsUses more memory to store a reference to the next node

Array Simple coding Cannot be easily extended or reduced to fit the data set

Less memory Expensive to maintain new insertions & deletion

Page 12: DATA STRUCTURE & ALGORITHMS

Types of linked list?• Singly linked list• Doubly linked list• Circularly linked list

Page 13: DATA STRUCTURE & ALGORITHMS

Singly linked list• The simplest kind of linked list, which has one link per

node. • This link points to the next node in the list, or to a null

value or empty list if it is the final node.• Divided into two parts: the first part holds or points to

information about the node, and second part holds the address of next node.

• Travels one way.

Page 14: DATA STRUCTURE & ALGORITHMS

Doubly linked list• Two-way linked list. • Each node has two links: one points to the previous

node, or points to a null value or empty list if it is the first node; and one points to the next, or points to a null value or empty list if it is the final node.

Page 15: DATA STRUCTURE & ALGORITHMS

Circularly linked list• The first and final nodes are linked together. • To traverse a circular linked list, you begin at any node and follow the

list in either direction until you return to the original node. • Circularly-linked lists can be seen as having no beginning or end. • This type of list is most useful for managing buffers for data ingest,

and in cases where you have one object in a list and wish to iterate through all other objects in the list in no particular order.

• The pointer pointing to the whole list may be called the access pointer.

Page 16: DATA STRUCTURE & ALGORITHMS

Declaration of linked list

Page 17: DATA STRUCTURE & ALGORITHMS

Declarations for Linked Lists• For this presentation, nodes in a linked list are objects,

as shown here.

data_field

link_field

10

data_field

link_field

15

data_field

link_field

7

null

class node{public: typedef double value_type; ...private value_type data_field; node *link_field;};

Page 18: DATA STRUCTURE & ALGORITHMS

Declarations for Linked Lists• The data_field of each node is a type called value_type,

defined by a typedef.

data_field

link_field

10

data_field

link_field

15

data_field

link_field

7

null

class node{public: typedef int value_type; ...private value_type data_field; node *link_field;};

Page 19: DATA STRUCTURE & ALGORITHMS

Declarations for Linked Lists• Each node also contains a link_field which is a

pointer to another node.

data_field

link_field

10

data_field

link_field

15

data_field

link_field

7

null

class node{public: typedef int value_type; ...private value_type data_field; node *link_field;};

Page 20: DATA STRUCTURE & ALGORITHMS

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.

head_ptr

data_field

link_field

10

data_field

link_field

15

data_field

link_field

7

null

Page 21: DATA STRUCTURE & ALGORITHMS

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_ptrnull

Page 22: DATA STRUCTURE & ALGORITHMS

void list_head_insert(node*& head_ptr, const node::value_type& entry);

Inserting a Node at the Front

We want to add a new entry, 13, to the front of the linked list shown here.

10

15

7

nullhead_ptr

entry

13

Page 23: DATA STRUCTURE & ALGORITHMS

Inserting a Node at the Front

• Create a new node, pointed to by a local variable insert_ptr.

10

15

7

nullhead_ptr

entry

13

insert_ptr

void list_head_insert(node*& head_ptr, const node::value_type& entry);

Page 24: DATA STRUCTURE & ALGORITHMS

Inserting a Node at the Front

insert_ptr = new node;

10

15

7

nullhead_ptr

entry

13

insert_ptr

void list_head_insert(node*& head_ptr, const node::value_type& entry);

Page 25: DATA STRUCTURE & ALGORITHMS

Inserting a Node at the Front

10

15

7

nullhead_ptr

entry

13

insert_ptr13

• insert_ptr = new node;• Place the data in the new node's

data_field.

void list_head_insert(node*& head_ptr, const node::value_type& entry);

Page 26: DATA STRUCTURE & ALGORITHMS

Inserting a Node at the Front

10

15

7

nullhead_ptr

entry

13

insert_ptr13

void list_head_insert(node*& head_ptr, const node::value_type& entry);

insert_ptr = new node;Place the data in the new node's data_field.Connect the new node to the front of the list.

Page 27: DATA STRUCTURE & ALGORITHMS

Inserting a Node at the Front

The correct new node can be completely created in one step by calling an appropriate node constructor.

10

15

7

nullhead_ptr

entry

13

insert_ptr13

insert_ptr = new node(entry, head_ptr);

void list_head_insert(node*& head_ptr, const node::value_type& entry);

Page 28: DATA STRUCTURE & ALGORITHMS

Inserting a Node at the Front

10

15

7

nullhead_ptr

entry

13

insert_ptr13

insert_ptr = new node(entry, head_ptr);Make the old head pointer point to the new node.

void list_head_insert(node*& head_ptr, const node::value_type& entry);

Page 29: DATA STRUCTURE & ALGORITHMS

Inserting a Node at the Front

10

15

7null

head_ptrentry13

insert_ptr13

insert_ptr = new node(entry, head_ptr);head_ptr = insert_ptr;

void list_head_insert(node*& head_ptr, const node::value_type& entry);

Page 30: DATA STRUCTURE & ALGORITHMS

Inserting a Node at the Front

insert_ptr = new node(entry, head_ptr);head_ptr = insert_ptr;

10

15

7

nullhead_ptr

13

When the function returns, thelinked list has a new node at thefront.

void list_head_insert(node*& head_ptr, const node::value_type& entry);

Page 31: DATA STRUCTURE & ALGORITHMS

void list_head_insert(node*& head_ptr, const node::value_type& entry){ node *insert_ptr;

insert_ptr = new node(entry, head_ptr); head_ptr = insert_ptr;}

Inserting a Node at the Front

Page 32: DATA STRUCTURE & ALGORITHMS

Inserting a Node at the Frontvoid list_head_insert(node*& head_ptr, const node::value_type& entry){ node *insert_ptr;

insert_ptr = new node(entry, head_ptr); head_ptr = insert_ptr;} Does the function work

correctly for the empty list ?

Page 33: DATA STRUCTURE & ALGORITHMS

head_ptrentry

13 null

Inserting a Node at the Front

Does the function workcorrectly for the empty

list ?

void list_head_insert(node*& head_ptr, const node::value_type& entry){ node *insert_ptr;

insert_ptr = new node(entry, head_ptr); head_ptr = insert_ptr;}

Page 34: DATA STRUCTURE & ALGORITHMS

Inserting a Node at the Front

head_ptrentry

13 nullinsert_ptr

13

void list_head_insert(node*& head_ptr, const node::value_type& entry){ node *insert_ptr;

insert_ptr = new node(entry, head_ptr); head_ptr = insert_ptr;}

null

Page 35: DATA STRUCTURE & ALGORITHMS

Inserting a Node at the Front

head_ptrentry

13insert_ptr

13

null

void list_head_insert(node*& head_ptr, const node::value_type& entry){ node *insert_ptr;

insert_ptr = new node(entry, head_ptr); head_ptr = insert_ptr;}

Page 36: DATA STRUCTURE & ALGORITHMS

Inserting a Node at the Front

head_ptr

13

null

void list_head_insert(node*& head_ptr, const node::value_type& entry){ node *insert_ptr;

insert_ptr = new node(entry, head_ptr); head_ptr = insert_ptr;}

When the functionreturns, the linked list

has one node.

Page 37: DATA STRUCTURE & ALGORITHMS

Pseudocode for Inserting Nodes• Nodes are often inserted at places other than

the front of a linked list.• There is a general pseudocode that you can

follow for any insertion function. . .

Page 38: DATA STRUCTURE & ALGORITHMS

Pseudocode for Inserting NodesDetermine whether the new node will be the first node inthe linked list. If so, then there is only one step:

list_head_insert(head_ptr, entry);

Page 39: DATA STRUCTURE & ALGORITHMS

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 f

uncti

on

we alre

ady w

rote

list_head_insert(head_ptr, entry);

Page 40: DATA STRUCTURE & ALGORITHMS

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:

list_head_insert(head_ptr, entry);

A pointerto the

head ofthe list

Page 41: DATA STRUCTURE & ALGORITHMS

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:

list_head_insert(head_ptr, entry);

The data to put

in the new node

Page 42: DATA STRUCTURE & ALGORITHMS

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 the new node's position.

Page 43: DATA STRUCTURE & ALGORITHMS

Pseudocode for Inserting Nodes

15

10

7

nullhead_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, thenew node will bethe second node

previous_ptr

Page 44: DATA STRUCTURE & ALGORITHMS

Pseudocode for Inserting Nodes

What is the name of this orange pointer ?

15

10

7

nullhead_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 pointerwhich is in the node

*previous_ptrprevious_ptr

Page 45: DATA STRUCTURE & ALGORITHMS

Pseudocode for Inserting Nodes

What is the name of this orange pointer ?

15

10

7

nullhead_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 calledprevious_ptr->link_field(although this name maybe private to the node)

previous_ptr

Page 46: DATA STRUCTURE & ALGORITHMS

Pseudocode for Inserting Nodes

15

10

7

nullhead_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_fieldpoints to the headof a small linked

list, with 10 and 7

previous_ptr

Page 47: DATA STRUCTURE & ALGORITHMS

Pseudocode for Inserting Nodes

Write one C++ statement which will do the insertion.

15

10

7

nullhead_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 mustbe inserted at thefront of this small

linked list.

13

previous_ptr

Page 48: DATA STRUCTURE & ALGORITHMS

Pseudocode for Inserting Nodes

What might cause this statement to fail to compile?

15

10

7

nullhead_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. 13

previous_ptrlist_head_insert(previous_ptr->link_field, entry);

Page 49: DATA STRUCTURE & ALGORITHMS

Pseudocode for Inserting Nodes

Use a node member function to get the link field if needed.

15

10

7

nullhead_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. 13

previous_ptrlist_head_insert(previous_ptr->link( ), entry);

Page 50: DATA STRUCTURE & ALGORITHMS

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:list_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. Make the function call:

list_head_insert(previous_ptr->link( ), entry);

Page 51: DATA STRUCTURE & ALGORITHMS

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.

• We’ll look at the pseudocode for removing a node from the front of a linked list.

Page 52: DATA STRUCTURE & ALGORITHMS

Removing the Head Node

10 15 7null

head_ptr13

Start by setting up a temporary pointer named remove_ptr to the head node.

remove_ptr

Page 53: DATA STRUCTURE & ALGORITHMS

Removing the Head Node

10 15 7

nullhead_ptr

13

Set up remove_ptr. head_ptr = remove_ptr->link( );

remove_ptr

Draw the change that this statement will make to the linked list.

Page 54: DATA STRUCTURE & ALGORITHMS

Removing the Head Node

10 15 7

nullhead_ptr

13

Set up remove_ptr. head_ptr = remove_ptr->link( );

remove_ptr

Page 55: DATA STRUCTURE & ALGORITHMS

Removing the Head Node Set up remove_ptr. head_ptr = remove_ptr->link( ); delete remove_ptr; // Return the node's memory to heap.

10 15 7

nullhead_ptr

13

remove_ptr

Page 56: DATA STRUCTURE & ALGORITHMS

Removing the Head NodeHere’s what the linked list looks like after the removal finishes.

10 15 7

nullhead_ptr

Page 57: DATA STRUCTURE & ALGORITHMS

Summary• 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 toolkit.

Page 58: DATA STRUCTURE & ALGORITHMS

ReferencesData Structures and Other Objects Using C++by Michael Main and Walter Savitch.