Allocating and initializing a node - PowerPoint PPT Presentation

1 / 57
About This Presentation
Title:

Allocating and initializing a node

Description:

unhook current node from list. prev- next = curr- next; // move current down to node after prev ... Unhook node to be deleted. // Store data from node, to be returned. ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 58
Provided by: ruthu
Category:

less

Transcript and Presenter's Notes

Title: Allocating and initializing a node


1
Allocating and initializing a node
cptr
  • nodeltchargt cptr
  • cptr new nodeltchargt('z')
  • cptr new nodeltchargt('m',cptr)

2
Changing the data of a node
  • Remember nodeValue and next are public.
  • nodeltchargt c1ptr, c2ptr
  • c1ptr new nodeltchargt('a')
  • c2ptr new nodeltchargt('b')

c2ptr
c1ptr
'a'
NULL
'b'
NULL
3
Changing the data of a node
  • Remember nodeValue and next are public.
  • nodeltchargt c1ptr, c2ptr
  • c1ptr new nodeltchargt('a')
  • c2ptr new nodeltchargt('b')
  • c1ptr-gtnext c2ptr

c2ptr
c1ptr
'b'
NULL
4
Changing the data of a node
  • Remember nodeValue and next are public.
  • nodeltchargt c1ptr, c2ptr
  • c1ptr new nodeltchargt('a')
  • c2ptr new nodeltchargt('b')
  • c1ptr-gtnext c2ptr
  • c2ptr-gtnodeValue 'k'

c2ptr
c1ptr
'k'
NULL
5
Inserting node between prev and curr
newNode
  • // allocate the new node
  • newNode new nodeltchargt('k')

6
Inserting node between prev and curr
'k'
newNode
  • // allocate the new node
  • newNode new nodeltchargt('k')
  • // connect new node to tail of list
  • newNode-gtnext curr

7
Inserting node between prev and curr
'a'
newNode
  • // allocate the new node
  • newNode new nodeltchargt('k')
  • // connect new node to tail of list
  • newNode-gtnext curr
  • // connect new node to head part of list
  • prev-gtnext newNode

8
Erasing current node
oldNode
  • // save address of current node i.e. node being
    erased
  • oldNode curr

9
Erasing current node
'a'
oldNode
  • // save address of current node i.e. node being
    erased
  • oldNode curr
  • // unhook current node from list
  • prev-gtnext curr-gtnext

10
Erasing current node
curr
oldNode
  • // save address of current node i.e. node being
    erased
  • oldNode curr
  • // unhook current node from list
  • prev-gtnext curr-gtnext
  • // move current down to node after prev
  • curr prev-gtnext

11
Erasing current node
oldNode
  • // save address of current node i.e. node being
    erased
  • oldNode curr
  • // unhook current node from list
  • prev-gtnext curr-gtnext
  • // move current down to node after prev
  • curr prev-gtnext
  • // deallocate the node which has been unhooked
  • delete oldNode

12
Adding node to the front of a list
  • Allocate new node storing data and front into
    nodeValue and next members of new node,
    respectively. Store address of new node into
    front.
  • front new Nodeltintgt(58, front)
  • Case 1 Adding to existing list.

front
5210
3570
5210
75
3570
39
NULL
4668
58
5210
13
Adding node to the front of a list
  • Allocate new node storing data and front into
    nodeValue and next members of new node,
    respectively. Store address of new node into
    front.
  • front new Nodeltintgt(58, front)
  • Case 1 Adding to existing list.

front
5210
3570
4668
75
3570
39
NULL
4668
58
5210
14
Adding node to the front of a list
  • Allocate new node storing data and front into
    nodeValue and next members of new node,
    respectively. Store address of new node into
    front.
  • front new Nodeltintgt(58, front)
  • Case 2 Adding to empty list.

front
NULL
4668
58
NULL
15
Adding node to the front of a list
  • Allocate new node storing data and front into
    nodeValue and next members of new node,
    respectively. Store address of new node into
    front.
  • front new Nodeltintgt(58, front)
  • Case 2 Adding to empty list.

head
4668
4668
58
NULL
16
Deleting node to the front of list
  • Case 1 - Removing the front node from list of
    more than one node.
  • // Record address of node to be deleted.
  • temp front
  • // Unhook node to be deleted.
  • // Store data from node, to be returned.
  • // Delete node returning storage to system.

temp
5210
3570
5210
75
3570
39
NULL
front
5210
17
Deleting node to the front of list
  • Case 1 - Removing the front node from list of
    more than one node.
  • // Record address of node to be deleted.
  • // Unhook node to be deleted.
  • front front-gtnext
  • // Store data from node, to be returned.
  • // Delete node returning storage to system.

temp
5210
3570
5210
75
3570
39
NULL
front
5210
18
Deleting node to the front of list
  • Case 1 - Removing the front node from list of
    more than one node.
  • // Record address of node to be deleted.
  • // Unhook node to be deleted.
  • // Store data from node, to be returned.
  • tempData temp-gtnodeValue
  • // Delete node returning storage to system.

temp
tempData
5210
3570
5210
75
75
3570
39
NULL
front
3570
19
Deleting node to the front of list
  • Case 1 - Removing the front node from list of
    more than one node.
  • // Record address of node to be deleted.
  • // Unhook node to be deleted.
  • // Store data from node, to be returned.
  • // Delete node returning storage to system.
  • delete temp

temp
tempData
3570
??
75
39
NULL
front
3570
20
Deleting node to the front of list
  • Case 2 - Removing the only node from list.
  • // Record address of node to be deleted.
  • temp front
  • // Unhook node to be deleted.
  • // Store data from node, to be returned.
  • // Delete node returning storage to system.

temp
5210
5210
75
NULL
front
5210
21
Deleting node to the front of list
  • Case 2 - Removing the only node from list.
  • // Record address of node to be deleted.
  • // Unhook node to be deleted.
  • front front-gtnext
  • // Store data from node, to be returned.
  • // Delete node returning storage to system.

temp
5210
5210
75
NULL
front
NULL
22
Deleting node to the front of list
  • Case 2 - Removing the only node from list.
  • // Record address of node to be deleted.
  • // Unhook node to be deleted.
  • // Store data from node, to be returned.
  • tempData temp-gtnodeValue
  • // Delete node returning storage to system.

temp
tempData
5210
5210
75
75
NULL
front
NULL
23
Deleting node to the front of list
  • Case 2 - Removing the only node from list.
  • // Record address of node to be deleted.
  • // Unhook node to be deleted.
  • // Store data from node, to be returned.
  • // Delete node returning storage to system.
  • delete temp

temp
tempData
??
75
front
NULL
24
Procedure for Copying a Linked List
  • Set up pointers which will move down each list.
  • Allocate first node in self and copy data into
    it, leave its pointer as default value NULL.
  • While there are more nodes in the list being
    copied, allocate node, store data and attach to
    end of list.

25
Copying a singly linked list
rhs.front
3570
5210
4668
75
3570
39
NULL
58
5210
4668
curr
newNode
??
4668
this.front
NULL
curr rhs.front
26
Copying a singly linked list
rhs.front
3570
5210
4668
75
3570
39
NULL
58
5210
4668
curr
newNode
4924
4668
this.front
4924
58
NULL
4924
newNode new nodeltintgt(curr-gtnodeValue) front
newNode
27
Copying a singly linked list
rhs.front
3570
5210
4668
75
3570
39
NULL
58
5210
4668
curr
newNode
4924
5210
this.front
4924
58
NULL
4924
curr curr-gtnext
28
Copying a singly linked list
rhs.front
3570
5210
4668
75
3570
39
NULL
58
5210
4668
curr
newNode
4924
5210
this.front
4924
75
NULL
58
3110
3110
4924
newNode-gtnext new nodeltintgt(curr-gtnodeValue)
29
Copying a singly linked list
rhs.front
3570
5210
4668
75
3570
39
NULL
58
5210
4668
curr
newNode
3110
3570
this.front
4924
75
NULL
58
3110
3110
4924
newNode newNode-gtnext curr curr-gtnext
30
Copying a singly linked list
rhs.front
3570
5210
4668
75
3570
39
NULL
58
5210
4668
curr
newNode
3110
3570
this.front
39
NULL
4924
75
5818
58
3110
5818
3110
4924
newNode-gtnext new nodeltintgt(curr-gtnodeValue)
31
Copying a singly linked list
rhs.front
3570
5210
4668
75
3570
39
NULL
58
5210
4668
curr
newNode
5818
NULL
this.front
39
NULL
4924
75
5818
58
3110
5818
3110
4924
newNode newNode-gtnext curr curr-gtnext
32
The LinkedList Class
  • template ltclass Tgt
  • class singlyLinkedList
  • public
  • // class iterator for singlyLinkedList
    (similar to iterator for list // in chapter
    six)
  • templatelttypename Tgt
  • class iterator
  • public
  • iterator()
  • T operator()
  • iterator() operator()
  • bool operator(iterator rhs)
  • bool operator! (iterator rhs)

33
The LinkedList Class - cont.
  • iterator end()
  • const_iterator end()
  • iterator begin()
  • const_iterator begin()
  • private
  • nodeltTgt nodePtr
  • // private constructor
  • iterator(nodeltTgt p)
  • // Constructor
  • singlyLinkedList( )
  • singlyLinkedList(int n, const T itemT())
  • singlyLinkedList(T first, T last)
  • // Copy constructor
  • singlyLinkedList(const singlyLinkedList
    sLLobj)

34
The LinkedList Class - cont.
  • // Destructor
  • singlyLinkedList( )
  • // Overloaded assignment operator
  • singlyLinkedList operator t(const
    singlyLinkedListltTgt sLLobj)
  • // list modifiers
  • void push_back(const T value)
  • void pop_back()
  • void push_front(const T value)
  • void pop_front()
  • iterator insert(iterator pos, const T item)
  • void erase(iterator pos)
  • void erase(iterator first, iterator last)

35
The LinkedList Class - cont.
  • // Methods to examine the list
  • T back()
  • const T back()const
  • T front()
  • const T front()const
  • bool empty()const
  • int size()const
  • private
  • nodeltTgt head, rear
  • int listSize
  • // private methods
  • nodeltTgt getNode(const T item)
  • nodeltTgt insert(nodeltTgt curr, const T item)
  • void erase(nodeltTgt curr)

36
Private member methods
  • // getNode allocates a node, stores the data and
    returns the address
  • // of the node. The nodes next field is NULL.
  • template lttypename Tgt
  • nodeltTgt singlyLinkedListgetNode(const T
    item)
  • return (new nodeltTgt(item))

37
Private member methods
  • // insert will insert a new node whose data field
    is item, before curr
  • // and return the address of the new node.
  • template lttypename Tgt
  • nodeltTgt singlyLinkedListinsert(nodeltTgt
    curr, const T item)
  • nodeltTgt prev, newNode
  • newNode getNode(item)
  • newNode-gtnext curr
  • if (head curr) // insert at front of list
  • head newNode
  • else // insert in middle or end of list
  • prev head
  • while (prev-gtnext ! curr) prev prev-gtnext
  • prev-gtnext newNode
  • return newNode

38
Private member methods
  • // erase will delete the node whose address is
    curr from the list
  • template lttypename Tgt
  • void singlyLinkedListerase(nodeltTgt curr)
  • node ltTgt prev
  • if (curr head) // erase front node of list
  • head head-gtnext
  • else // erase node in middle or end of list
  • prev head
  • while(prev-gtnext ! curr)
  • prev prev-gtnext
  • prev-gtnext curr-gtnext
  • delete curr

39
Constructors
  • // default constructor constructs empty list
  • template ltclass Tgt
  • singlyLinkedListltTgtsinglyLinkedList( )
  • head rear NULL
  • listSize 0
  • // construct list of n elements all having data
    value item
  • template ltclass Tgt
  • singlyLinkedListsinglyLinkedList(int n, const
    T itemT())
  • for (int i1 iltn i)
  • head insert(head, item)

40
Constructors
  • // construct a sublist of the list from data in
    the specified range
  • template ltclass Tgt
  • singlyLinkedListsinglyLinkedList(T first, T
    last )
  • T curr first
  • head new node (curr) // create first node in
    list
  • listSize 1
  • rear head
  • curr
  • while (curr ! last)
  • rear insert(NULL, curr)
  • pos
  • listSize

41
  • // Copy constructor creates a copy of list L
  • template ltclass Tgt
  • singlyLinkedListltTgtsinglyLinkedList(const
    singlyLinkedListltTgt sLLobj)
  • iterator curr
  • node ltTgt newCurr
  • if (sLLobj.empty()) // create an empty list
  • head NULL
  • rear NULL
  • listSize 0
  • else // copy nodes inserting at rear
  • curr sLLobj.front()
  • head getNode(curr) // copy first node
  • curr curr-gtnext
  • while (curr ! sLLobj.end()) // copy and insert
    other nodes
  • newCurr insert(,NULL, curr)
  • curr curr-gtnext
  • listSize sLLobj.size() // set size of list
    and rear to last node

42
// Overloaded assignment operator creates a copy
of list L template ltclass Tgt singlyLinkedList
singlyLinkedListltTgtoperator( const
singlyLinkedListltTgt sLLobj) iterator
curr node ltTgt newCurr if (L.empty()) //
create an empty list head NULL rear
NULL listSize 0 else // copy nodes
inserting at rear curr L.front() head
getNode(curr) // copy first node curr
curr-gtnext while (curr ! L.end()) // copy
and insert other nodes newCurr
insert(NULL, curr) curr
curr-gtnext listSize L.size() // set
size of list and rear to last node rear
newCurr return this
43
Destructor
  • // destructor deletes all elements in the list
  • template ltclass Tgt
  • void singlyLinkedListltTgt singlyLinked List
  • while (!empty())
  • erase(head) // erase front node until list is
    empty

44
List modifier methods
  • template ltclass Tgt
  • void singlyLinkedListpush_back(const T value)
  • insert(value,NULL)
  • listSize
  • template ltclass Tgt
  • void singlyLinkedListpop_back()
  • erase(rear)
  • listSize--

45
List modifier methods
  • template ltclass Tgt
  • void singlyLinkedListpush_front(const T value)
  • insert(value,head)
  • listSize
  • template ltclass Tgt
  • void singlyLinkedListpop_front()
  • erase(front)
  • listSize--

46
List modifier methods
  • template ltclass Tgt
  • iterator singlyLinkedListinsert(iterator pos,
    const T item)
  • node ltTgt curr, newNode
  • curr pos.nodePtr
  • newNode insert(curr, item)
  • listSize
  • return iterator(newNode)
  • template ltclass Tgt
  • void singlyLinkedListerase(iterator pos)
  • erase(pos.nodePtr)
  • listSize--

47
List modifier methods
  • template ltclass Tgt
  • void singlyLinkedListerase(iterator first,
    iterator last)
  • nodeltTgt curr, finish
  • curr first.nodePtr
  • finish last.nodePtr
  • while(curr ! finish)
  • erase(curr)
  • listSize--

48
Methods to examine the list
  • // Retrieve the last item of the list
  • template ltclass Tgt
  • T singlyLinkedListback()
  • return rear-gtnodeValue
  • // Retrieve the last item of the list
  • template ltclass Tgt
  • const T singlyLinkedListback()const
  • return rear-gtnodeValue

49
Methods to examine the list
  • // Retrieve the first item of the list
  • template ltclass Tgt
  • T singlyLinkedListfront()
  • return head-gtnodeValue
  • // Retrieve the first item of the list
  • template ltclass Tgt
  • const T singlyLinkedList front()const
  • return head-gtnodeValue

50
Methods to examine the list
  • // Retrieve the size of the list
  • template ltclass Tgt
  • int singlyLinkedListltTgtsize(void) const
  • return listSize
  • // Determines if the list is empty
  • template ltclass Tgt
  • bool singlyLinkedListltTgtempty(void) const
  • return (listSize 0)

51
Implementing the iterator class
  • // The iterator class will be inplemented as
    inline code
  • iterator()
  • nodePtrNULL
  • T operator()
  • return nodePtr-gtnodeValue
  • iterator() operator()
  • return nodePtr-gtnext
  • bool operator(iterator rhs)
  • return (nodePtr rhs.nodePtr)
  • bool operator! (iterator rhs)
  • return (nodePtr!rhs.nodePtr)

52
Implementing the iterator class
  • iterator end()
  • return NULL
  • const_iterator end()
  • return NULL
  • iterator begin()
  • return head
  • const_iterator begin()
  • return head
  • // private constructor also implemented with
    inline code
  • iterator (nodeltTgt p)
  • nodePtr p

53
What are Header and Trailer Nodes?
  • A Header Node is a node at the beginning of a
    list that contains a key value smaller than any
    possible key.
  • A Trailer Node is a node at the end of a list
    that contains a key larger than any possible key.
  • Both header and trailer are place holding nodes
    used to simplify list processing.

rear
INT_MIN 5 8
13 INT_MAX
head
54
Circular Singly Linked List
  • A circular linked list is a list in which every
    node has a successor the last element is
    succeeded by the first element.

B C L
listData
55
Queues as circular lists
  • Implementing a queue using a circular singly
    linked list allows us to have a single pointer
    into the list, to the rear of the queue.
  • The front of the queue is then rear-gtnext.

56
Some simple queues
  • What does an empty queue look like if implemented
    as a circular queue?
  • What does a queue of one element look like if
    implemented as a circular queue?

57
More Questions about circular queues
  • How would you insert an element to a queue which
    is implemented as a circular singly linked list?

58
More Questions about circular queues
  • How would you erase an element from a queue which
    is implemented as a circular singly linked list?

59
What is a Doubly Linked List?
  • A doubly linked list is a list in which each node
    is linked to both its successor and its
    predecessor.

rear
head
A C F
T Z
60
Each node contains two pointers
templatelt typename T gt class dNode
public dNodeltTgt back // Pointer to
predecessor T nodeValue // Data
value dNodeltTgt next // Pointer to
successor // default constructor dnode(const T
itemT(), dNodeltTgt bpNULL, dNodeltTgt
npNULL)
3000 A NULL
back nodeValue next
61
Inserting to a doubly linked list
  • Locate the node prior to the insertion point.
  • Insert into the forward pointing list, as with a
    singly linked list.
  • The next field of the new node is the before for
    the backward pointing list.
  • Insert into the backward pointing list.

62
Deleting from a doubly linked list
  • Locate the node to be deleted.
  • It contains the addresses of the two nodes which
    need to be modified to unhook the node.
  • Unhook the node in both directions.

63
The list class implementation
  • Implemented as a circular doubly linked list with
    a header node.
  • The header node is the address returned by end.
  • The pointer into the list is to the header node.

64
Writing the iterator functions for list
  • iterator()
  • iteratorltTgt begin()
  • iteratorltTgt end()
  • bool operator (iterator rhs)

header
b
n
92
73
46
65
Writing the iterator functions for list
  • iterator()
  • nodeptr NULL
  • iteratorltTgt begin()
  • return iterator(header-gtnext)
  • iteratorltTgt end()
  • return iterator(header)
  • bool operator (iterator rhs)
  • return (nodePtr rhs.nodePtr)

header
b
n
92
73
46
66
Writing the iterator functions for list
header
  • // prefix version of
  • iterator operator ()
  • // prefix version of --
  • iterator operator --()

b
n
92
73
46
67
Writing the iterator functions for list
header
  • // prefix version of
  • iterator operator ()
  • nodePtr(nodePtr-gtnext)
  • return this
  • // prefix version of --
  • iterator operator --()
  • nodePtr(nodePtr-gtback)
  • return this

b
n
92
73
46
68
Postfix version of unary operators
  • Specify an int parameter.
  • Save the value of this, for later return.
  • Update this.
  • Return saved value.

69
Postfix version of for iterators
  • iterator operator(int)
  • iterator temp
  • temp this // save curr value of self
  • nodePtr nodePtr-gtnext // perform
    autoincrement
  • return temp // return saved value
  • // to be used
Write a Comment
User Comments (0)
About PowerShow.com