Linked Lists
Dynamic Data Structure Applications –where amount of required memory is determined at run-time.
Definition & Examples A list is a finite sequence of zero or more elements. –{a 1,a 2,..., a n } E.g. the list of prime numbers less than 20 –{2,3,5,7,11,13,17,19} A line of text of individual characters –{h,e,l,l,o,,w,o,r,l,d} A list of lists! –{{1,2,, e,g,g,s},{1,, s,a,l,a,m,i},{1,k,g,r,,o,f,,to,m,a,t,o,e,s}} a1 { a2 { a3 {
List Operations Print Length Insert Remove Lookup Other, e.g. list cloning, sub-list. Error Checking } “Running” through the list. Enumeration
Representation a1 a2 a3 a4 a14 datumpointer a27a310a to next element
Implementation Node { Handle datum, Handle pointerToNextElement} e.g. struct Node {int myDatum, struct Node *next}; or class Node {int myDatum, Node next}; 1st Head of listTail of list List Handle
Accessing the Data Structure List Handle setData(), getData(), setNext(), getNext() E.g. void setData(struct Node *whichNode, int value); Or Node.setData(int); Printing the list Handle current = head; While (notAtEndOfList(current)) { Print(current); Current = getNext(current); } headcurrent a1 a2 a3 a4
Access the list void printTriangleList(TriListEl *list) { TriListEl *x; for (x=list;x;x=x->next) printf("->%d\n",x->datum); /* datum can be a data structure then printTriangle (x->datum)*/ }
Number of elements int triangleListLen(TriListEl *list) { TriListEl *x; int result = 0; for (x=list;x;x=x->next) result++; return result; }
Insert a1 a2 a3 a4 aNaN aNaN at position 2 a1 a2 a3 a4 aNaN aNaN current aNaN + 1.create new node 2.find handle to new position 3.update handles 4.return
Insert // New node aN = new Node(theNewValue); // Handle to position current = head; loop until current indicates the target position // Update handles aN->next = current->next; current->next = aN; a1 a2 a3 a4 aNaN aNaN a1 a2 a3 a4 aNaN aNaN current aNaN +
TriListEl * addToTriangleList(TriListEl *list, int datum) { TriListEl *x, *result, *current, *newEl; newEl = (TriListEl *) malloc(sizeof(TriListEl)); newEl->datum = datum; newEl->next = (TriListEl *) NULL; if (list == (TriListEl *) NULL) { result = newEl; result->next = (TriListEl *) NULL; } else { for (x=list;x;x=x->next) { if (x->next == (TriListEl *) NULL) break; } current = x; current->next = newEl; current = newEl; current->next = (TriListEl *) NULL; result = list; } return result; }
for (x=list;x;x=x->next) { if (x->next == (TriListEl *) NULL) break; } current = x; current->next = newEl; current = newEl; current->next = (TriListEl *) NULL; result = list;
Remove 1.find handle to removal position 2.update handles 3.free memory free(current); 4.return a5a5 a5a5 a1 a2 a3 a4 current a5a5 a5a5 a1 a2 a3 a4 current Don’t forget
Lookup a1 a2 a3 a4 current a1 a2 a3 a4 current a1 a2 a3 a4 current Enumerate through list { if (current equals target) return current; } return NOT_FOUND; Return Value index, handle, Boolean, numeric
list_el * getByKey(list_el *list, char *mykey) { list_el *x; for (x=list;x;x=x->next) { if (strcmp(x->key,mykey)==0) return x; } return (list_el *) NULL; }
List of Data Structures Insertion Removal ?
Sub - List current May be composed of simpler operations
typedef struct Triangle { Point p1,p2,p3; } Triangle; typedef struct List { Triangle datum; struct List *next; } List;
Structural Variants Doubly Linked List Cyclic List List of Lists a2 a3 a4 a5a5 a5a5 a1 a6a6 a6a6 list handle
List of lists typedef struct TriListEl { int datum; struct TriListEl *next; } TriListEl; typedef struct ListList { TriListEl *datum; struct ListList *next; } ListList;
void printCardinalities(ListList *list) { ListList *x; int cnt = 1; for (x=list;x;x=x->next) { printf(“%d ",triListLen(x->datum)); cnt++; } printf("\n all elems = %d\n“,cnt); }
void freeListList(ListList *list) { ListList *x, *prev; prev = (ListList *) NULL; for (x=list;x;x=x->next) { if (prev != (ListList *) NULL) free(prev); freeTriangleList(x->datum); prev = x; } if (prev != (ListList *) NULL) free(prev); }
void freeListList(ListList *list) { ListList *x, *prev; prev = (ListList *) NULL; for (x=list;x;x=x->next) { if (prev != (ListList *) NULL) free(prev); freeTriangleList(x->datum); prev = x; } if (prev != (ListList *) NULL) free(prev); }
Abstraction Abstraction of Representation w.r.t. Stored Data Type Code Reusability Implementation –Pointer Abstraction C –Object abstraction Java –Templates C++
Algorithmic Variants Push / Pop Stack Enqueue, Dequeue Queues –FILO (stack) –FIFO (priority queue)
How to remember the length? struct ListHolder { int numberOfElements /* private, init=-1*/ ListElement *head; } int lengthList(struct ListHolder *) addElement, deleteElement must update numberOfElements