CS 228 meeting -*- Outline -*- * array implementations of linked lists (HR 8.7) ** motivation shows that linked lists need not be exclusively associated with dynamic data ------------------------------------------ WHY STUDY LINKED LISTS IMPLEMENTED BY ARRAYS? - Shows that linked lists aren't exclusively implemented by dynamic data - Shows how to manage free storage - Sometimes needed for maximum efficiency ------------------------------------------ ** array representation ------------------------------------------ ARRAY REPRESENTATION // ACharNode.h typedef int ACharPtr; struct ACharNode { char data; ACharPtr link; }; const ACharPtr NULL_PTR = -1; // client code #include "ACharNode.h" ACharNode heap[100]; ACharPtr head; Abstract diagram: Concrete picture: ------------------------------------------ show the list (G T L) *** managing a free store ------------------------------------------ MANAGING A FREE STORE // heap.h #include "ACharNode.h" const int HEAP_SIZE = 10; extern ACharNode heap[HEAP_SIZE]; extern void InitializeHeap(); // POST: initializes the storage in the // heap to be all free extern ACharPtr NewLoc(); // PRE: InitializeHeap has been called // POST: IF space is available // THEN FCTVAL == index of unused spot // ELSE FCTVAL == NULL_PTR extern void Deallocate ( ACharPtr p ); // PRE: InitializeHeap has been called // && p is result of previous call // to NewLoc. // POST: IF p is not NULL_PTR, THEN // p is marked as free ------------------------------------------ ------------------------------------------ MANAGING A FREE STORE (2) // heapHR.C #include "heap.h" #include #include "bool.h" ACharNode heap[HEAP_SIZE]; static Boolean free[HEAP_SIZE]; Picture: ------------------------------------------ ------------------------------------------ OPERATIONS ON FREE STORE // heapHR.C #include "heap.h" #include #include "bool.h" ACharNode heap[HEAP_SIZE]; static Boolean free[HEAP_SIZE]; void InitializeHeap() // MODIFIES: heap // POST: free[0..HEAP_SIZE] == TRUE { for (int i = 0; i < HEAP_SIZE; i++) { free[i] = TRUE; } } ACharPtr NewLoc() // MODIFIES: free // POST: IF space is available in free // THEN FCTVAL == index of unused spot // ELSE FCTVAL == NULL_PTR { } ------------------------------------------ { for (int i = 0; i < HEAP_SIZE && !free[i]; i++) { ; } // ASSERT: i == HEAP_SIZE || free[i] if (i == HEAP_SIZE) { return NULL_PTR; } else { free[i] = FALSE; return i; } } ------------------------------------------ void Deallocate ( ACharPtr p ) // PRE: p is result of previous call // to NewLoc. // MODIFIES: free // POST: IF p is not NULL_PTR, THEN // free[p] == TRUE { } ------------------------------------------ if (p != NULL_PTR) { free[p] = TRUE; } ------------------------------------------ FOR YOU TO DO Implement the following (hint: use NewLoc) // test.C #include #include "ACharNode.h" #include "heap.h" ACharPtr Cons(char c, ACharPtr rest) // MODIFIES: cerr // POST: IF can't allocate // THEN give error and stop program // ELSE return pointer to new AcharNode // initialized with c and rest { } ------------------------------------------ ACharPtr p = NewLoc(); if (p != NULL_PTR) { heap[p].data = c; heap[p].link = rest; return p; } else { cerr << "No more space to allocate." << endl; exit(1); } } If time, can also do... void PrintList(ACharPtr head) { ACharPtr current = head; // INV: all items before current have been printed // and current is NULL_PTR or pointing at an item in the list starting // with head while (current != NULL_PTR) { cout << heap[current].data << endl; current = heap[current].link; } } *** insertion Can also show the following on line, with program that prints the arrays heap and free ------------------------------------------ INSERTION // of char x at front of the list head = Cons(x, head); // of char in x after prevPtr heap[prevPtr].link = Cons(x, heap[prevPtr].link); ------------------------------------------ draw pictures *** deletion ------------------------------------------ DELETION // of node after prevPtr ACharPtr tempPtr = heap[prevPtr].link; heap[prevPtr].link = heap[tempPtr].link; Deallocate(tempPtr); ------------------------------------------ Q: How would you delete the node pointed to by the list head? ** summary a lot like the dynamic implementation, but uses different syntax have to allocate more memory, but faster at run-time, as don't pay price for general-purpose storage allocation takes more time to develop