1 / 16

Linked Lists

Linked Lists. Linked list : a list of items (nodes), in which the order of the nodes is determined by the address, called the link , stored in each node. Link field in last node is NULL. Linked Lists (continued). Example:

oralee
Télécharger la présentation

Linked Lists

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linked Lists • Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node Link field in last node is NULL C++ Programming: Program Design Including Data Structures, Fourth Edition

  2. Linked Lists (continued) • Example: • Suppose that the first node is at memory location 1200, and the second node is at memory location 1575 C++ Programming: Program Design Including Data Structures, Fourth Edition

  3. Linked Lists (continued) • Because each node of a linked list has two components, we need to declare each node as a class or struct • Data type of a node depends on the specific application • The link component of each node is a pointer C++ Programming: Program Design Including Data Structures, Fourth Edition

  4. Linked Lists: Some Properties C++ Programming: Program Design Including Data Structures, Fourth Edition

  5. Linked Lists: Some Properties (continued) • current = head; • Copies value of head into current C++ Programming: Program Design Including Data Structures, Fourth Edition

  6. Linked Lists: Some Properties (continued) • current = current->link; C++ Programming: Program Design Including Data Structures, Fourth Edition

  7. exercise ? ??? ? ? ? ?

  8. Linked Lists: Some Properties (continued) C++ Programming: Program Design Including Data Structures, Fourth Edition

  9. Traversing a Linked List • The basic operations of a linked list are: • Search to determine if an item is in the list • Insert an item in the list • Delete an item from the list • Traversal: given a pointer to the first node of the list, step through the nodes of the list C++ Programming: Program Design Including Data Structures, Fourth Edition

  10. Traversing a Linked List (continued) • To traverse a linked list: • Example: C++ Programming: Program Design Including Data Structures, Fourth Edition

  11. Item Insertion and Deletion • Consider the following definition of a node: • We will use the following variable declaration: C++ Programming: Program Design Including Data Structures, Fourth Edition

  12. Insertion • Consider the following linked list: • A new node with info50 is to be created and inserted after p C++ Programming: Program Design Including Data Structures, Fourth Edition

  13. The sequence of statements to insert the node is very important. Suppose that we reverse the sequence of the statements and execute the statements in the following order:

  14. Deletion Node with info 34 is removed from the list, but memory is still occupied; node is dangling C++ Programming: Program Design Including Data Structures, Fourth Edition

More Related