Download presentation
Presentation is loading. Please wait.
Published byAlvin Gordon Modified over 9 years ago
1
Linked Lists
2
Dynamic Data Structure Applications –where amount of required memory is determined at run-time.
3
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 {
4
List Operations Print Length Insert Remove Lookup Other, e.g. list cloning, sub-list. Error Checking } “Running” through the list. Enumeration
5
Representation a1 a2 a3 a4 a14 datumpointer a27a310a40 0124536789101112 to next element
6
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
7
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
8
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)*/ }
9
Number of elements int triangleListLen(TriListEl *list) { TriListEl *x; int result = 0; for (x=list;x;x=x->next) result++; return result; }
10
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
11
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 +
12
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; }
13
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;
14
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
15
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
16
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; }
17
List of Data Structures Insertion Removal ?
18
Sub - List current May be composed of simpler operations
19
typedef struct Triangle { Point p1,p2,p3; } Triangle; typedef struct List { Triangle datum; struct List *next; } List;
20
Structural Variants Doubly Linked List Cyclic List List of Lists a2 a3 a4 a5a5 a5a5 a1 a6a6 a6a6 list handle
21
List of lists typedef struct TriListEl { int datum; struct TriListEl *next; } TriListEl; typedef struct ListList { TriListEl *datum; struct ListList *next; } ListList;
22
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); }
23
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); }
24
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); }
25
Abstraction Abstraction of Representation w.r.t. Stored Data Type Code Reusability Implementation –Pointer Abstraction C –Object abstraction Java –Templates C++
26
Algorithmic Variants Push / Pop Stack Enqueue, Dequeue Queues –FILO (stack) –FIFO (priority queue)
27
How to remember the length? struct ListHolder { int numberOfElements /* private, init=-1*/ ListElement *head; } int lengthList(struct ListHolder *) addElement, deleteElement must update numberOfElements
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.