Microsoft PowerPoint – 12_LinkedLists.pptx
1
CS 2211
Systems Programming
Part Eleven:
Linked Lists
1
Linked List
.
Node object
Nodes in Singly Linked Lists • Nodes for our linked lists will be objects • Each Node object in a singly linked list will Node* nextItem value Linked List . Node objects Data objects can be simple data (int, double, etc) or 1 2 3 4 2 typedef struct fraction { } fract; MEMORY MAPPING – STRUCTURE 397 – 398 – 399 – POINTERS and STRUCTURES and LINKS to DYNAMICALLY allocate memory. Address Label Address Value typedef struct fraction { MEMORY MAPPING – STRUCTURE 397 – 398 – 399 – POINTERS and STRUCTURES and LINKS to DYNAMICALLY allocate memory. Address Label Address Value typedef struct fraction { fract first *pNew; MEMORY MAPPING – STRUCTURE 397 – 398 – 399 – POINTERS and STRUCTURES and LINKS to DYNAMICALLY allocate memory. Address Label Address Value first.wP 400 – 403 typedef struct fraction { fract first, *pNew; first.wp = 7; MEMORY MAPPING – STRUCTURE 397 – 398 – 399 – POINTERS and STRUCTURES and LINKS to DYNAMICALLY allocate memory. Address Label Address Value first.wP 400 – 403 7 5 6 7 8 3 MEMORY MAPPING – STRUCTURE 397 – 398 – 399 – POINTERS and STRUCTURES and LINKS to DYNAMICALLY allocate memory. Address Label Address Value first.wP 400 – 403 7 typedef struct fraction { fract first, *pNew; first.wp = 7; pNew = (fract*)malloc(sizeof (fract)); 397 – 398 – 399 – typedef struct fraction { fract first, *pNew; first.wp = 7; pNew = (fract*)malloc(sizeof (fract)); pNew->wP = 56; MEMORY MAPPING – STRUCTURE POINTERS and STRUCTURES and LINKS to DYNAMICALLY allocate memory. Address Label Address Value first.wP 400 – 403 7 397 – 398 – 399 – typedef struct fraction { fract first, *pNew; first.wp = 7; pNew = (fract*)malloc(sizeof (fract)); pNew->wP = 56; first.link = MEMORY MAPPING – STRUCTURE POINTERS and STRUCTURES and LINKS to DYNAMICALLY allocate memory. Address Label Address Value first.wP 400 – 403 7 397 – 398 – 399 – MEMORY MAPPING – STRUCTURE POINTERS and STRUCTURES and LINKS to DYNAMICALLY allocate memory. Address Label Address Value first.wP 400 – 403 7 typedef struct fraction { fract first, *pNew; first.wp = 7; pNew = (fract*)malloc(sizeof (fract)); pNew->wP = 56; first.link = pNew; 9 10 11 12 4 397 – 398 – 399 – MEMORY MAPPING – STRUCTURE POINTERS and STRUCTURES and LINKS to DYNAMICALLY allocate memory. Address Label Address Value first.wP 400 – 403 7 … pNew->wP = 56; first.link = pNew; . 7 56 [ 400 ] [ 10100 ] [ 10100 ] link 397 – 398 – 399 – MEMORY MAPPING – STRUCTURE better use of pointer variables in a link Address Label Address Value first.wP 400 – 403 7 typedef struct fraction { fract first, *pTop, *pNew; first.wp = 7; pTop = &first; pNew = (fract*)malloc(sizeof (fract)); pNew->wP = 56; pTop->link = pNew; 15 Linked Lists in C END OF PART 1 13 14 15
dynamically created or deleted in the HEAP
contain two member variables:
complex data (arrays, structures or unions ) or
pointers to data outside each individual nodes
int wP;
int fP;
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
400 – 401 – 402 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
403 – 404 – 405 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
406 – 407 – 408 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
409 – 410 – 411 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
412 – 413 – 414 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
415 – 416 – 417 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
418 – 419 – 420 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
421 – 422 – 423 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
– a pointer variable can also be used
Label
int wP;
int fP;
struct fraction* link;
} fract;
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
400 – 401 – 402 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
403 – 404 – 405 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
406 – 407 – 408 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
409 – 410 – 411 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
412 – 413 – 414 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
415 – 416 – 417 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
418 – 419 – 420 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
421 – 422 – 423 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
– a pointer variable can also be used
Label
int wP;
int fP;
struct fraction* link;
} fract;
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
400 – 401 – 402 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
403 – 404 – 405 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
406 – 407 – 408 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
409 – 410 – 411 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
412 – 413 – 414 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
415 – 416 – 417 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
418 – 419 – 420 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
421 – 422 – 423 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
– a pointer variable can also be used
Label
first.fP 404 – 407
first.link 408 – 411
pNew 412 – 415
int wP;
int fP;
struct fraction* link;
} fract;
first.fP = 3;
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
400 – 401 – 402 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
403 – 404 – 405 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
406 – 407 – 408 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
409 – 410 – 411 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
412 – 413 – 414 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
415 – 416 – 417 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
418 – 419 – 420 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
421 – 422 – 423 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
– a pointer variable can also be used
Label
first.fP 404 – 407 3
first.link 408 – 411
pNew 412 – 415
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
400 – 401 – 402 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
403 – 404 – 405 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
406 – 407 – 408 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
409 – 410 – 411 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
412 – 413 – 414 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
415 – 416 – 417 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
418 – 419 – 420 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
421 – 422 – 423 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
– a pointer variable can also be used
Label
first.fP 404 – 407 3
first.link 408 – 411
pNew 412 – 415 10100
{DM} 10100 – 10111
int wP;
int fP;
struct fraction* link;
} fract;
first.fP = 3;
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
400 – 401 – 402 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
403 – 404 – 405 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
406 – 407 – 408 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
409 – 410 – 411 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
412 – 413 – 414 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
415 – 416 – 417 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
418 – 419 – 420 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
421 – 422 – 423 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
int wP;
int fP;
struct fraction* link;
} fract;
first.fP = 3;
pNew->fP = 92;
– a pointer variable can also be used
Label
first.fP 404 – 407 3
first.link 408 – 411
pNew 412 – 415 10100
pNew->wP 10100 – 10103 56
pNew->fP 10104 – 10107 92
{DM} 10108 – 10111
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
400 – 401 – 402 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
403 – 404 – 405 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
406 – 407 – 408 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
409 – 410 – 411 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
412 – 413 – 414 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
415 – 416 – 417 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
418 – 419 – 420 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
421 – 422 – 423 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
int wP;
int fP;
struct fraction* link;
} fract;
first.fP = 3;
pNew->fP = 92;
pNew;
– a pointer variable can also be used
Label
first.fP 404 – 407 3
first.link 408 – 411 10100
pNew 412 – 415 10100
pNew->wP 10100 – 10103 56
pNew->fP 10104 – 10107 92
{DM} 10108 – 10111
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
400 – 401 – 402 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
403 – 404 – 405 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
406 – 407 – 408 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
409 – 410 – 411 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
412 – 413 – 414 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
415 – 416 – 417 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
418 – 419 – 420 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
421 – 422 – 423 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
– a pointer variable can also be used
Label
first.fP 404 – 407 3
first.link 408 – 411 10100
pNew 412 – 415 10100
pNew->x 10100 – 10103 56
pNew->y 10104 – 10107 92
{DM} 10108 – 10111
int wP;
int fP;
struct fraction* link;
} fract;
first.fP = 3;
pNew->fP = 92;
printf( “\nwP: %d, fP: %d\n” , first.link->wP , first.link->fP ) ;
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
400 – 401 – 402 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
403 – 404 – 405 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
406 – 407 – 408 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
409 – 410 – 411 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
412 – 413 – 414 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
415 – 416 – 417 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
418 – 419 – 420 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
421 – 422 – 423 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
– a pointer variable can also be used
Label
first.fP 404 – 407 3
first.link 408 – 411 10100
pNew 412 – 415 10100
pNew->wP 10100 – 10103 56
pNew->fP 10104 – 10107 92
{DM} 10108 – 10111
pNew = (fract*)malloc(sizeof (fract));
pNew->fP = 92;
printf( “\nwP: %d, fP: %d\n” , first.link->wP , first.link->fP ) ;
3
92
link
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
400 – 401 – 402 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
403 – 404 – 405 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
406 – 407 – 408 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
409 – 410 – 411 –
0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1 1
412 – 413 – 414 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
415 – 416 – 417 –
1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
418 – 419 – 420 –
0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 1 0
421 – 422 – 423 –
1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 1 0 1
Label
first.fP 404 – 407 3
first.link 408 – 411 10100
pNew 412 – 415 10100
pNew->x 10100 – 10103 56
pNew->y 10104 – 10107 92
{DM} 10108 – 10111
int wP;
int fP;
struct fraction* link;
} fract;
first.fP = 3;
pNew->fP = 92;
printf( “\nwP: %d, fP: %d\n”, pTop->link->wP , pTop->link->fP ) ;