Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linked Lists.

Similar presentations


Presentation on theme: "Linked Lists."— Presentation transcript:

1 Linked Lists

2

3 Linked Lists A linked list is a series of connected nodes
B C Head A linked list is a series of connected nodes Each node contains at least A piece of data (any type) Pointer to the next node in the list Head: pointer to the first node The last node points to NULL node A data pointer

4 A Simple Linked List Node Struct
Declare Node struct for the nodes _data : double-type data in this example _next : a pointer to the next node in the list struct Node { double _data; // the data Node *_next; // pointer to the next node };

5 Variations of Linked Lists
Circular linked lists The last node points to the first node of the list How do we know when we have finished traversing the list? (Tip: check if the pointer of the current node is equal to the head.) A B C Head

6 Variations of Linked Lists
Doubly linked lists Each node points to not only successor but the predecessor There are two NULL: at the first and last nodes in the list Advantage: given a node, it is easy to visit its predecessor. Convenient to traverse lists backwards A B C Head

7 Array versus Linked Lists
Linked lists are more complex to code and manage than arrays, but they have several advantages. Dynamic length: a linked list can easily grow and shrink in size. We don’t need to know how many nodes will be in the list. They are created in memory as needed. But, this can be solved using dynamic arrays! Easy and fast insertions and deletions To insert or delete an element in an array, we need to copy to temporary variables to make room for new elements or close the gap caused by deleted elements. With a linked list, no need to move other nodes. Only need to reset some pointers.

8 Dynamic arrays & Linked lists
Let’s compute the time it takes to double the size of dynamic array & linked list. And, dynamic array has immediate “random access”! But, as said before, the main advantage of linked lists is the ability to move and insert data at any place of the list. Like you’ll do on the ex.

9 בנית רשימה מהתחלה לסוף ומהסוף להתחלה
מכאן: אלגוריתם פשוט להיפוך רשימה: עוברים עליה מההתחלה לסוף, ובונים היעתק מהסוף להתחלה.


Download ppt "Linked Lists."

Similar presentations


Ads by Google