1. Trang chủ
  2. » Công Nghệ Thông Tin

Linked lists in action

45 252 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

Cấu trúc

  • Linked Lists in Action

  • Declarations for Linked Lists

  • Slide 3

  • Slide 4

  • Slide 5

  • Slide 6

  • Inserting a Node at the Front

  • Slide 8

  • Slide 9

  • Slide 10

  • Slide 11

  • Slide 12

  • Slide 13

  • Slide 14

  • Slide 15

  • Slide 16

  • Slide 17

  • Slide 18

  • Slide 19

  • Slide 20

  • Slide 21

  • Caution!

  • Pseudocode for Inserting Nodes

  • Slide 24

  • Slide 25

  • Slide 26

  • Slide 27

  • Slide 28

  • Slide 29

  • Slide 30

  • Slide 31

  • Slide 32

  • Slide 33

  • Slide 34

  • Slide 35

  • Slide 36

  • Slide 37

  • Pseudocode for Removing Nodes

  • Removing the Head Node

  • Slide 40

  • Slide 41

  • Slide 42

  • Slide 43

  • Summary

  • THE END

Nội dung

 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  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; }; 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; }; 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; }; Declarations for Linked Lists 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 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 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, We want to add a new entry, 13, to the to the front front of the linked list of the linked list shown here. shown here. 10 15 7 null head_ptr entry 13 Inserting a Node at the Front  Create a new node, pointed to by a local variable insert_ptr. 10 15 7 7 null head_ptr entry 13 insert_ptr void list_head_insert(node*& head_ptr, const node::value_type& entry); Inserting a Node at the Front insert_ptr = new node; 10 15 7 7 null head_ptr entry 13 insert_ptr void list_head_insert(node*& head_ptr, const node::value_type& entry); Inserting a Node at the Front 10 15 7 null head_ptr entry 13 insert_ptr 13 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); [...]... 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); 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); T we he f alr unc ead tio yw n ro t e  Pseudocode for Inserting Nodes  Determine whether... will be the first node in the linked list If so, then there is only one step: list_head_insert(head_ptr, entry); A pointer to the head of the 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); in Th e t h da e n ta ew t o no put de Pseudocode for Inserting Nodes  Otherwise... head_ptr = insert_ptr; } When the function returns, the linked list has one node 13 head_ptr null Caution! Always make sure that your linked list functions work correctly with an empty list EMPTY 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 Pseudocode for Inserting... completely created in one step by calling an appropriate node constructor 15 10 7 13 entry insert_ptr null head_ptr Inserting a Node at the Front void list_head_insert(node*& head_ptr, const node::value_type& entry); insert_ptr = new node(entry, head_ptr); Make the old head pointer 13 point to the new node insert_ptr 15 10 7 13 entry null head_ptr Inserting a Node at the Front void list_head_insert(node*&... entry null head_ptr 13 null insert_ptr 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; } 13 13 entry head_ptr null insert_ptr 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,... by setting a pointer named previous_ptr to point to the node which is just before the new node's position 13 list_head_insert(previous_ptr->link( ), entry); previous_ptr 10 15 Use a node member function to get the link field if 7 null head_ptr 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,... 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 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 ? Inserting a Node... position previous_ptr->link_field points to the head of a small linked list, with 10 and 7 previous_ptr 10 15 7 null head_ptr 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 13 The new node must be inserted at the front of this small linked list Write one C++... node::value_type& entry); insert_ptr = new node(entry, head_ptr); head_ptr = insert_ptr; 13 insert_ptr 15 10 7 13 entry null head_ptr Inserting a Node at the Front void list_head_insert(node*& head_ptr, const node::value_type& entry); insert_ptr = new node(entry, head_ptr); head_ptr = insert_ptr; 13 15 When the function returns, the linked list has a new node at the front 10 7 null head_ptr Inserting a Node at... setting a pointer named previous_ptr to point to the node which is just before the new node's position 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 In this example, the new node will be the second node previous_ptr 10 15 7 null head_ptr Pseudocode for Inserting .  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  For this. int value_type; private value_type data_field; node *link_field; }; 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. *link_field; }; 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

Ngày đăng: 24/10/2014, 01:17

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN