Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 16 Linked Lists. In this lecture Fundamentals Applications Memory Allocation Creating a List Inserting Nodes.

Similar presentations


Presentation on theme: "Lecture 16 Linked Lists. In this lecture Fundamentals Applications Memory Allocation Creating a List Inserting Nodes."— Presentation transcript:

1 Lecture 16 Linked Lists

2 In this lecture Fundamentals Applications Memory Allocation Creating a List Inserting Nodes

3 Fundamentals of Linked Lists A dynamic data structure Head pointer indicates the beginning of the list List traversal starts from the head NULL pointer indicates the end of the list List traversal ends at NULL or when the goal is reached Head 10 ff 20 aa 32 f1 40 01 NULL

4 Fundamentals of Linked Lists Three logically distinct forms of linked lists – Empty List – List with only one node – List with more than one node Head NULL Head 10 ff NULL Head NULL

5 Types of Linked Lists Single linked list This is the most basic type of linked list, with each node containing a single pointer, to the next node. Multi linked list More advanced than the single linked list, each node may be connected to many other nodes. Special case : Doubly linked lists Circular linked list With a circular linked list, the last node is connected to the first, to form a circle. Head NULL Head NULL Head

6 Examples Linked Lists Read a file and build a list of nodes in order. Insert a new node in order. Delete a node. Search for a target node (single linked list) Multi Linked List : The standard use of multi-linked lists is to organize a collection of elements in many different ways. For example, suppose my elements include the name of a person and his/her age. e.g. (FRED,19) (MARY,16) (JACK,21) (JILL,18). I might want to order these elements alphabetically and also order them by age. I would have two pointers - NEXT-alphabetically, NEXT-age - and the list header would have two pointers, one based on name, the other on age. Age pointer name pointer Fred 19Jack 21Jill 18Mary 16 NULL

7 Examples of Doubly Linked Lists Sparse Matrices - common use A sparse matrix is a matrix of numbers, in which almost all the entries are zero. These arise frequently in engineering applications. Typically only about N elements are non-zero. For example: We can represent this by having linked lists for each row and each column. Because each node is in exactly one row and one column it will appear in exactly two lists - one row list and one column. So it needs two pointers: Next-in-this-row and Next-in-this-column. In addition to storing the data in each node, it is normal to store the co- ordinates (i.e. the row and column the data is in in the matrix). Operations that set a value to zero cause a node to be deleted, and vice versa. 0 88 0 0 0 0 27 0 0 19 0 66

8 Sparse Matrix Implementation

9 Doubly Linked List - An Example

10 Class Node class Node { public Node(); public Node(int x) ; public Node(int x, Node ptr) public Node getPtr(); public int getValue(); public void setPtr(Node ptr); public void setValue(int) ; private int value; private Node next; }; value next

11 Implementing node methods Constructors Node() { value = 0; next = NULL;} Node(int x) { value = x;} Node(int x, Node ptr) {value = x; next = ptr;} Accessors Node getPtr() { return next;} int getValue() { return value;} Mutators void setPtr(Node ptr) { next = ptr;} void setValue(int) { value = next;}

12 Graph Implementation - class Edge class Edge { int dest; double cost; Edge next; Edge(int d, double c, Edge link) {dest=d, cost=c, next=link} };


Download ppt "Lecture 16 Linked Lists. In this lecture Fundamentals Applications Memory Allocation Creating a List Inserting Nodes."

Similar presentations


Ads by Google