Download presentation
Presentation is loading. Please wait.
1
Linked Lists
2
Linked Lists Dynamic Data Structure Applications
where amount of required memory is determined at run-time. The linked list is a dynamic data structure, where items may be added to it or deleted from it at will. Dynamic data structures are found of use in applications where the amount of memory required for the successful completion of the implemented task, is determined at run-time. A very common source of problems in program maintenance is the need to increase the capacity of a program to handle larger collections: even the most generous allowance for growth tends to prove inadequate over time!
3
{ { { Definition & Examples
A list is a finite sequence of zero or more elements. {a1,a2, ..., an} 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}} Conceptually a list is defined as a finite sequence if zero or more elements and suitably denoted. For example, the list of prime numbers less than 20, is a 8 element list, or the list of observed unicorns today, a zero element list. User inputted strings can be encoded in lists of individual characters. Furthermore, a list of lists may be defined, such a list of names or other character sequences. a1 { a2 { a3 {
4
} “Running” through the list. Enumeration
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 datum pointer a1 4 a2 7 a3 10 a4 1 2 3 4 5
to next element A conceptual of the computer representation of list data structure is illustrated by the top figure. As seen part of the data structure is used for the representation of the actual list elements (viewed in orange), while the rest represents the order of the sequence (viewed in violet). Finally, a reserved token is used to signal the end of elements, or else the end of the list. The physical interpretation of the above conceptual representation, in the memory of a computer, is illustrated by the figure below. The orange memory block are dedicated in storage of the list distinct information elements, or otherwise the stored data. The violet memory blocks store a cue, pointing to the memory location where the next list element can be found. Typically, the strategy adopted, is the use of the memory address of the next list element. This way the trail of elements may be followed from first to last. The end of the list is signified by the use of a non-valid memory address, and specifically the null pointer. a1 4 a2 7 a3 10 a4 1 2 3 4 5 6 7 8 9 10 11 12
6
Implementation List Handle
Node { Handle datum, Handle pointerToNextElement} e.g. struct Node {int myDatum, struct Node *next}; or class Node {int myDatum, Node next}; Head of list Tail of list As implicitly described in the previous transparency, a list is composed by simpler, static, data structures, referred to as list nodes, each one storing a list element and a memory address. In a generic programming language the definition of the list node is given by a structure containing the variable handles for the stored variable, and the memory address, respectively. In order for a program to refer to the list a way to refer to the first element is required. Afterwards, by following the pointer trail, access to every list element is possible. A pointer pointing to the first element is employed for this task, referred to as list handle. Due to its outstanding significance, the first list element is individually characterised as list head, while the rest of the list as list tail. 1st 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); } a1 a2 a3 a4 As noted the list is accessed, as any other data structure, through its handle. The data stored at the list nodes are accessed through their respective handles stored at each list node. List content and sequence is defined by the nodes variable values, which may be set or retrieved using the list element or pointer values. The management of these variable is structured by the use of corresponding functions. For example… head current
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 + create new node find handle to new position update handles
at position 2 current + create new node find handle to new position update handles return
11
Insert + aN a1 a2 a3 a4 aN a1 a2 a3 a4 aN // 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; + aN current a1 a2 a3 a4 aN a1 a2 a3 a4 aN
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 find handle to removal position update handles free memory
current Don’t forget find handle to removal position update handles free memory free(current); return
15
Lookup Return Value index, handle, Boolean, numeric
Enumerate through list { if (current equals target) return current; } return NOT_FOUND; Return Value index, handle, Boolean, numeric a1 a2 a3 a4 current
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 a1 a2
list handle 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; }
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; }
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) A Stack ADT allows for the retrieval of data items in the reverse order of their storage. A stack is a First-In-Last-Out (FILO) data structure, meaning the first data item added to a stack is the last item that can be retrieved from the stack.
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
© 2025 SlidePlayer.com. Inc.
All rights reserved.