Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linked List ADT used to store information in a list

Similar presentations


Presentation on theme: "Linked List ADT used to store information in a list"— Presentation transcript:

1 Linked List ADT used to store information in a list
Consists of a series of “linked” containers, called “nodes”, that hold the stored data Each node contains: Storage for one data element A pointer to the next element in the list The linked list maintains a pointer to the first element The pointer stored in the last node is set to NULL

2 Linked List Node Node Node Node Data Data Data Data head Node* Node*
NULL

3 Advantages/Disadvantages
Can add additional items to the list without affecting existing items Can insert items into arbitrary locations Allows for maintaining a sorted list as elements are added No wasted memory – only allocate what’s needed Disadvantage Access is not random Locating an item requires a sequential search Memory overhead (need to store pointers) Storage is not localized

4 Source: www.slideshare.net

5 Iterators A special class that is used to sequentially search a linked list Needed to maintain “encapsulation” Individual nodes (and pointers) should remain hidden from a user Usually declared a “friend” of the linked list Since it needs to keep track of the nodes in the list A compromise to normal OOP practices

6 Iterator operation Iterator maintains a pointer to a “current “ list node Each sequential call to an update/next method moves the pointer to the next list node Iterator starts at the head node A method is provided to reset the pointer to the head so the iterator can be reused A method is provided to access the stored data in the current node Iterators shield the user from the details of Node properties (and existence) Pointer manipulation Multiple iterators can be created for a given list

7 More complex Linked Lists
Linked lists can be made circular “last” node points back to “first” Doubly linked Two pointers per node Allows both forwards and backwards traversal


Download ppt "Linked List ADT used to store information in a list"

Similar presentations


Ads by Google