2/4/2011 1
1
Linear Structures
Chapter 3
CPTR 318
2 CPTR 314
Linear Structure
Every non-empty linear structure has
A unique element called first A unique element called last Every element except last has a unique successor Every element except first has a unique
predecessor
Implementations
Array
Contiguous memory Space needed only for data Insertions and deletions may require data movement
Linked list
Non-contiguous memory Data and links both require space Insertions and deletions require no data movement
Array details
For an array A of size n, where n > 0: First element: A[0] Last element: A[n – 1] Predecessor of A[i], for all i > 0: A[i – 1] Successor of A[i], for all i < n – 1: A[i + 1] n − 1 i − 1 i + 1 i n A template <typename T> struct Node { T data; Node *prev, *next; }; template <typename T> class List { Node<T> *head, *tail; // Methods omitted … };
List details
For a list L, where L.head is not null, and p is a
Node<T> pointer to an element in the list:
First element: L.head Last element: L.tail Predecessor of p, for all p ≠ L.head: p->prev Successor of p, for all p ≠ L.tail: p->next