linked list 1. introduction to linked list 2. node class 3. linked list 4. the bag class with linked...

59
Linked List 1. 1. Introduction to Linked List Introduction to Linked List 2. 2. Node Class Node Class 3. 3. Linked List Linked List 4. 4. The Bag Class with Linked The Bag Class with Linked List List

Upload: jalyn-prall

Post on 30-Mar-2015

271 views

Category:

Documents


8 download

TRANSCRIPT

Page 1: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

Linked List

1.1. Introduction to Linked ListIntroduction to Linked List

2.2. Node ClassNode Class

3.3. Linked ListLinked List

4.4. The Bag Class with Linked ListThe Bag Class with Linked List

Page 2: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

Linked List

Linked ListLinked List Is a series of connected nodes, where each Is a series of connected nodes, where each

node is a data structure with data field and node is a data structure with data field and pointer(s) field.pointer(s) field.

Advantages over array implementationAdvantages over array implementation Can grow and shrink in size, with no upper limitCan grow and shrink in size, with no upper limit Fast insertion and deletionFast insertion and deletion

Ωhead

Page 3: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

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;};

DeclarationsDeclarations forfor LinkedLinked ListsLists

Page 4: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

typedef Keyword

typedef int age_t;typedef int age_t;typedef int count_t;typedef int count_t;typedef double gpa_t;typedef double gpa_t;……age_t myAge = 21;age_t myAge = 21;count_t numElements = 5;count_t numElements = 5;gpa_t myGPA = 3.8;gpa_t myGPA = 3.8;

void someFunc(age_t age, count_t count);void someFunc(age_t age, count_t count);

is clearer than is clearer than

void someFunct(int age, int count);void someFunct(int age, int count);

Page 5: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

Why Use typedef?

Using typedefs can make your code clearer

Using ypedefs can make your code easier to modify

Page 6: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

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;};

DeclarationsDeclarations forfor LinkedLinked ListsLists

Page 7: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

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;};

DeclarationsDeclarations forfor LinkedLinked ListsLists

Page 8: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

Your Turn

Write the public interface for the Node Write the public interface for the Node class—e.g, Constructors, accessors, class—e.g, Constructors, accessors, modifiers.modifiers.

Page 9: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

Solution

class node{public: //CONSTRUCTORS node(); node(const valueType& value, node* ptr); //CONSTANT FUNCTIONS valueType getData(); node* getNext(); //MODIFIER FUNCTIONS void setData(valueType newData); void setLink(node* newLink);

private: valueType data; node* next;};

Page 10: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

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 11: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

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 null

Page 12: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

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, We want to add a new entry, 13, to the 13, to the frontfront of the linked of the linked list shown here.list shown here.

10

15

7

nullhead_ptr

entry

13

Page 13: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

Inserting a Node at the Front

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

10

15

77

nullhead_ptrentry

13

insert_ptr

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

Page 14: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

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 15: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

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 16: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

Inserting a Node at the Front

10

15

7

nullhead_ptr

entry

13

insert_ptr

13

insert_ptr = new node;

Place the data in the new node's data_field.

Connect the new node to the front of the list.

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

Page 17: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

Inserting a Node at the Front

10

15

7

nullhead_ptr

entry

13

insert_ptr13

insert_ptr = new node(entry, head_ptr);

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

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

Page 18: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

Inserting a Node at the Front

10

15

7

nullhead_ptr

entry

13

insert_ptr

13

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 19: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

Inserting a Node at the Front

10

15

7

nullhead_ptr

entry

13

insert_ptr

13

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

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

Page 20: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

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 21: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked 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;

}

Inserting a Node at the Front

Page 22: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

Inserting a Node at the Front

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;

} Does the function work

correctly for the empty list ?

Page 23: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

head_ptrentry

13 null

Inserting a Node at the Front

Does the function work

correctly 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 24: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

Inserting a Node at the Front

head_ptrentry

13 null

insert_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 25: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

Inserting a Node at the Front

head_ptrentry

13

insert_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 26: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

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 function

returns, the linked list has one node.

Page 27: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

Caution!

Always make sure that your linked list functions work correctly with an empty list.

Page 28: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

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 29: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

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);

Page 30: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

Pseudocode for Inserting Nodes

Determine whether the new node will be the first node Determine whether the new node will be the first node in the linked list. If so, then there is only one step:in the linked list. If so, then there is only one step:

The

func

tion

we

alre

ady

wro

te

list_head_insert(head_ptr, entry);

Page 31: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

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 32: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

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 33: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

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 34: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

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 35: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

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

What is the name of this orange pointer ?

Look at the pointerwhich is in the node

*previous_ptr

previous_ptr

Page 36: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

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

This pointer is calledprevious_ptr->link_field(although this name maybe private to the node)

What is the name of What is the name of this orange pointer ?this orange pointer ?

previous_ptr

Page 37: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

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 linkedlist, with 10 and 7

previous_ptr

Page 38: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

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.

The new node mustbe inserted at thefront of this small

linked list.

13

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

previous_ptr

Page 39: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

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

What might cause this statement to fail to compile?

previous_ptr

list_head_insert(previous_ptr->link_field, entry);

Page 40: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

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

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

previous_ptr

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

Page 41: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

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 42: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

Pseudocode for Inserting Nodes

The process of adding a new node in the The process of adding a new node in the middle of a list can also be incorporated as a middle of a list can also be incorporated as a separate function. This function is called separate function. This function is called list_insert in the linked list toolkit of Section list_insert in the linked list toolkit of Section 5.2.5.2.

Page 43: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

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 44: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

Removing the Head Node

10 15 7

nullhead_ptr

13

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

remove_ptr

Page 45: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

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 46: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

Removing the Head Node

10 15 7

nullhead_ptr

13

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

remove_ptr

Page 47: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

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 48: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

Removing the Head Node

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

10 15 7

nullhead_ptr

Page 49: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

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.

Summary

Page 50: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

Doubly Linked List

head Ω

class node{ . . .private: elementType data; node* next; node* previous;};

class list{ . . .private: node* head; int count;};

Ω

Page 51: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

insertAtFront()

head ΩΩ

newPtr ΩΩ

head Ω

newPtrΩ

Page 52: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

insertAtFront()

void list::insertAtFront(valueType item){ node* temp = new node(item, NULL, NULL); if (isEmpty()) head = temp; else { temp->setNext(head); head->setPrevious(temp); head = temp; } count++;}

Page 53: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

removeFromFront()

head ΩΩ

head Ω

temp

ΩΩ

Page 54: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

removeFromFront()

valueType list::removeFromFront(){ valueType result = NIL;

if (!isEmpty()) { result = head->getData();

node* temp = head; head->setPrevious(NULL); head = head->getNext(); delete temp; count--; } return result;}

Page 55: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

insertAtBack()

head ΩΩ

newPtr ΩΩ

headΩ

newPtr Ω

temp

temp

Page 56: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

insertAtBack()void list::insertAtBack(valueType item){ node* newPtr = new node(item, NULL, NULL);

// find the last node if (isEmpty()) head = newPtr; else { node* temp = head; while (temp->getNext() != NULL) { temp = temp->getNext(); } temp->setNext(newPtr); newPtr->setPrevious(temp); } count++;}

Page 57: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

removeFromBack()

head ΩΩ

head Ω

temp

ΩΩ

Page 58: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

removeFromBack()

valueType list::removeFromBack(){ valueType result = NIL;

if (!isEmpty()) { // find last node node* temp = head; while (temp->getNext() != NULL) { temp = temp->getNext(); } // remove last node temp->getPrevious()->setNext(NULL); delete temp; } return result;}

Page 59: Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List

Tail Pointer (to the last node)

head Ω

class node{ . . .private: elementType data; node* next;};

class list{ . . .private: node* head; node* tail; int count;};

tail