/
Linked Lists Motivation A Linked Lists Motivation A

Linked Lists Motivation A - PowerPoint Presentation

phoebe-click
phoebe-click . @phoebe-click
Follow
344 views
Uploaded On 2019-06-29

Linked Lists Motivation A - PPT Presentation

list data structure that uses only the amount of memory needed for the number of elements in use at a given point in time Compared to Arrays Arrays easily allocateddeallocated direct access accessing element ID: 760796

head node null list node head list null remove llist insert pointer element key return empty points void allocated

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Linked Lists Motivation A" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Linked Lists

Slide2

Motivation

A

list

data structure that uses only the amount of memory needed for the number of elements in use

at a given point in time.

Slide3

Compared to Arrays

Arrays:

- easily allocated/deallocated

- direct access (accessing element

n

is done simply by indexing: [n])

Linked Lists:

- no maximum number of elements

- no waste of memory for unused

elements.

Slide4

Node

A

Node

is a dynamically allocated li

st

element

, implemented as a structure that has at least two members:

- key:

a (usually) unique identifier

for an element.

- next:

a pointer to a node.

Slide5

Definition

A

Linked List

is a linear, dynamicall

y-

allocated data structure. It consists of a

head

which is a pointer to a node.

- The

head

points to the first element of the list; or is

NULL

when the list is empty.

- The

next

pointer of each node points to the next node in the list; except the last node, where

next

is

NULL

.

Slide6

Examples:

Slide7

Class Definitions

class node {

friend class

LList

;

private:

int

key;

node * next;

// circular reference OK

};

class

LList

{

private: node * head;

};

Slide8

Algorithms

When designing a function that

operates on a Linked List, consider

if the solution works for:

- an empty list

- a list of one node

- a list of two or more nodes

Slide9

Algorithms

"The Loop that solves all problems!"

(well, most anyway)

//

point p to the first/head node

node * p = head;

while (p != NULL) {

// process this node

...

p = p->next;

// go to next node

}

// or NULL

Slide10

Constructors

#include <

iostream

>

n

ode::node() {

key = 0;

next = NULL;

}

LList

::

LList

() {

head = NULL;

}

Slide11

Print

void

LList

::print() {

node * p = head;

while (p != NULL) {

cout

<< p->key << " ";

p = p-> next;

}

}

Slide12

Search

// return

ptr

to found node or

// NULL = not found

node *

LList

::search(

int

srchKey

) {

node * p = head;

while (p != NULL) {

if (p->key ==

srchKey

)

return p;

p = p->next;

}

return NULL;

//looked at all, not found

}

Slide13

Insert

A new node should first be created and populated:

void

LList

::insert(

int

newkey

) {

node * n = new node;

//allocate new node

n->key =

newkey

;

n->next = NULL;

insert

(n);

// some insertion method

}

// given pointer to new node

Slide14

Insert (After)

Given:

-

after:

a pointer to a node in the List,

after which the new node will be

inserted. NULL to insert new head.

-

n:

a pointer to the new node, already

allocated and populated.

Slide15

Insert

General Case: Blue arrows/null showvalues before insertion.Red arrows show links after insertion (changes)

Slide16

Insert

New Head Case: red arrows show links after changes.Empty List Case:the same code willwork when head is NULL

Slide17

Insert

void

LList

::

insertAfter

(node*

after,

node*

n

) {

if (head

== NULL || after == NULL) {

n-

>next =

head;

// insert as new

head

= n

;

// head of list

}

else {

// insert middle/tail of list

n-

>next

= after-

>next;

after-

>next

= n

;

}

// note: ORDER of statements is IMPORTANT!

}

Slide18

Insert

Checks:

- Does it work for Empty List? [

head

is NULL]

- Does it work for a List of 1 element?

- as the new head? [

after

is NULL]

- after the current head? [

after

==

head

]

- Does it work for a list of 2 or more elements?

- as the new head? [

after

is NULL]

- as the new tail? (last in list) [

after

points to

tail

]

- in the middle of the list?

Slide19

Remove

Given:

r

- a

pointer to a node in the

List to be

removed

Removes the node from the list, but does not

deallocate it.

Alternative:

Removes and deallocates the node.

Slide20

Remove

Remove Head case:

Slide21

Remove

General Case:Use b4 tofind apointer to the nodebeforethe one toremove

Slide22

Remove

void

LList::remove(node *

r) {

if

(r ==

head)

head

=

head-

>next;

else {

// must find node BEFORE one being removed

node * b4

=

head

;

while

(b4->next != r)

{ b4

= b4->next

; }

b4-

>next = r->next;

}

r->next = NULL

; // detach following node

}

Slide23

Remove

Checks:

- Assumption: never invoked on Empty List

- Assumption: r points to a node

actually

in the List.

- Does it work for a List of 1 element?

- Does it work for a list of 2 or more elements?

- removing the head?

- removing the tail?

- removing a node in the middle of the list?

Slide24

Remove - Defensive Version

void LList::

remove(node *

r)

{

if (head == NULL) return;

// list empty, can't remove

if (r == NULL) return;

// nothing to remove!?

if

(r ==

x.head

)

x.head

=

x.head

->next;

else { // must find node BEFORE one being removed

node * b4

=

x.head

;

while

(b4->next !=

r

&& b4 != NULL

)

{ b4

= b4->next

; }

if (b4 == NULL) return;

// node not in list!!

b4-

>next = r->next;

}

r->next = NULL

; }

Slide25

Remove

Note:

r

still points to the removed node

after remove() is finished.

The function invoking

remove()

may choose

to deallocate it or not.

Slide26

Remove - invocation

void

LList::fun() {

...

node * p;

p =

ptr to node to remove

;

remove(p

);

cout <<

p-

>key

<<

" was removed";

delete p

;

// deallocate removed node

...

}

Slide27

Length

// return: count of the nodes in the

list

void

LList

::length()

{

int num = 0;

node * p = head;

while (p != NULL) {

num

++;

p = p->next;

}

return num;

}

Slide28

Vocabulary

Term

Definition

Node

A dynamically-allocated li

st

element

that has at least two members:

- key:

a (usually) unique identifier

for an element.

- next:

a pointer to a node.

Linked List

A linear, dynamically-allocated list data structure.

It consists of a

head

which is a pointer to a node,

and the list of nodes.

- The

head

points to the first element of the list;

or is NULL when the list is empty.

- The

next

pointer of each node points to the next node in

the list; except the last node, where

next

is NULL.