Abstract Model of a List Obj' - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Abstract Model of a List Obj'

Description:

2) Erase - assign front the pointer value of the first node, and then delete the node. ... Inserting/Erasing inside a singly linked list ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 28
Provided by: alan325
Category:
Tags: abstract | erasing | list | model | obj

less

Transcript and Presenter's Notes

Title: Abstract Model of a List Obj'


1
Chapter 9 Linked Lists
Abstract Model of a List Obj. Insertion into a
List Linked List (LL) nodes (2 slides) Node
Composition Inserting at the Front of a
LL Deleting from the Front of a LL Removing a
Target Node
Handling the Back of the List Designing a New
LL Structure Inserting a Node at a Position
(2 slides) Circular Doubly LLs (2
slides) Updating a Doubly LL Summary Slides (5
slides)
2
Abstract Model of a List Object
3
Insertion Into a List by Shifting Vector Storage
Elements
4
Linked List Nodes
  • Each Node is like a piece of a chain
  • To insert a new link, break the chain at the
    desired location and simply reconnect at both
    ends of the new piece.

5
Linked List Nodes
  • Removal is like Insertion in reverse.

6
Node Composition
  • 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
Inserting at the Front of a Linked List
8
Deleting From the Front of a Linked List
9
Removing a Target Node
10
  • template lttypename Tgt
  • void eraseValue(nodeltTgt front, const T
    target)
  • // curr moves through list, trailed by prev
  • nodeltTgt curr front, prev NULL
  • // becomes true if we locate target
  • bool foundItem false
  • // scan until locate item or come to end of
    list
  • while (curr ! NULL !foundItem)
  • if (curr-gtnodeValue target) // have a
    match
  • if (prev NULL) // remove the first
    node
  • front front-gtnext
  • else
  • prev-gtnext curr-gtnext // erase
    intermediate node
  • delete curr

11
Handling the Back of the List
push() and pop() only involve elements in front
hence singly linked list is efficient
  • For push() and pop() of queue containers we need
    to access elements in front , as well as back.
  • For singly linked list we need to scan the entire
    list to access the back element O(n) time

12
Designing a New Linked List Structure
  • Introduce a second pointer , called back, that
    always points to the end of the list

Inserting at the back of a non-empty list
13
Adding a node with the value item in the front of
an empty list
  • nodeltTgt front, back, newnode
  • Newnode new nodeltTgt(item)
  • if (front!NULL)
  • back-gtnextnewNode
  • else
  • front-gtnext newNode
  • backnewNode

Adding a node with the value item in the front of
an empty list
nodeltTgt front, back, newnode newnode new
nodeltTgt(item, front) if (frontNULL) backnewN
ode frontnewNode
14
Doubly Linked List
dnode object
prev
next
A singly linked list is inefficient to implement
the list operations such as inserting and
deleting elements at a position. We create a new
node called dnode, which has two pointer fields,
which specify the addresses of both the previous
node and next node in the list. A sequence of
dnode objects creates a list called a doubly
linked list
15
Inserting a Node at a Position
16
Deleting a Node at a Position
17
Circular Doubly Linked Lists
  • A Watch Band provides a good Real Life analogue
    for this Data Structure

18
Circular Doubly Linked Lists
  • Implemented on a Computer it might look something
    like this.

19
dnode Class
  • template lttypename Tgt
  • class dnode
  • public
  • // the members of a dnode object are used for
    operations within a
  • // doubly linked list access is simplified by
    making them public
  • T nodeValue // data value of the node
  • dnodeltTgt prev // previous node in the list
  • dnodeltTgt next // next node in the list
  • // default constructor. creates object with
    value T(), the
  • // default value of type T. set the node
    pointers to point at
  • // the node itself
  • dnode()
  • next this // the next node is the current
    node
  • prev this // the previous node is the
    current node

20
Updating a Doubly Linked List
21
dnode insert()
  • template lttypename Tgt
  • dnodeltTgt insert(dnodeltTgt curr, const T item)
  • // declare pointer variables for the new node
    and the previous node
  • dnodeltTgt newNode, prevNode
  • // allocate new dnode with item as initial value
  • newNode new dnodeltTgt(item)
  • // assign prevNode the pointer value of node
    before p
  • prevNode curr-gtprev
  • // update pointer fields in newNode
  • newNode-gtprev prevNode
  • newNode-gtnext curr
  • // update curr and prevNode to point at newNode
  • prevNode-gtnext newNode
  • curr-gtprev newNode

22
dnode erase()
  • template lttypename Tgt
  • void erase(dnodeltTgt curr)
  • // return if the list is empty
  • if (curr-gtnext curr)
  • return
  • // declare pointers for the predecessor and
    successor nodes
  • dnodeltTgt prevNode curr-gtprev, succNode
    curr-gtnext
  • // update pointer fields for predecessor and
    successor
  • prevNode-gtnext succNode
  • succNode-gtprev prevNode
  • // deallocate the memory used by the node
  • delete curr

23
Summary 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.
24
Summary 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.
25
Summary 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.
26
Summary 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.
27
Summary 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.
Write a Comment
User Comments (0)
About PowerShow.com