L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked...

54
1 CSCI 104 Rafael Ferreira da Silva [email protected] Slides adapted from: Mark Redekopp and David Kempe

Transcript of L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked...

Page 1: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

1

CSCI104

[email protected]

Slidesadaptedfrom:MarkRedekopp andDavidKempe

Page 2: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

2

Courtesy of Randall Munroe @ http://xkcd.com

XKCD #138

Page 3: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

3

RECURSION(cont.)

Page 4: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

4

RecursiveDefinitions• N=Non-NegativeIntegersandisdefinedas:

– Thenumber0[Base]– n+1wherenissomenon-negativeinteger[Recursive]

• String– Emptystring,ε– Stringconcatenatedwithacharacter(e.g.'a'-'z')

• Palindrome(stringthatreadsthesameforwardasbackwards)– Example:dad,peep,level– Definedas:

• Emptystring[Base]• Singlecharacter[Base]• xPx wherexisacharacterandPisaPalindrome[Recursive]

• Recursivedefinitionsareoftenusedindefininggrammarsforlanguagesandparsers(i.e.yourcompiler)

Page 5: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

5

C++Grammar

• Languageshaverulesgoverningtheirsyntaxandmeaning

• Theserulesarereferredtoasitsgrammar• Programminglanguagesalsohavegrammarsthatcodemustmeettobecompiled– Compilersusethisgrammartocheckforsyntaxandothercompile-timeerrors

– Grammarsoftenexpressedas“productions/rules”

• ANSICGrammarReference:– http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#declaration

Page 6: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

6

SimpleParagraphGrammarSubstitution Rule

subject "I"|"You"|"We"

verb "run"|"walk"|"exercise" |"eat"|"play"|"sleep"

sentence subject verb'.'

sentence_list sentence|sentence_list sentence

paragraph [TAB=\t]sentence_list [Newline=\n]

I run. You walk. We exercise.subject verb. subject verb.

subject verb.

sentence sentence sentencesentence_list sentence sentencesentence_list sentencesentence_listparagraph

Example: Example:I eat You sleepSubject verb subject verbError

Page 7: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

7

C++GrammarRule Expansion

expr constant|variable_id|function_call|assign_statement|‘(‘expr ‘)’|expr binary_op expr|unary_op expr

assign_statement variable_id ‘=‘expr

expr_statement ‘;’|expr ‘;’

5 * (9 + max);expr * ( expr + expr );expr * ( expr );expr * expr;expr;expr_statement

Example: Example: x + 9 = 5;expr + expr = expr;expr = expr;

NO SUBSTITUTIONCompile Error!

Page 8: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

8

C++GrammarRule Substitution

statement expr_statement|compound_statement|if(expr )statement|while(expr )statement…

compound_statement ‘{‘statement_list ‘}’

statement_list statement|statement_list statement

while(x > 0) { doit(); x = x-2; }while(expr) { expr; assign_statement; }while(expr) { expr; expr; }while(expr) { expr_statement expr_statement }while(expr) { statement statement }while(expr) { statement_list statement }while(expr) { statement_list }while(expr) compound_statementwhile(expr) statementstatement

Exam

ple: while(x > 0)

x--;x = x + 5;

while(expr) statementstatement

statementstatement

Exam

ple:

Page 9: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

9

MOREEXAMPLES

Page 10: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

10

Combinatorics Examples• Givennthings,howcanyouchoosekofthem?

– WrittenasC(n,k)

• Howdowesolvetheproblem?– Pickonepersonandsinglethemout

• GroupsthatcontainJoe=>C(n-1,k-1)• Groupsthatdon'tcontainJoe=>C(n-1,k)

– Totalnumberofsolutions:C(n-1,k-1)+C(n-1,k)– Whatarebasecases?

Joe

Page 11: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

11

Combinatorics Examples

• You'regoingtoDisneylandandyou'retryingtopick4peoplefromyourdormtogowithyou

• Givennthings,howcanyouchoosekofthem?– WrittenasC(n,k)– Analyticalsolution:C(n,k)=n!/[k!*(n-k)!]

• Howdowesolvetheproblem?

Page 12: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

12

RecursiveSolution

• Sometimesrecursioncanyieldanincrediblysimplesolutiontoaverycomplexproblem

• Needsomebasecases– C(n,0)=1– C(n,n)=1

int C(int n, int k){if(k == 0 || k == n)return 1;

elsereturn C(n-1,k-1) + C(n-1,k);

}

Page 13: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

13

LINKEDLISTS

Page 14: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

14

ArrayProblems• Onceallocatedanarraycannotgroworshrink• Ifwedon'tknowhowmanyitemswillbeaddedwecouldjustallocatean

arraylargerthanweneedbut…– Wemightwastespace– Whatifweendupneedingmore…wouldneedtoallocateanewarrayand

copyitems• Arrayscan'tgrowwiththeneedsoftheclient

30 51 52 53 540 1 2 3 4 5

106 7 8 9 10 11

30 51 52 53 540 1 2 3 4 5

10

21append(21) =>

Old, full array

Copy over items

0 1 2 3 4 5 6 7 8 9 10 11Allocate new array

30 51 52 53 540 1 2 3 4 5

106 7 8 9 10 11

Add new item 21

Page 15: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

15

MotivationforLinkedLists• Canwecreatealistimplementationthatcaneasilygrowor

shrinkbasedonthenumberofitemscurrentlyinthelist• Observation:ArraysareallocatedanddeallocatedinLARGE

chunks– Itwouldbegreatifwecouldallocate/deallocateatafinergranularity

• Linkedliststaketheapproachofallocatinginsmallchunks(usuallyenoughmemorytoholdoneitem)

Bulk Item(i.e. array)

Single Item(i.e. linked

list)

Page 16: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

16

LinkedList• Usestructures/classesandpointers

tomake‘linked’datastructures• AListis…

– Arbitrarilysizedcollectionofvalues

– Canaddanynumberofnewvaluesviadynamicmemoryallocation

– SupportstypicalListADToperations:

• Insert• Get• Remove• Size• Empty

• CandefineaListclass

#include<iostream>using namespace std;

struct Item {int val;Item* next;

};

class List{

public:List();~List();void push_back(int v); ...

private:Item* head_;

};

intval

Item*next

Item blueprint:

Rule of thumb: Still use ‘structs’ for objects that are purely collections of data and don’t really have

operations associated with them. Use ‘classes’ when data does have associated functions/methods.

val next

3 0x1c0

val next

9 0x1680x148head 0x148 0x1c0

val next

2 0x0(Null)

0x168

Page 17: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

17

Don'tNeedClasses• Wedon'thavetouse

classes…– Theclassjustactsasawrapper

aroundtheheadpointerandtheoperations

– Sowhileaclassisprobablythecorrectwaytogointermsoforganizingyourcode,fortodaywecanshowyoualessmodular,proceduralapproach

• Definefunctionsforeachoperationandpassittheheadpointerasanargument

#include<iostream>using namespace std;struct Item {

int val;Item* next;

};

void append(Item*& head, int v);bool empty(Item* head);int size(Item* head);

int main(){

Item* head1 = NULL;Item* head2 = NULL;int size1 = size(head1);bool empty2 = empty(head2);...

}

0x0

head_

intval

Item*next

Item blueprint:

class List:

Rule of thumb: Still use ‘structs’ for objects that are purely collections of data and don’t really have

operations associated with them. Use ‘classes’ when data does have associated functions/methods.

Page 18: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

18

LinkedListImplementation• Tomaintainalinkedlistyouneedonly

tokeeponedatavalue:head– Likeatrainengine,wecanattachany

numberof'cars'totheengine– Theenginelooksdifferentthanallthe

others• Inourlinkedlistit'sjustasinglepointer

toanItem• AllthecarsareItemstructs• Eachcarhasahitchforafollowingcar

(i.e.nextpointer)

Each car = "Item"

Engine = "head"

0x0NULL

head1

#include<iostream>using namespace std;struct Item {

int val;Item* next;

};

void append(Item*& head, int v);

int main(){

Item* head1 = NULL;Item* head2 = NULL;

}

Page 19: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

19

ACommonMisconception• ImportantNote:

– 'head'isNOTanItem,itisapointertothefirstitem

– Sometimesfolksgetconfusedandthinkheadisanitemandsotogetthelocationofthefirstitemtheywrite'head->next'

– Infact,'head->next'evaluatestothe2nditemsaddress

val next

3 0x1c0

val next

9 0x168

0x148

head

0x148 0x1c0

val next

2 0x0(Null)

0x168

Page 20: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

20

Append• Addinganitem(traincar)tothe

backcanbesplitinto2cases:– Attachingthecartotheengine(i.e.

thelistisemptyandwehavetochangetheheadpointer)

– Attachingthecartoanothercar(i.e.thelisthasotherItemsalready)andsoweupdatethenextpointerofanItem

val next

0x0

head1

0x148

3 NULL

0x148

#include<iostream>using namespace std;struct Item {

int val;Item* next;

};

void append(Item*& head, int v){

if(head == NULL){head = new Item;head->val = v; head->next = NULL;

}else {...}

}

int main(){

Item* head1 = NULL;Item* head2 = NULL;append(head1, 3)

}

Page 21: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

21

NULL

LinkedList• Addinganitem(traincar)tothe

backcanbesplitinto2cases:– Attachingthecartotheengine(i.e.

thelistisemptyandwehavetochangetheheadpointer)

– Attachingthecartoanothercar(i.e.thelisthasotherItemsalready)andsoweupdatethenextpointerofanItem

val next

3 0x1c0

val next

9 0x0NULL

0x148

head

0x148 0x1c0

#include<iostream>using namespace std;struct Item {

int val;Item* next;

};

void append(Item*& head, int v){

if(head == NULL){head = new Item;head->val = v; head->next = NULL;

}else {...}

}

int main(){

Item* head1 = NULL;Item* head2 = NULL;append(head1, 3)

}

Page 22: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

22

Append()• Lookathowtheheadparameteris

passed…Canyouexplainit?– Headmightneedtochangeifitisthe1st

itemthatweareadding– We'vepassedtheheadpointerBYVALUE

soifwemodify'head'inappend()we'llonlybemodifyingthecopy

– Weneedtopassthepointerbyreference– WechooseItem*&butwecouldalsopass

anItem**

val next

3 0x0NULL

0x0

head

0x148

void append(Item*& head, int v){

Item* newptr = new Item;newptr->val = v; newptr->next = NULL;

if(head == NULL){head = newptr;

}else {

Item* temp = head;// iterate to the end...

}}

0x148

void append(Item** head, int v){

Item* newptr = new Item;newptr->val = v; newptr->next = NULL;

if(*head == NULL){head = newptr;

}else {

Item* temp = head;// iterate to the end...

}}

0x200

Page 23: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

23

Arrays/LinkedListEfficiency• Arraysarecontiguouspiecesofmemory• Tofindasinglevalue,computeronlyneeds

– Thestartaddress• Rememberthenameofthearrayevaluatesto

thestartingaddress(e.g.data=120)– Whichelementwewant

• Providedasanindex(e.g.[20])

– Thisisallthankstothefactthatitemsarecontiguousinmemory

• Linkedlistitemsarenotcontiguous– Thus,linkedlistshaveanexplicitfieldto

indicatewherethenextitemis– Thisis"overhead"intermsofmemoryusage– Requiresiterationtofindanitemormoveto

theend

Memory

10045 31 21 04 98 73 …

104 108 112 116 120

data = 100

#include<iostream>using namespace std;

int main(){

int data[25];data[20] = 7;return 0;

}

val next

3 0x1c0

val next

9 0x168

0x148

head

0x148 0x1c0

val next

2 0x0(Null)

0x168

Page 24: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

24

Append()• Startfromheadanditeratetoendoflist– Allocatenewitemandfillitin– Copyheadtoatemppointer– Usetemppointertoiteratethrough

thelistuntilwefindthetail(elementwithnextfield=NULL)

– Updateoldtailitemtopointatnewtailitem

val next

3 0x1c0

val next

9 0x0NULL

0x148

head

0x148 0x1c0

val next

2 0x0(Null)

0x1680x168

0x148

temp

I don’t know where the list ends so I have to traverse it

0x1c0

temp

void append(Item*& head, int v){

Item* newptr = new Item;newptr->val = v; newptr->next = NULL;

if(head == NULL){head = newptr;

}else {

Item* temp = head;// iterate to the end...

}}

Page 25: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

25

IteratingOveraLinkedList• Toiterateweprobablyneedtocreateacopyoftheheadpointer(becauseifwemodify'head'we'llneverrememberwheretheliststarted

• Howdowetakeastep(advanceoneItem)giventhetemppointer– temp=temp->next;

val next

3 0x1c0

val next

9 0x0NULL

0x148head 0x148 0x1c0

0x148

temp

0x1c0

temp

void append(Item*& head, int v){

Item* newptr = new Item;newptr->val = v; newptr->next = NULL;

if(head == NULL){head = newptr;

}else {

Item* temp = head;while(temp->next){

temp = temp->next;}temp->next = newptr;

}}

Page 26: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

26

UsingaForloop

void append(Item*& head, int v){

Item* newptr = new Item;newptr->val = v; newptr->next = NULL;

if(listPtr == NULL){head = newptr;

}else {

Item* temp = head; // initwhile(temp->next){ // condition

temp = temp->next; // update}temp->next = newptr;

}}

void append(Item*& head, int v){

Item* newptr = new Item;newptr->val = v; newptr->next = NULL;

if(listPtr == NULL){head = newptr;

}else {

Item* temp;for(temp = head; // init

temp->next; // conditiontemp = temp->next); // update

temp->next = newptr;}

}

Page 27: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

27

PrintingOutEachItem

void print(Item* head){

Item* temp = head; // initwhile(temp) { // condition

cout << temp->val << endl;temp = temp->next; // update

}}

void print(Item* head){

Item* temp;for(temp = head; // init

temp; // conditiontemp = temp->next){ // update

cout << temp->val << endl;}

}

Page 28: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

28

RECURSION&LINKEDLISTS

Page 29: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

29

RecursionandLinkedLists

• NoticethatoneItem'snextpointerlookslikeaheadpointertotheremainderofthelinkedlist– Ifwehaveafunctionthatprocessesalinkedlistbyreceivingtheheadpointerasaparameterwecanrecursivelycallthatfunctionbypassingour'next'pointerasthe'head'

val next

3 0x1c0

val next

9 0x168

0x148

0x148 0x1c0

val next

2 0x0(Null)

0x168

head

0x1c0head2

Page 30: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

30

RecursiveOperationsonLinkedList• Manylinkedlistoperationscanberecursivelydefined• Canwemakearecursiveiterationfunctiontoprintitems?

– Recursivecase:Printoneitemthentheproblembecomestoprintthen-1otheritems.• Noticethatany'next'pointercanbethoughofasa'head'pointertotheremainingsublist

– Basecase:Emptylist(i.e.Nullpointer)

• Howcouldyouprintvaluesinreverseorder?

void print(Item* ptr){

if(ptr == NULL) return;else {

cout << ptr->val << endl;print(ptr->next);

}}int main(){ Item* head;

...print(head);

}

val next

3 0x1c0

val next

9 0x0NULL

0x148head 0x148 0x1c0

main0x148 head0xbf8

00400120 Return link

0xbfc

0x148 ptr0xbf0

004001844 Return link

0xbf4print

0x1c0 ptr0xbe8

004001844 Return link

0xbecprint

0x0 ptr0xbe8

004001844 Return link

0xbecprint

Page 31: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

31

SummingtheValues• Writearecursiveroutinetosumthevaluesofalinkedlist– HeadRecursion(recurse first,doworkonthewaybackup)

– TailRecursion(doworkonthewaydown,thenrecurse)

val next

3 0x1c0

val next

9 0x168

0x148

0x148 0x1c0

val next

2 0x0(Null)

0x168

Page 32: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

32

HeadRecursion• Recurse totheendofthechain(head==NULL)andthenstart

summingonthewaybackup– Whatshouldthebasecasereturn– Whatshouldrecursivecases(normalnodes)return?

val next

3 0x1c0

val next

9 0x168

0x148

0x148 0x1c0

val next

2 0x0(Null)

0x168

head

sum(0x148) sum(0x1c0) sum(0x168) sum(0x0)

00+2=22+9=113+11=14

Main()

Page 33: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

33

TailRecursion• Producesumasyouwalkdownthelistthenjustreturnthefinalanswerbackupthelist

val next

3 0x1c0

val next

9 0x168

0x148

0x148 0x1c0

val next

2 0x0(Null)

0x168

head

sum(0x148) sum(0x1c0) sum(0x168) sum(0x0)

14141414

Main()

3 12 14

Page 34: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

34

Exercises

• llsum_head• llsum_tail

http://bits.usc.edu/cs104/exercises.html

Page 35: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

35

RecursiveCopy• Howcouldyoumakeacopyofalinkedlistusingrecursion

struct Item {int val; Item* next;Item(int v, Item* n){

val = v; next = n;}

};

Item* copyLL(Item* head){

if(head == NULL) return NULL;else {

return new Item(head->val,copyLL(head->next));

}}int main(){ Item* oldhead, *newhead;

...newhead = copyLL(oldhead);

}

val next

3 0x1c0

val next

9 0x0NULL

0x148oldhead

0x148 0x1c0

val next

3 0x7c0

val next

9 0x0NULL

0x7c0

0x840

newhead

copyLL(0x148) copyLL(0x1c0) copyLL(0x0)

0x00x7c0

0x840

Page 36: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

36

INCREASINGEFFICIENCYOFOPERATIONS+DOUBLYLINKEDLISTS

Page 37: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

37

AddingaTailPointer• Ifinadditiontomaintainingahead

pointerwecanalsomaintainatailpointer

• Atailpointersavesusfromiteratingtotheendtoaddanewitem

• Needtoupdatethetailpointerwhen…– Weaddanitemtotheend(fast)– Weremoveanitemfromtheend

(slow)

val next

2 0x0(Null)

0x1680x168

val next

3 0x1c0

val next

9 NULL

0x148

head

0x148 0x1c0

0x1c0

tail

0x168

tail

Page 38: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

38

Removal

• Toremovethelastitem,weneedtoupdatethe2ndtolastitem(setit'snextpointertoNULL)

• Wealsoneedtoupdatethetailpointer• Butthiswouldrequireustotraversethefulllist• ONESOLUTION:doubly-linkedlist

val next

5 0x1c0

val next

9 NULL

0x200 0x1c0

0x1c0

tail

val next

3 0x2000x148

head0x148 …

Page 39: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

39

Doubly-LinkedLists• Includesapreviouspointer

ineachitemsothatwecantraverse/iteratebackwardsorforward

• Firstitem'spreviousfieldshouldbeNULL

• Lastitem'snextfieldshouldbeNULL

#include<iostream>

using namespace std;struct DLItem {

int val;DLItem* prev;DLItem* next;

};

int main(){

DLItem* head, *tail;};

intval

DLItem *next

struct Item blueprint:DLItem *

prev

0x148

head

3 0x1c0NULL

val nextprev

9 0x2100x148

val nextprev

0x148 0x1c0

6 NULL0x1c0

val nextprev

0x210

0x210

tail

Page 40: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

40

Doubly-LinkedListAddFront• Addingtothefrontrequiresyoutoupdate…• …Answer

– Head– Newfront'snext&previous– Oldfront'sprevious

0x148

head

3 0x1c0NULL

val nextprev

9 0x2100x148

val nextprev

0x148 0x1c0

6 NULL0x1c0

val nextprev

0x210

12

val nextprev

0x190

Page 41: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

41

Doubly-LinkedListAddFront• Addingtothefrontrequiresyoutoupdate…

– Head– Newfront'snext&previous– Oldfront'sprevious

0x148

head

3 0x1c00x190

val nextprev

9 0x2100x148

val nextprev

0x148 0x1c0

6 NULL0x1c0

val nextprev

0x210

12 0x148NULL

val nextprev

0x190

Page 42: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

42

Doubly-LinkedListAddMiddle• Addingtothemiddlerequiresyoutoupdate…

– Previousitem'snextfield– Nextitem'spreviousfield– Newitem'snextfield– Newitem'spreviousfield

0x148

head

3 0x1c0NULL

val nextprev

9 0x2100x148

val nextprev

0x148 0x1c0

6 NULL0x1c0

val nextprev

0x210

12

val nextprev

0x190

Page 43: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

43

Doubly-LinkedListAddMiddle• Addingtothemiddlerequiresyoutoupdate…

– Previousitem'snextfield– Nextitem'spreviousfield– Newitem'snextfield– Newitem'spreviousfield

0x148

head

3 0x1c0NULL

val nextprev

9 0x1900x148

val nextprev

0x148 0x1c0

6 NULL0x190

val nextprev

0x210

12 0x2100x1c0

val nextprev

0x190

Page 44: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

44

Doubly-LinkedListRemoveMiddle

• Removingfromthemiddlerequiresyoutoupdate…– Previousitem'snextfield– Nextitem'spreviousfield– Deletetheitemobject

0x148

head

3 0x1c0NULL

val nextprev

9 0x2100x148

val nextprev

0x148 0x1c0

6 NULL0x1c0

val nextprev

0x210

Page 45: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

45

Doubly-LinkedListRemoveMiddle

• Removingfromthemiddlerequiresyoutoupdate…– Previousitem'snextfield– Nextitem'spreviousfield– Deletetheitemobject

0x148

head

3 0x210NULL

val nextprev

9 0x2100x148

val nextprev

0x148

0x1c0

6 NULL0x148

val nextprev

0x210

Page 46: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

46

ABSTRACTDATATYPE(ADT)

Page 47: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

47

AbstractDataTypes• DAPSdefinesanabstractdatatype,orADT,as:

– Specification/modelforagroupofvalues/dataandtheoperationsonthosevalues

• Themodelallowsustoseparate…– Thedecisionofwhatdatastructuretouseandhowitwillbeusedinour

higherlevelapplication– Andtheimplementationofthespecificdatastructure

• DAPSdefinesadatastructureas:– AnimplementationofanADTinagivenprogramminglanguage

• EachADTwewillexamineinthiscoursehascertain:– Welldefinedoperationsandcapabilitiesthatareoftenuseful– Time&spaceadvantages– Time&spacedisadvantages

• Youneedtoknowthoseoperations,advantagesanddisadvantages

Data Abstraction & Problem Solving with C++, Carrano and Henry will henceforthbe abbreviated as DAPS

Page 48: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

48

3PopularADTs

• List• Dictionary/Map• Set• (Possible4th:PriorityQueue)

Page 49: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

49

Lists• Orderedcollectionofitems,whichmaycontainduplicate

values,usuallyaccessedbasedontheirposition(index)– Ordered=Eachitemhasanindexandthereisafrontandback(start

andend)– Duplicatesallowed(i.e.inalistofintegers,thevalue0couldappear

multipletimes)– Accessedbasedontheirposition(list[0],list[1],etc.)

• Whataresomeoperationsyouperformonalist?

list[0]list[1]

list[2]

Page 50: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

50

ListOperationsOperation Description Input(s) Output(s)

insert Addanewvalueataparticularlocationshiftingothersback

Index :intValue

remove Remove valueatthegivenlocation Index:int Valueatlocation

get/at Getvalueatgivenlocation Index:int Valueatlocation

set Changes thevalueatagivenlocation Index :intValue

empty Returns trueiftherearenovaluesinthelist

bool

size Returnsthenumberofvaluesinthelist

int

push_back /append

Addanew valuetotheendofthelist Value

find Returnthe locationofagivenvalue Value Int :Index

Page 51: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

51

Maps/Dictionaries• Storeskey,value pairs

– Example:MapstudentnamestotheirGPA

• Keysmustbeunique(canonlyoccuronceinthestructure)

• Noconstraintsonthevalues• Whatoperationsdoyouperformona

map/dictionary?• Noinherentorderingbetween

key,value pairs– Can'taskforthe0th item…

"Tommy Trojan"

3.7

"BillyBruin"

2.5

"HarryHarvard"

4.3

"Dale Duck"

2.5

Page 52: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

52

Map/DictionaryOperationsOperation Description Input(s) Output(s)

Insert/add Addanew key,value pairtothedictionary(assumingitsnottherealready)

Key,Value

Remove Removethekey,value pairwiththegivenkey

Key

Get/lookup Lookup thevalueassociatedwiththegivenkeyorindicatethekey,valuepairdoesn'texist

Key Valueassociatedwiththekey

In/Find Checkifthegivenkeyispresentinthemap

Key bool(orptr topair/NULL)

empty Returns trueiftherearenovaluesinthelist

bool

size Returnsthenumberofvaluesinthelist

int

Page 53: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

53

Set• Asetisadictionarywhereweonlystorekeys(noassociated

values)– Example:AllthecoursestaughtatUSC(ARLT100,…,CSCI104,MATH

226,…)

• Items(a.k.a.Keys)mustbeunique– Noduplicatekeys(onlyoneoccurrence)

• Notaccessedbasedonindexbutonvalue– Wewouldn'tsay,"Whatisthe0th courseatUSC?"

• InDAPStextbookChapter1,thisisthe'bag'ADT

• Whatoperationsdoweperformonaset?

EE101

ARLT 100

CSCI 104

MATH226

Page 54: L03 Recursion LinkedList ADT - Rafael Ferreira da Silva, Ph.D. · 2021. 3. 1. · Arrays/Linked List Efficiency • Arrays are contiguous pieces of memory • To find a single value,

54

SetOperationsOperation Description Input(s) Output(s)

Insert/add Addanew keytotheset(assumingitsnottherealready)

Key

Remove Remove Key

In/Find Checkifthegivenkeyispresentinthemap

Key bool(orptr toitem/NULL)

empty Returns trueiftherearenovaluesinthelist

bool

size Returnsthenumberofvaluesinthelist Int

intersection Returnsanewsetwiththecommonelementsofthetwoinputsets

Set1,Set2 Newsetwithall elementsthatappearinbothset1andset2

union Returnsanewsetwithalltheitemsthatappearineitherset

Set1,Set2 Newsetwithall elementsthatappearineitherset1andset2

difference Returnsasetwith allitemsthatarejustinset1butnotset2

Set1, Set2 Newsetwithonly theitemsinset1thatarenotinset2