35
Linked Lists... Linked Lists... An introduction to An introduction to creating dynamic data creating dynamic data structures structures

Linked Lists

  • Upload
    weylin

  • View
    46

  • Download
    3

Embed Size (px)

DESCRIPTION

Linked Lists. An introduction to creating dynamic data structures. Post-conditions. To extend understanding of pointers by using them to create dynamic data structures Understand when to use a Linked List Be able to create a Linked List in C understand internals be able to program them!. - PowerPoint PPT Presentation

Citation preview

Page 1: Linked Lists

Linked Lists...Linked Lists...

An introduction to creating An introduction to creating dynamic data structuresdynamic data structures

Page 2: Linked Lists

Post-conditionsPost-conditions

• To extend understanding of pointers by using them to create dynamic data structures

• Understand when to use a Linked List

• Be able to create a Linked List in C– understand internals– be able to program them!

Page 3: Linked Lists

OutlineOutline

• Linked Lists introduction

• Characteristics of Linked Lists

• Programming Linked Lists

Page 4: Linked Lists

Linked Lists definitionLinked Lists definition• Abstract common use of “list”

– TODO:• Read task description• Design test data• Create makefile• Collect other files needed• Begin working on code• Type in first version• Test it• …

Page 5: Linked Lists

Linked Lists and pointersLinked Lists and pointers• The word “list” suggests an ordered collection

• The word “linked” hints at pointers to where the next element in the list is located– This is different from arrays, which use contiguous

memory locations– Linked lists may use multiple links to link elements

according to different interpretations of “order”.– We will only consider “singly linked lists” for now.

Page 6: Linked Lists

Linked Lists memory diagramsLinked Lists memory diagrams• A list of the numbers [0, 12, -4, 22]• An array of the numbers [0, 12, -4, 22]

• Where are arrays stored?– Global/static/extern/const?– Stack?– Heap?

• Where are linked lists stored?

Page 7: Linked Lists

Linked Lists: InternalsLinked Lists: Internals• The dynamic nature of the linked list data

structure means we must allocate and free memory on demand

• Some languages do this for you.

• Not so for C - you have to do it explicitly using malloc/calloc/realloc/free.

Page 8: Linked Lists

Linked Lists: InternalsLinked Lists: InternalsHere is a high-level schematic of a linked list.

A pointer enables us to reference its 1st element.

Element 0Element 0 Element 1Element 1 Element 2Element 2

PointerPointer

Page 9: Linked Lists

Things to consider in test designThings to consider in test designThree distinctive parts of the list - what are they?

Element 0Element 0 Element 1Element 1 Element 2Element 2

PointerPointer

Page 10: Linked Lists

Linked Lists: InternalsLinked Lists: InternalsEach element in our singly-linked list has two components:– the data component

• anything you want...

– the link component• a pointer, of course!

DataData

LinkLink

Page 11: Linked Lists

Linked Lists: InternalsLinked Lists: InternalsWe now expose the internals of the previous schematic.

Element 0Element 0 Element 1Element 1 Element 2Element 2

PointerPointer

Data 0Data 0

PointerPointer

Data 1Data 1

PointerPointer

Data 2Data 2

PointerPointerNULLNULL

Q: How might a Linked List be terminated?

A: By our “old friend”, the NULL pointer...

Page 12: Linked Lists

Linked Lists: InternalsLinked Lists: InternalsAdding an element to the front of the list.

Element 1Element 1 Element 2Element 2

PointerPointer

Element 0Element 0DataData

PointerPointer

Data 0Data 0

PointerPointer

Data 1Data 1

PointerPointerNULLNULL

Page 13: Linked Lists

PointerPointer

Linked Lists: InternalsLinked Lists: InternalsAdding an element to the front of the list.

Element 1Element 1 Element 2Element 2Element 0Element 0Data 0Data 0

PointerPointer

Data 2Data 2

PointerPointerNULLNULL

Data 1Data 1

PointerPointer

Page 14: Linked Lists

PointerPointer

Linked Lists: InternalsLinked Lists: InternalsNew links are forged by simple assignment operations.

Element 1Element 1 Element 2Element 2Element 0Element 0Data 0Data 0

PointerPointer

Data 2Data 2

PointerPointerNULLNULL

Data 1Data 1

PointerPointer

Q: What happens if we change the order?

A: Ooops!

Page 15: Linked Lists

Linked Lists: InternalsLinked Lists: InternalsUsing “boxes and arrows” to show what we are doing with pointers is very helpful…

But it is easy to lose sight of one simple thing:

One pointer can only point to one thing!One pointer can only point to one thing!

Every time we “draw an arrow” we effectively erase any existing arrow coming from that box.

In the previous example we “lost” the linked list because we neglected this!

Page 16: Linked Lists

Element 1Element 1DataData

PointerPointer

Linked Lists: InternalsLinked Lists: InternalsAdding an element elsewhere.

Element 0Element 0 Element 2Element 2

PointerPointer

Data 0Data 0

PointerPointer

Data 1Data 1

PointerPointerNULLNULL

Page 17: Linked Lists

Element 1Element 1Data 1Data 1

PointerPointer

Linked Lists: InternalsLinked Lists: InternalsAdding an element elsewhere.

Element 0Element 0 Element 2Element 2Data 0Data 0

PointerPointer

Data 2Data 2

PointerPointerNULLNULL

PointerPointer

Page 18: Linked Lists

Characteristics of Linked ListsCharacteristics of Linked ListsWhat is the worst-case situation for altering the n-th element of:

– an array?

– a linked list?

Altering an element anywhere in the array has the same cost. Arrays elements are referred to by their address offset and have O(1) cost.

Altering an element to the end of the linked list is more costly as the list has to be traversed. The cost is therefore O(n).

Page 19: Linked Lists

Characteristics of Linked ListsCharacteristics of Linked ListsAdvantages of linked lists:

• Dynamic nature of the data structure

• Flexible structure– singly linked lists– doubly linked – circular lists– etc., etc.

Page 20: Linked Lists

Characteristics of Linked ListsCharacteristics of Linked ListsDisadvantages of linked lists:

• Linear worst-case cost of access

• The absence of a Linked List implementation in standard C libraries

Page 21: Linked Lists

Programming Linked ListsProgramming Linked ListsThe dynamic nature of the data structure means we must allocate and free memory

– malloc()• to allocate memory when we add an element

– free()• to de-allocate memory when we remove an element

Page 22: Linked Lists

Programming Linked ListsProgramming Linked ListsThe link element we will use for the example:

typedef struct List List;

struct List {

char *content;

List *next;

}Could be anything, of course!!

Note the self-referential nature of the pointer; it points to one of these structures.

char *char *

List *List *

”Hello””Hello”

Page 23: Linked Lists

Programming Linked ListsProgramming Linked ListsAdding an element to the front of the list.

List *addToFront(List *listp, List *newp) {newp->next = listp;return newp;

}

Draw memory diagrams for:empty list; list with one element; list with three elements.

Page 24: Linked Lists

List *addToFront(List *listp, List *newp) {

newp->next = listp;

return newp;

}

char *char *

List *List *

List *listpList *listp

char *char *

List *List *

List *newpList *newp

char *char *

NULLNULL

Return this pointer

Page 25: Linked Lists

Programming Linked ListsProgramming Linked ListsAdding an element to the end of the list.

List *addToEnd(List *listp, List *newp) {

List *p;

if (listp == NULL)

return newp;

for (p = listp; p->next != NULL; p = p->next)

; /* null statement */

p->next = newp;

return listp;

}

Page 26: Linked Lists

Programming Linked ListsProgramming Linked ListsDraw pictures of case of empty list *listp

List *addToEnd(List *listp, List *newp) {

List *p;

if (listp == NULL)

return newp;

for (p = listp; p->next != NULL; p = p->next)

; /* null statement */

p->next = newp;

return listp;

}

Page 27: Linked Lists

NULLNULL

char *char *

NULLNULL

List *newpList *newp

List *addToEnd(List *listp, List *newp) {List *p;if (listp == NULL)

return newp;for (p = listp; p->next != NULL; p = p->next)

;p->next = newp;return listp;

}

Return this pointer

Page 28: Linked Lists

Programming Linked ListsProgramming Linked ListsDraw pictures of case of list *listp containing 2 elements

List *addToEnd(List *listp, List *newp) {

List *p;

if (listp == NULL)

return newp;

for (p = listp; p->next != NULL; p = p->next)

; /* null statement */

p->next = newp;

return listp;

}

Page 29: Linked Lists

char *char *

List *List *

List *listpList *listp

char *char *

NULLNULL

List *newpList *newp

char *char *

NULLNULL

List *addToEnd(List *listp, List *newp) {List *p;if (listp == NULL)

return newp;for (p = listp; p->next != NULL; p = p->next)

;p->next = newp;return listp;

}

List *pList *p

List *

Page 30: Linked Lists

Programming Linked ListsProgramming Linked ListsDe-allocating a complete list

void freeAll(List *listp) {

List *p;

for ( ; listp != NULL; listp=p) {

p = listp->next;

free(listp->content); /* the string */

free(listp);

}

}

Page 31: Linked Lists

Programming Linked ListsProgramming Linked ListsDraw the memory diagram for a 5 element list.

void freeAll(List *listp) {List *p;for ( ; listp != NULL; listp=p) {

p = listp->next;free(listp->content); /* the string */free(listp);

}}

Page 32: Linked Lists

char *char *

List *List *

char *char *

NULLNULL

char *char *

List *List *

void freeAll(List *listp) {

List *p;

for ( ; listp != NULL; listp=p) {

p = listp->next;

free(listp->content);

free(listp);

}

}

List *pList *pNULL

List *listpList *listpNULL

Page 33: Linked Lists

Programming Linked ListsProgramming Linked ListsWrite a function that deletes the first element in a list.

List * deleteFirst(List *listp) {

List *p;

if (listp != NULL) {

p = listp;

listp = listp->next;

free(p);

}

return listp;

}

Page 34: Linked Lists

Programming Linked ListsProgramming Linked ListsWrite a function that counts the number of elements in

a list.

int count(List *listp) {

int cnt = 0;

for ( ; listp != NULL; cnt++)

listp = listp->next;

return cnt;

}

Page 35: Linked Lists

Post-conditionsPost-conditions

To extend understanding of pointers by using them to create dynamic data structures

Understand when to use a Linked List

Be able to create a Linked List in C understand internals be able to program them!