40
Implementing Linked Lists Lecture 17 Sections 18.1 - 18.3 Robb T. Koether Hampden-Sydney College Wed, Feb 21, 2018 Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 1 / 36

Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

Implementing Linked ListsLecture 17

Sections 18.1 - 18.3

Robb T. Koether

Hampden-Sydney College

Wed, Feb 21, 2018

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 1 / 36

Page 2: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

1 Modifying a Linked ListTest PreconditionsCreate New NodesLocate “Action” PointDraw “Before” PictureDraw “After” PictureModify PointersArrange Statements in OrderConsolidate the CasesCombine CasesDistinguish the CasesDelete Old NodesMiscellaneous

2 Assignment

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 2 / 36

Page 3: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

Outline

1 Modifying a Linked ListTest PreconditionsCreate New NodesLocate “Action” PointDraw “Before” PictureDraw “After” PictureModify PointersArrange Statements in OrderConsolidate the CasesCombine CasesDistinguish the CasesDelete Old NodesMiscellaneous

2 Assignment

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 3 / 36

Page 4: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

Modifying a Linked List

The insert() Prototypevoid insert(int pos, const T& value);

The method outlined here offers a reliable strategy for modifying alinked list.As we go through the method, we will apply it to the problem ofinserting a new element into a linked list.

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 4 / 36

Page 5: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

Outline

1 Modifying a Linked ListTest PreconditionsCreate New NodesLocate “Action” PointDraw “Before” PictureDraw “After” PictureModify PointersArrange Statements in OrderConsolidate the CasesCombine CasesDistinguish the CasesDelete Old NodesMiscellaneous

2 Assignment

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 5 / 36

Page 6: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

The Method of Modifying a Linked List

Step 1assert(pos >= 0 && pos <= m_size);

Test any necessary pre-conditions.

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 6 / 36

Page 7: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

Outline

1 Modifying a Linked ListTest PreconditionsCreate New NodesLocate “Action” PointDraw “Before” PictureDraw “After” PictureModify PointersArrange Statements in OrderConsolidate the CasesCombine CasesDistinguish the CasesDelete Old NodesMiscellaneous

2 Assignment

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 7 / 36

Page 8: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

The Method of Modifying a Linked List

Step 2LinkedListNode<T>* new_node

= new LinkedListNode<T>(value);

Create any additional nodes and pointers that are needed for the task.

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 8 / 36

Page 9: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

Outline

1 Modifying a Linked ListTest PreconditionsCreate New NodesLocate “Action” PointDraw “Before” PictureDraw “After” PictureModify PointersArrange Statements in OrderConsolidate the CasesCombine CasesDistinguish the CasesDelete Old NodesMiscellaneous

2 Assignment

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 9 / 36

Page 10: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

The Method

Step 3LinkedListNode<T>* succ = head;LinkedListNode<T>* pred = NULL;for (int i = 0; i < pos; i++){

pred = succ;succ = succ->m_next;

}

Use pointers to locate the position(s) in the list where the change willtake place.

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 10 / 36

Page 11: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

The Method

Now divide the task of modifying the list into distinct cases.Begin with the most general case.Work down to the least general case.(1) Insert into the middle of a non-empty list.(2) Insert at the head of a non-empty list.(3) Insert at the tail of a non-empty list.(4) Insert into an empty list.

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 11 / 36

Page 12: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

Outline

1 Modifying a Linked ListTest PreconditionsCreate New NodesLocate “Action” PointDraw “Before” PictureDraw “After” PictureModify PointersArrange Statements in OrderConsolidate the CasesCombine CasesDistinguish the CasesDelete Old NodesMiscellaneous

2 Assignment

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 12 / 36

Page 13: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

The Method

Step 4

succpred

Draw a picture of the structure before the modifications take place.

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 13 / 36

Page 14: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

The Method

Step 4

new_node

succpred

Show any newly created nodes.

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 13 / 36

Page 15: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

The Method

Step 4

new_node

new_node->next

pred->next

succpred

Label each relevant pointer.

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 13 / 36

Page 16: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

Outline

1 Modifying a Linked ListTest PreconditionsCreate New NodesLocate “Action” PointDraw “Before” PictureDraw “After” PictureModify PointersArrange Statements in OrderConsolidate the CasesCombine CasesDistinguish the CasesDelete Old NodesMiscellaneous

2 Assignment

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 14 / 36

Page 17: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

The Method

Step 5

new_node

new_node->next

pred->next

succpred

Draw a picture of the structure after the modifications take place.

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 15 / 36

Page 18: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

The Method

Step 5

new_node

new_node->next

pred->next

succpred

Draw a picture of the structure after the modifications take place.

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 15 / 36

Page 19: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

The Method

Step 5

new_node

new_node->next

pred->next

succpred

Draw a picture of the structure after the modifications take place.

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 15 / 36

Page 20: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

Outline

1 Modifying a Linked ListTest PreconditionsCreate New NodesLocate “Action” PointDraw “Before” PictureDraw “After” PictureModify PointersArrange Statements in OrderConsolidate the CasesCombine CasesDistinguish the CasesDelete Old NodesMiscellaneous

2 Assignment

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 16 / 36

Page 21: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

The Method

Step 6pred->m_next = new_node;new_node->m_next = succ;

For the pointers which were modified, write the assignment statementsthat will modify them.

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 17 / 36

Page 22: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

Outline

1 Modifying a Linked ListTest PreconditionsCreate New NodesLocate “Action” PointDraw “Before” PictureDraw “After” PictureModify PointersArrange Statements in OrderConsolidate the CasesCombine CasesDistinguish the CasesDelete Old NodesMiscellaneous

2 Assignment

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 18 / 36

Page 23: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

Arrange the Statements in Order

Step 7pred->m_next = new_node;new_node->m_next = succ;

Arrange the statements it correct order.

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 19 / 36

Page 24: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

The Other Cases

Now apply Steps 4 - 7 to the other three cases:Insertion at the head.Insertion at the tail.Insertion into an empty list (head and tail).

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 20 / 36

Page 25: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

The Method

Step 7// Case 1

pred->m_next = new_node;new_node->m_next = succ;

// Case 2head = new_node;new_node->m_next = succ;

// Case 3pred->m_next = new_node;new_node->m_next = NULL;

// Case 4head = new_node;new_node->m_next = NULL;

Arrange the assignment statements in the correct order.

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 21 / 36

Page 26: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

Outline

1 Modifying a Linked ListTest PreconditionsCreate New NodesLocate “Action” PointDraw “Before” PictureDraw “After” PictureModify PointersArrange Statements in OrderConsolidate the CasesCombine CasesDistinguish the CasesDelete Old NodesMiscellaneous

2 Assignment

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 22 / 36

Page 27: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

The Method

Step 8Replace

new_node->m_next = NULL;

with

new_node->m_next = succ;

Then the line is common to all four cases.

Consolidate the cases.Determine what code is common to all cases.Write the common code either before or after dividing into cases,as appropriate.

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 23 / 36

Page 28: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

Outline

1 Modifying a Linked ListTest PreconditionsCreate New NodesLocate “Action” PointDraw “Before” PictureDraw “After” PictureModify PointersArrange Statements in OrderConsolidate the CasesCombine CasesDistinguish the CasesDelete Old NodesMiscellaneous

2 Assignment

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 24 / 36

Page 29: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

The Method

Step 9Cases 1 and 3 are identical.Cases 2 and 4 are identical.

Combine cases that use the same code into a single case.

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 25 / 36

Page 30: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

Outline

1 Modifying a Linked ListTest PreconditionsCreate New NodesLocate “Action” PointDraw “Before” PictureDraw “After” PictureModify PointersArrange Statements in OrderConsolidate the CasesCombine CasesDistinguish the CasesDelete Old NodesMiscellaneous

2 Assignment

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 26 / 36

Page 31: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

The Method

Step 10Based on the values of the pointers,

In cases 1 and 3, pred != NULL.In cases 2 and 4, pred == NULL.

Based on the values of indices,In cases 1 and 3, pos > 0.In cases 2 and 4, pos == 0.

Distinguish the cases.Find conditions that are unique to each case.Write the if statements and the code to handle the separatecases.

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 27 / 36

Page 32: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

The Method

Step 10if (pred == NULL)

head = new_node;else

pred->m_next = new_node;

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 28 / 36

Page 33: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

Outline

1 Modifying a Linked ListTest PreconditionsCreate New NodesLocate “Action” PointDraw “Before” PictureDraw “After” PictureModify PointersArrange Statements in OrderConsolidate the CasesCombine CasesDistinguish the CasesDelete Old NodesMiscellaneous

2 Assignment

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 29 / 36

Page 34: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

The Method

Step 11In this example, there are no nodes to be deleted.

Delete any old nodes.

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 30 / 36

Page 35: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

Outline

1 Modifying a Linked ListTest PreconditionsCreate New NodesLocate “Action” PointDraw “Before” PictureDraw “After” PictureModify PointersArrange Statements in OrderConsolidate the CasesCombine CasesDistinguish the CasesDelete Old NodesMiscellaneous

2 Assignment

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 31 / 36

Page 36: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

The Method

Step 12m_size++;

Write any other statements necessary to complete the task.

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 32 / 36

Page 37: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

The Method

The insert() Functiontemplate <class T>void LinkedList<T>::insert(int pos, const T& value){// Test validity of parameters

assert(pos >= 0 && pos <= m_size);// Create a new node

LinkedListNode<T>* new_node = new LinkedListNode<T>(value);// Locate insertion point

LinkedListNode<T>* succ = head;LinkedListNode<T>* pred = NULL;for (int i = 0; i < pos; i++){

pred = succ;succ = succ->m_next;

}// Modify pointers to insert new node

new_node->m_next = succ;if (pred == NULL)

head = new_node;else

pred->m_next = new_node;// Update the size

m_size++;return;

}

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 33 / 36

Page 38: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

The LinkedList Class

The LinkedList Classlinkedlistnode.h.linkedlist.h.List Test.cpp.

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 34 / 36

Page 39: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

Outline

1 Modifying a Linked ListTest PreconditionsCreate New NodesLocate “Action” PointDraw “Before” PictureDraw “After” PictureModify PointersArrange Statements in OrderConsolidate the CasesCombine CasesDistinguish the CasesDelete Old NodesMiscellaneous

2 Assignment

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 35 / 36

Page 40: Implementing Linked Lists - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · Modifying a Linked List The insert() Prototype void insert(int pos, const

Assignment

AssignmentRead Sections 18.1 - 18.3.

Robb T. Koether (Hampden-Sydney College) Implementing Linked Lists Wed, Feb 21, 2018 36 / 36