1. Trang chủ
  2. » Giáo án - Bài giảng

Giáo trình C++ - Ngành CNTT - Part 13

80 768 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 80
Dung lượng 1,9 MB

Nội dung

[...]... here->link is the address of the next node  To make here point to the next node, make the assignment: here = here->link; Copyright © 2007 Pearson Education, Inc Publishing as Pearson Addison-Wesley Slide 1 3- 27 A Refinement of search  The search function can be refined in this way: here = head; while(here->data != target && here->link != NULL) { here = here->next; Check for last node } if (here->data... Pearson Addison-Wesley Slide 1 3- 18 Pseudocode for head_insert     Create a new dynamic variable pointed to by temp_ptr Place the data in the new node called *temp_ptr Make temp_ptr's link variable point to the head node Make the head pointer point to temp_ptr Display 13. 3 Copyright © 2007 Pearson Education, Inc Publishing as Pearson Addison-Wesley Slide 1 3- 19 Translating head_insert to C++  The pseudocode... Education, Inc Publishing as Pearson Addison-Wesley Slide 1 3- 30 Iterator Example  Using the previous outline of an iterator we can display the contents of a linked list in this way: NodePtr iter; for (iter = Head; iter != NULL; iter = iter->Link) cout data); Copyright © 2007 Pearson Education, Inc Publishing as Pearson Addison-Wesley Slide 1 3- 31 Inserting a Node Inside a List  To insert... temp_ptr->link = after_me->link; after_me->link = temp_ptr; head 2 2 after_me 3 2 7 2 5 2 Copyright © 2007 Pearson Education, Inc Publishing as Pearson Addison-Wesley 9 0 temp_ptr Slide 1 3- 34 Caution!  The order of pointer assignments is critical  If we changed after_me->link to point to temp_ptr first, we would loose the rest of the list!  The complete insert function is shown in Display 13. 9 Copyright... 2007 Pearson Education, Inc Publishing as Pearson Addison-Wesley Slide 1 3- 11 To Use NULL  A definition of NULL is found in several libraries, including and  A using directive is not needed for NULL Copyright © 2007 Pearson Education, Inc Publishing as Pearson Addison-Wesley Slide 1 3- 12 Linked Lists   The diagram in Display 13. 2 depicts a linked list A linked list is a list of... Display 13. 8 Copyright © 2007 Pearson Education, Inc Publishing as Pearson Addison-Wesley Slide 1 3- 32 Inserting the New Node   Function insert creates the new node just as head_insert did We do not want our new node at the head of the list however, so…  We use the pointer after_me to insert the new node Copyright © 2007 Pearson Education, Inc Publishing as Pearson Addison-Wesley Slide 1 3- 33 Inserting... NULL Copyright © 2007 Pearson Education, Inc Publishing as Pearson Addison-Wesley Slide 1 3- 13 Building a Linked List: The node definition  Let's begin with a simple node definition: struct Node { int data; Node *link; }; typedef Node* NodePtr; Copyright © 2007 Pearson Education, Inc Publishing as Pearson Addison-Wesley Slide 1 3- 14 Building a Linked List: Declaring Pointer Variable head  With the... Addison-Wesley Slide 1 3- 25 Pseudocode for search    Make pointer variable here point to the head node while(here does not point to a node containing target AND here does not point to the last node) { make here point to the next node } If (here points to a node containing the target) return here; else return NULL; Copyright © 2007 Pearson Education, Inc Publishing as Pearson Addison-Wesley Slide 1 3-. .. Education, Inc Publishing as Pearson Addison-Wesley Slide 1 3- 15 Building a Linked List: Creating the First Node  To create the first node, the operator new is used to create a new dynamic variable: head = new Node;  Now head points to the first, and only, node in the list Copyright © 2007 Pearson Education, Inc Publishing as Pearson Addison-Wesley Slide 1 3- 16 Building a Linked List: Initializing... Inc Publishing as Pearson Addison-Wesley Slide 1 3- 28 Searching an Empty List  Our search algorithm has a problem  If the list is empty, here equals NULL before the while loop so…     here->data is undefined here->link is undefined The empty list requires a special case in our search function A refined search function that handles an empty list is shown in Display 13. 7 Copyright © 2007 Pearson Education, . Pearson Addison-Wesley Chapter 13 Pointers and Linked Lists Slide 1 3- 3 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Overview 13. 1 Nodes and Linked Lists 13. 2 Stacks. temp_ptr Slide 1 3- 20 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Display 13. 4 Translating head_insert to C++  The pseudocode for head_insert can be written in C++ using. is allowed in C++ Implementing Nodes Slide 1 3- 8 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The head of a List  The box labeled head, in display 13. 1, is not

Ngày đăng: 14/07/2014, 11:01

TỪ KHÓA LIÊN QUAN

w