Lecture%2011%20%20-%20%20Array%20of%20Linked%20Lists -...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Copyright @ 2009 Ananda Gunawardena Lecture 11 Array of Linked Lists In this lecture Array of Linked Lists Creating an Array of Linked Lists Representing a Sparse Matrix Defining a Node for Sparse Matrix Exercises Solutions An Array of Linked Lists A linked list is defined as a collection of nodes that can be traversed starting at the head node. It is important to note that head is not a node, rather the address of the first node of the list. Linked lists are very useful in situations where the program needs to manage memory very carefully and a contiguous block of memory is not needed. An array of linked lists is an important data structure that can be used in many applications. Conceptually, an array of linked lists looks as follows. An array of linked list is an interesting structure as it combines a static structure (an array) and a dynamic structure (linked lists) to form a useful data structure. This type of a structure is appropriate for applications, where say for example, number of categories is known in advance, but how many nodes in each category is not known. For example, we can use an array (of size 26) of linked lists, where each list contains words starting with a specific letter in the alphabet.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Copyright @ 2009 Ananda Gunawardena The following code can be used to create an array of linked lists as shown in the figure above. Assume that all variables are declared. node* A[n] ; // defines an array of n node pointers for (i=0; i<n; i++) A[i] = NULL; // initializes the array to NULL Creating an Array of Linked Lists Suppose that a linked list needs to be created starting at A[i]. The first node can be created as follows. A[i] = (node*)malloc(sizeof(node)); // allocate memory for
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/27/2009 for the course CS 123 taught by Professor Bajkzek during the Fall '08 term at Carnegie Mellon.

Page1 / 7

Lecture%2011%20%20-%20%20Array%20of%20Linked%20Lists -...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online