+ All Categories
Home > Documents > Main Index Contents 11 Main Index Contents Abstract Model of a List Obj. Abstract Model of a List...

Main Index Contents 11 Main Index Contents Abstract Model of a List Obj. Abstract Model of a List...

Date post: 21-Dec-2015
Category:
View: 231 times
Download: 2 times
Share this document with a friend
Popular Tags:
21
1 Main Index Conten ts 1 Main Index Conten ts Abstract Model of a Lis t Obj . Insertion into a List Linked List (LL) nodes (2 slides) (2 slides) Node Composition Inserting at the Front of a LL Deleting from the Front of a LL Removing a Target Node Chapter 9 Chapter 9 Linked Lists Linked Lists Handling the Back of th e List Designing a New LL Stru cture Inserting a Node at a P osition (2 (2 slides) slides) Circular Doubly LL’s (2 slides) Updating a Doubly LL Summary Slides (5 slides)
Transcript

1 Main IndexMain Index ContentsContents1 Main IndexMain Index ContentsContents

Abstract Model of a List Obj.

Insertion into a List

Linked List (LL) nodes (2 slides) (2 slides)

Node Composition

Inserting at the Front of a LL

Deleting from the Front of a LL

Removing a Target Node

Chapter 9 Chapter 9 – – Linked ListsLinked Lists

Handling the Back of the List

Designing a New LL Structure

Inserting a Node at a Position (2 (2

slides)slides)

Circular Doubly LL’s (2 slides)

Updating a Doubly LL

Summary Slides (5 slides)

2 Main IndexMain Index ContentsContents

Abstract Model of a List ObjectAbstract Model of a List Object

fro n t b ack

3 Main IndexMain Index ContentsContents3 Main IndexMain Index ContentsContents

Insertion Into a List by Shifting Insertion Into a List by Shifting Vector Storage ElementsVector Storage Elements

2 7 3 1 0

In s ert 5 in t o lis t 2 7 3 1 0 at t h e 3 rd p o s it io nIm p lem en t at io n s h ift s lis t Vect o r[2 ] an d lis t Vect o r[3 ]

2 7 5 3 1 0

R es u lt in g lis t 2 7 5 3 1 0Im p lem en t at io n as s ign s 5 t o lis t Vect o r[2 ]

4 Main IndexMain Index ContentsContents4 Main IndexMain Index ContentsContents

Linked List NodesLinked List Nodes Each Node is like a piece of a chain

In d iv id u al P iece P o p C h ain

To insert a new link, break the chain at the desired location and simply reconnect at both ends of the new piece.

D is c o nnec t

R ec o nnec t

5 Main IndexMain Index ContentsContents5 Main IndexMain Index ContentsContents

Linked List NodesLinked List Nodes Removal is like Insertion in reverse.

D is co n n ect

R eco n n ect

6 Main IndexMain Index ContentsContents6 Main IndexMain Index ContentsContents

Node CompositionNode Composition

n o de Va lue

n ext

n o d eValu e n ext

An individual Node is composed of two parts, a Data field containing the data stored by the node, and a Pointer field that marks the address of the next Node in the list.

7 Main IndexMain Index ContentsContents7 Main IndexMain Index ContentsContents

Inserting at the Front of a Linked Inserting at the Front of a Linked ListList

fro n t

fro n t

B efo re

(a)

it em

n ew N o d e

A ft er

b ack

(b )

2 0

fro n t

B efo re

fro n t

5 5

2 0it em

fro n t

A ft er

fro n t

5 5

8 Main IndexMain Index ContentsContents8 Main IndexMain Index ContentsContents

Deleting From the Front of a Deleting From the Front of a Linked ListLinked Listfro n t

fro n t

//

fro n t = N U LL

D elet in g fro n t o f a 1 -n o d e lis t

D elet in g fro n t o f a m u lt i-n o d e lis t

//

fro n t = fro n t -> n ext

9 Main IndexMain Index ContentsContents9 Main IndexMain Index ContentsContents

Removing a Target NodeRemoving a Target Node

ne xt

f ro nt

targe t

pre v c ur r

// //

10 Main IndexMain Index ContentsContents10 Main IndexMain Index ContentsContents

Handling the Back of the ListHandling the Back of the List

D

C

B

to p

A

Stac k

D ABC

L inke d L is t

f ro nt

11 Main IndexMain Index ContentsContents11 Main IndexMain Index ContentsContents

Designing a New Linked List Designing a New Linked List StructureStructure

... //

i te m

ne wN o de

f ro nt

bac k

//

12 Main IndexMain Index ContentsContents12 Main IndexMain Index ContentsContents

Inserting a Node at a PositionInserting a Node at a Position

ne xt pre v

pre vN ode = c urr -> pre v

p r ev item n ex t

143 2 c ur r

n ew N o d e

13 Main IndexMain Index ContentsContents13 Main IndexMain Index ContentsContents

Inserting a Node at a PositionInserting a Node at a Position

pre v ne xt

s uc c N o de = c ur r->ne xt

1

2

//////

//

pre vN o de = c ur r->pre v c ur r

14 Main IndexMain Index ContentsContents14 Main IndexMain Index ContentsContents

Circular Doubly Linked ListsCircular Doubly Linked Lists A Watch Band provides a good Real Life

analogue for this Data Structure

Firs t Ele m e n t

S e co n d Ele m e n t

L a s t Ele m e n t

15 Main IndexMain Index ContentsContents15 Main IndexMain Index ContentsContents

Circular Doubly Linked ListsCircular Doubly Linked Lists Implemented on a Computer it might look

something like this.

head er

23 4 9

16 Main IndexMain Index ContentsContents

Updating a Doubly Linked ListUpdating a Doubly Linked List

p r e v n e x t

h ead er

B efo re In s ert : E m p t y lis t

p r e v n e x t

h ead er

n ew N o d e

A ft er Iin s ert : L is t w it h o n e elem en t

17 Main IndexMain Index ContentsContents17 Main IndexMain Index ContentsContents

Summary Slide 1Summary Slide 1

§- singly linked lists

- Each node contains a value and a pointer to the next node in the list.

- The list begins with a pointer to the first node of the list and terminates when a node has a NULL pointer.

18 Main IndexMain Index ContentsContents18 Main IndexMain Index ContentsContents

Summary Slide 2Summary Slide 2

§- Inserting/Deleting at the front of a singly linked list

1)Insert

- Set the pointer in the new node to the previous value of front.

- update front to point at the new node.

2)Erase

- assign front the pointer value of the first node, and then delete the node.

19 Main IndexMain Index ContentsContents19 Main IndexMain Index ContentsContents

Summary Slide 3Summary Slide 3

§- Inserting/Erasing inside a singly linked list- Maintain a pointer to the current list node and a

pointer to the previous node.

- Change the pointer value in the previous node.

20 Main IndexMain Index ContentsContents20 Main IndexMain Index ContentsContents

Summary Slide 4Summary Slide 4

§- Insert/Delete at the back of a singly linked list

- Maintain a pointer to the last list node that has value NULL when the list is empty.

- Assign a “back” pointer the address of the first node added to the list.

- To add other nodes at the back:1) allocate a new node

2) assign the pointer in node “back” to point to the new node

3) assign the pointer “back” the address of the new node.

21 Main IndexMain Index ContentsContents21 Main IndexMain Index ContentsContents

Summary Slide 5Summary Slide 5

§- Doubly linked lists- provide the most flexible implementation for the

sequential list.

§- Its nodes have pointers to the next and the previous node, so the program can traverse a list in either the forward or backward direction.

- Traverse a list by starting at the first node and follow the sequence of next nodes until you

arrive back at the header.- To traverse a list in reverse order, start at the last

node and follow the sequence of previous nodes until arriving back at the header.


Recommended