1 / 17

Linked Lists

Linked Lists. Example. We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked list What are the disadvantages of using a linked list?. Linked List. Object. Object. Object. next_ptr. next_ptr. next_ptr. Ø.

calan
Download Presentation

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

  2. Example • We would like to keep a list of inventory records – but only as many as we need • An array is a fixed size • Instead – use a linked list • What are the disadvantages of using a linked list?

  3. Linked List Object Object Object next_ptr next_ptr next_ptr Ø • Node – one element of the linked list • Object – data stored in the node – examples? • next_ptr – a pointer to the next node in the list • last node points to NULL

  4. Linked List tail Object Object Object head next_ptr next_ptr next_ptr Ø • head keeps track of the head of the list • tail keeps track of the last node in the list • tail not always used

  5. Insertion at Head tail new_node Object1 Object2 head next_ptr next_ptr Ø Object3 Insert here next_ptr

  6. Insertion at Head tail new_node Object1 Object2 head next_ptr next_ptr Ø Object3 next_ptr • Create new_node • store object in new_node • Point new_node next_ptr to the node head points to

  7. Insertion at Head tail new_node Object1 Object2 head next_ptr next_ptr Ø Object3 next_ptr • Create new_node • store object in new_node • Point new_node next_ptr to the node head points to • Point head to new_node

  8. Insertion at Head tail Object3 Object1 Object2 head next_ptr next_ptr next_ptr Ø • Does this algorithm work for the list below? new_node tail Ø head Object3 next_ptr

  9. Insertion at Head • Create new_node • store object in new_node • Point new_node next_ptr to the node head points to • Point head to new_node • If tail points to NULL • point tail to new_node

  10. Insertion at Head new_node tail • Create new_node • store object in new_node • Point new_node next_ptr to the node head points to • Point head to new_node • If tail points to NULL • point tail to new_node Ø head Object3 next_ptr

  11. Insertion at Tail tail Object1 Object2 head next_ptr next_ptr Ø Object3 Insert here new_node next_ptr new_node tail Ø head Object3 next_ptr

  12. Deletion tail Object3 Object1 Object2 head next_ptr next_ptr next_ptr Ø • Deletion of head • Complexity? • Deletion of tail • Complexity?

  13. Insertion/Deletion in Middle tail Object3 Object1 Object2 head next_ptr next_ptr next_ptr Ø • Insert between Object1 and Object2 • Delete Object1

  14. Doubly Linked Lists Object1 Object2 Object3 header prev_ptr prev_ptr prev_ptr trailer next_ptr next_ptr next_ptr • Each node keeps a pointer to the next node and to the previous node • Makes some operations (such as insertion at end) more efficient • Costs? • At the beginning and end of the list are sentinel nodes • Simplify insertion/deletion algorithm

  15. Doubly Linked Lists Object1 Object2 Object3 header prev_ptr prev_ptr prev_ptr trailer next_ptr next_ptr next_ptr • Insertion and deletion at beginning/end • Insertion and deletion in middle header trailer

  16. new_node Doubly Linked Lists Object4 prev_ptr insert here next_ptr Object1 Object2 Object3 header prev_ptr prev_ptr prev_ptr trailer next_ptr next_ptr next_ptr • Insertion

  17. new_node Doubly Linked Lists Object4 prev_ptr insert here next_ptr Object1 Object2 Object3 header prev_ptr prev_ptr prev_ptr trailer next_ptr next_ptr next_ptr • Insertion • Set next_ptr of new_node to point to what header points to • Set prev_ptr of node that header points to to point to new_node • Set prev_ptr of new_node to point to header • Set header to point to new_node • Number 1 must come before number 4

More Related