Linked Lists. Dynamic Data Structure Applications –where amount of required memory is determined...

27
Linked Lists

Transcript of Linked Lists. Dynamic Data Structure Applications –where amount of required memory is determined...

Linked Lists

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.– {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}}

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

a1a1 a2a2 a3a3 a4a4

a1 4

datum pointer

a2 7 a3 10 a4 0

0 1 2 4 53 6 7 8 9 10 11 12

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 list Tail 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 listHandle current = head;While (notAtEndOfList(current)){

Print(current);Current = getNext(current);

}headcurrent

a1a1 a2a2 a3a3 a4a4

Access the list

voidprintTriangleList(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

inttriangleListLen(TriListEl *list){TriListEl *x;int result = 0;

for (x=list;x;x=x->next)result++;

return result;}

Insert

a1a1 a2a2 a3a3 a4a4

aNaNat position 2

a1a1 a2a2 a3a3 a4a4

aNaN

current

aN +1. create new

node

2. find handle to new position

3. update handles

4. return

Insert

// New nodeaN = new Node(theNewValue);

// Handle to positioncurrent = head;loop

until current indicates the target position

// Update handlesaN->next = current->next;current->next = aN;

a1a1 a2a2 a3a3 a4a4

aNaN

a1a1 a2a2 a3a3 a4a4

aNaN

current

aN +

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 memoryfree(current);

4.return

a5a5a1a1a2a2

a3a3 a4a4current

a5a5a1a1

a2a2

a3a3 a4a4

current

Don’t forget

Lookup

a1a1 a2a2 a3a3 a4a4

current

a1a1 a2a2 a3a3 a4a4

current

a1a1 a2a2 a3a3 a4a4

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 - Listcurrent

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

a2a2 a3a3 a4a4 a5a5a1a1 a6a6

list handle

list handle

List of lists

typedef struct TriListEl{int datum;struct TriListEl *next;

} TriListEl;

typedef struct ListList{TriListEl *datum;struct ListList *next;

} ListList;

voidprintCardinalities(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);}

voidfreeListList(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);}

voidfreeListList(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