Linked List ADT used to store information in

  • Slides: 7
Download presentation
Linked List • ADT used to store information in a list • Consists of

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 Professor John Carelli Kutztown University Computer Science Department

Linked List head Node Data Node* Professor John Carelli Kutztown University NULL Computer Science

Linked List head Node Data Node* Professor John Carelli Kutztown University NULL Computer Science Department

Advantages/Disadvantages • Advantages • Can additional items to the list without affecting existing items

Advantages/Disadvantages • Advantages • Can 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 Professor John Carelli Kutztown University Computer Science Department

Source: www. slideshare. net Professor John Carelli Kutztown University Computer Science Department

Source: www. slideshare. net Professor John Carelli Kutztown University Computer Science Department

Iterators • A special class that is used to sequentially search a linked list

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 Professor John Carelli Kutztown University Computer Science Department

Iterator operation • Iterator maintains a pointer to a “current “ list node •

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 Professor John Carelli Kutztown University Computer Science Department

More complex Linked Lists • Linked lists can be made circular • “last” node

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 Professor John Carelli Kutztown University Computer Science Department