Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1...
Transcript of Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1...
Unit 3: Linked ListsPart 2: More on Linked Lists
Engineering 4892:Data Structures
Faculty of Engineering & Applied ScienceMemorial University of Newfoundland
May 30, 2011
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 1 / 20
1 Deletion in singly linked lists (cont’d)
1 Other Functions
1 Doubly Linked Lists
1 Circular lists
1 Linked lists vs. arrays
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 2 / 20
Deletion in singly linked lists (cont’d)
We now consider the more general deleteNode method.
deleteNode
deletes the node containing a particular integer (it will delete the firstnode containing the integer, if there are more than one).
First, we will need to search for the node to delete.
Removing a node from an SLL is accomplished by linking the node’spredecessor to its successor.
Like in deleteFromTail we will have to use a loop to find the predecessor.
So we need two loops:
one to find tmp, the node to remove
one to find tmp’s predecessor, pred
Actually, we can combine these into one.
Deletion in singly linked lists (cont’d)
We now consider the more general deleteNode method. deleteNode
deletes the node containing a particular integer (it will delete the firstnode containing the integer, if there are more than one).
First, we will need to search for the node to delete.
Removing a node from an SLL is accomplished by linking the node’spredecessor to its successor.
Like in deleteFromTail we will have to use a loop to find the predecessor.
So we need two loops:
one to find tmp, the node to remove
one to find tmp’s predecessor, pred
Actually, we can combine these into one.
Deletion in singly linked lists (cont’d)
We now consider the more general deleteNode method. deleteNode
deletes the node containing a particular integer (it will delete the firstnode containing the integer, if there are more than one).
First, we will need to search for the node to delete.
Removing a node from an SLL is accomplished by linking the node’spredecessor to its successor.
Like in deleteFromTail we will have to use a loop to find the predecessor.
So we need two loops:
one to find tmp, the node to remove
one to find tmp’s predecessor, pred
Actually, we can combine these into one.
Deletion in singly linked lists (cont’d)
We now consider the more general deleteNode method. deleteNode
deletes the node containing a particular integer (it will delete the firstnode containing the integer, if there are more than one).
First, we will need to search for the node to delete.
Removing a node from an SLL is accomplished by linking the node’spredecessor to its successor.
Like in deleteFromTail we will have to use a loop to find the predecessor.
So we need two loops:
one to find tmp, the node to remove
one to find tmp’s predecessor, pred
Actually, we can combine these into one.
Deletion in singly linked lists (cont’d)
We now consider the more general deleteNode method. deleteNode
deletes the node containing a particular integer (it will delete the firstnode containing the integer, if there are more than one).
First, we will need to search for the node to delete.
Removing a node from an SLL is accomplished by linking the node’spredecessor to its successor.
Like in deleteFromTail we will have to use a loop to find the predecessor.
So we need two loops:
one to find tmp, the node to remove
one to find tmp’s predecessor, pred
Actually, we can combine these into one.
Deletion in singly linked lists (cont’d)
We now consider the more general deleteNode method. deleteNode
deletes the node containing a particular integer (it will delete the firstnode containing the integer, if there are more than one).
First, we will need to search for the node to delete.
Removing a node from an SLL is accomplished by linking the node’spredecessor to its successor.
Like in deleteFromTail we will have to use a loop to find the predecessor.
So we need two loops:
one to find tmp, the node to remove
one to find tmp’s predecessor, pred
Actually, we can combine these into one.
Deletion in singly linked lists (cont’d)
We now consider the more general deleteNode method. deleteNode
deletes the node containing a particular integer (it will delete the firstnode containing the integer, if there are more than one).
First, we will need to search for the node to delete.
Removing a node from an SLL is accomplished by linking the node’spredecessor to its successor.
Like in deleteFromTail we will have to use a loop to find the predecessor.
So we need two loops:
one to find tmp, the node to remove
one to find tmp’s predecessor, pred
Actually, we can combine these into one.
Deletion in singly linked lists (cont’d)
We now consider the more general deleteNode method. deleteNode
deletes the node containing a particular integer (it will delete the firstnode containing the integer, if there are more than one).
First, we will need to search for the node to delete.
Removing a node from an SLL is accomplished by linking the node’spredecessor to its successor.
Like in deleteFromTail we will have to use a loop to find the predecessor.
So we need two loops:
one to find tmp, the node to remove
one to find tmp’s predecessor, pred
Actually, we can combine these into one.
Deletion in singly linked lists (cont’d)
We now consider the more general deleteNode method. deleteNode
deletes the node containing a particular integer (it will delete the firstnode containing the integer, if there are more than one).
First, we will need to search for the node to delete.
Removing a node from an SLL is accomplished by linking the node’spredecessor to its successor.
Like in deleteFromTail we will have to use a loop to find the predecessor.
So we need two loops:
one to find tmp, the node to remove
one to find tmp’s predecessor, pred
Actually, we can combine these into one.
Deletion in singly linked lists (cont’d)
We now consider the more general deleteNode method. deleteNode
deletes the node containing a particular integer (it will delete the firstnode containing the integer, if there are more than one).
First, we will need to search for the node to delete.
Removing a node from an SLL is accomplished by linking the node’spredecessor to its successor.
Like in deleteFromTail we will have to use a loop to find the predecessor.
So we need two loops:
one to find tmp, the node to remove
one to find tmp’s predecessor, pred
Actually, we can combine these into one.
In the following, we are trying to delete the 8-node.
The loop begins with pred = head and tmp = head−>next.
At each step, both pred and tmp are incremented:
pred = pred−>next, tmp = tmp−>next
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 4 / 20
In the following, we are trying to delete the 8-node.
The loop begins with pred = head and tmp = head−>next.
At each step, both pred and tmp are incremented:
pred = pred−>next, tmp = tmp−>next
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 4 / 20
In the following, we are trying to delete the 8-node.
The loop begins with pred = head and tmp = head−>next.
At each step, both pred and tmp are incremented:
pred = pred−>next, tmp = tmp−>next
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 4 / 20
In the following, we are trying to delete the 8-node.
The loop begins with pred = head and tmp = head−>next.
At each step, both pred and tmp are incremented:
pred = pred−>next, tmp = tmp−>next
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 4 / 20
In the following, we are trying to delete the 8-node.
The loop begins with pred = head and tmp = head−>next.
At each step, both pred and tmp are incremented:
pred = pred−>next, tmp = tmp−>next
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 4 / 20
The loop terminates when tmp is at the item to delete: tmp−>info == el.
Actually, if the item is not present this will never happen. So we shouldcheck that tmp != 0 before trying to dereference tmp.
The loop:
IntSLLNode ∗pred , ∗tmp ;for ( pred = head , tmp = head−>next ;
tmp != 0 && ! ( tmp−>info == el ) ;pred = pred−>next , tmp = tmp−>next ) ;
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 5 / 20
The loop terminates when tmp is at the item to delete: tmp−>info == el.
Actually, if the item is not present this will never happen. So we shouldcheck that tmp != 0 before trying to dereference tmp.
The loop:
IntSLLNode ∗pred , ∗tmp ;for ( pred = head , tmp = head−>next ;
tmp != 0 && ! ( tmp−>info == el ) ;pred = pred−>next , tmp = tmp−>next ) ;
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 5 / 20
The loop terminates when tmp is at the item to delete: tmp−>info == el.
Actually, if the item is not present this will never happen.
So we shouldcheck that tmp != 0 before trying to dereference tmp.
The loop:
IntSLLNode ∗pred , ∗tmp ;for ( pred = head , tmp = head−>next ;
tmp != 0 && ! ( tmp−>info == el ) ;pred = pred−>next , tmp = tmp−>next ) ;
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 5 / 20
The loop terminates when tmp is at the item to delete: tmp−>info == el.
Actually, if the item is not present this will never happen. So we shouldcheck that tmp != 0 before trying to dereference tmp.
The loop:
IntSLLNode ∗pred , ∗tmp ;for ( pred = head , tmp = head−>next ;
tmp != 0 && ! ( tmp−>info == el ) ;pred = pred−>next , tmp = tmp−>next ) ;
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 5 / 20
The loop terminates when tmp is at the item to delete: tmp−>info == el.
Actually, if the item is not present this will never happen. So we shouldcheck that tmp != 0 before trying to dereference tmp.
The loop:
IntSLLNode ∗pred , ∗tmp ;for ( pred = head , tmp = head−>next ;
tmp != 0 && ! ( tmp−>info == el ) ;pred = pred−>next , tmp = tmp−>next ) ;
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 5 / 20
The loop terminates when tmp is at the item to delete: tmp−>info == el.
Actually, if the item is not present this will never happen. So we shouldcheck that tmp != 0 before trying to dereference tmp.
The loop:
IntSLLNode ∗pred , ∗tmp ;for ( pred = head , tmp = head−>next ;
tmp != 0 && ! ( tmp−>info == el ) ;pred = pred−>next , tmp = tmp−>next ) ;
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 5 / 20
The loop terminates when tmp is at the item to delete: tmp−>info == el.
Actually, if the item is not present this will never happen. So we shouldcheck that tmp != 0 before trying to dereference tmp.
The loop:
IntSLLNode ∗pred , ∗tmp ;for ( pred = head , tmp = head−>next ;
tmp != 0 && ! ( tmp−>info == el ) ;
pred = pred−>next , tmp = tmp−>next ) ;
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 5 / 20
The loop terminates when tmp is at the item to delete: tmp−>info == el.
Actually, if the item is not present this will never happen. So we shouldcheck that tmp != 0 before trying to dereference tmp.
The loop:
IntSLLNode ∗pred , ∗tmp ;for ( pred = head , tmp = head−>next ;
tmp != 0 && ! ( tmp−>info == el ) ;pred = pred−>next , tmp = tmp−>next ) ;
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 5 / 20
The loop terminates when tmp is at the item to delete: tmp−>info == el.
Actually, if the item is not present this will never happen. So we shouldcheck that tmp != 0 before trying to dereference tmp.
The loop:
IntSLLNode ∗pred , ∗tmp ;for ( pred = head , tmp = head−>next ;
tmp != 0 && ! ( tmp−>info == el ) ;pred = pred−>next , tmp = tmp−>next ) ;
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 5 / 20
Before continuing we check that tmp != 0 (if this is not true, the item doesnot exist... so we have nothing to do).
The 8-node is removed by setting pred−>next = tmp−>next.
tmp is delete’ed
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 6 / 20
Before continuing we check that tmp != 0 (if this is not true, the item doesnot exist... so we have nothing to do).
The 8-node is removed by setting pred−>next = tmp−>next.
tmp is delete’ed
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 6 / 20
Before continuing we check that tmp != 0 (if this is not true, the item doesnot exist... so we have nothing to do).
The 8-node is removed by setting pred−>next = tmp−>next.
tmp is delete’ed
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 6 / 20
Before continuing we check that tmp != 0 (if this is not true, the item doesnot exist... so we have nothing to do).
The 8-node is removed by setting pred−>next = tmp−>next.
tmp is delete’ed
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 6 / 20
Before continuing we check that tmp != 0 (if this is not true, the item doesnot exist... so we have nothing to do).
The 8-node is removed by setting pred−>next = tmp−>next.
tmp is delete’ed
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 6 / 20
This is the code for the general case:
void IntSLList : : deleteNode ( int el ) {. . .IntSLLNode ∗pred , ∗tmp ;for ( pred = head , tmp = head−>next ;
tmp != 0 && ! ( tmp−>info == el ) ;pred = pred−>next , tmp = tmp−>next ) ;
if ( tmp != 0) {pred−>next = tmp−>next ;. . .delete tmp ;
}. . .
}
There are several special cases:
Trying to delete from an empty list
Could prevent by precondition; Here we choose to allow thisExit immediately if head = 0
This is the code for the general case:
void IntSLList : : deleteNode ( int el ) {. . .IntSLLNode ∗pred , ∗tmp ;for ( pred = head , tmp = head−>next ;
tmp != 0 && ! ( tmp−>info == el ) ;pred = pred−>next , tmp = tmp−>next ) ;
if ( tmp != 0) {pred−>next = tmp−>next ;. . .delete tmp ;
}. . .
}
There are several special cases:
Trying to delete from an empty list
Could prevent by precondition; Here we choose to allow thisExit immediately if head = 0
This is the code for the general case:
void IntSLList : : deleteNode ( int el ) {. . .IntSLLNode ∗pred , ∗tmp ;for ( pred = head , tmp = head−>next ;
tmp != 0 && ! ( tmp−>info == el ) ;pred = pred−>next , tmp = tmp−>next ) ;
if ( tmp != 0) {pred−>next = tmp−>next ;. . .delete tmp ;
}. . .
}
There are several special cases:
Trying to delete from an empty list
Could prevent by precondition; Here we choose to allow thisExit immediately if head = 0
This is the code for the general case:
void IntSLList : : deleteNode ( int el ) {. . .IntSLLNode ∗pred , ∗tmp ;for ( pred = head , tmp = head−>next ;
tmp != 0 && ! ( tmp−>info == el ) ;pred = pred−>next , tmp = tmp−>next ) ;
if ( tmp != 0) {pred−>next = tmp−>next ;. . .delete tmp ;
}. . .
}
There are several special cases:
Trying to delete from an empty list
Could prevent by precondition; Here we choose to allow this
Exit immediately if head = 0
This is the code for the general case:
void IntSLList : : deleteNode ( int el ) {. . .IntSLLNode ∗pred , ∗tmp ;for ( pred = head , tmp = head−>next ;
tmp != 0 && ! ( tmp−>info == el ) ;pred = pred−>next , tmp = tmp−>next ) ;
if ( tmp != 0) {pred−>next = tmp−>next ;. . .delete tmp ;
}. . .
}
There are several special cases:
Trying to delete from an empty list
Could prevent by precondition; Here we choose to allow thisExit immediately if head = 0
Deleting the one node in an SLL of length 1:
Delete the node and set head = tail = 0
if ( head == tail && el == head−>info ) {delete head ;head = tail = 0 ;
}
Deleting the first node in an SLL of length ≥ 2:
Update head and delete the node
. . . else if (el == head−>info ) {IntSLLNode ∗tmp = head ;head = head−>next ;delete tmp ;
}
Deleting the last node in an SLL of length ≥ 2:
Same as general case, but set tail = pred
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 8 / 20
Deleting the one node in an SLL of length 1:
Delete the node and set head = tail = 0
if ( head == tail && el == head−>info ) {delete head ;head = tail = 0 ;
}
Deleting the first node in an SLL of length ≥ 2:
Update head and delete the node
. . . else if (el == head−>info ) {IntSLLNode ∗tmp = head ;head = head−>next ;delete tmp ;
}
Deleting the last node in an SLL of length ≥ 2:
Same as general case, but set tail = pred
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 8 / 20
Deleting the one node in an SLL of length 1:
Delete the node and set head = tail = 0
if ( head == tail && el == head−>info ) {delete head ;head = tail = 0 ;
}
Deleting the first node in an SLL of length ≥ 2:
Update head and delete the node
. . . else if (el == head−>info ) {IntSLLNode ∗tmp = head ;head = head−>next ;delete tmp ;
}
Deleting the last node in an SLL of length ≥ 2:
Same as general case, but set tail = pred
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 8 / 20
Deleting the one node in an SLL of length 1:
Delete the node and set head = tail = 0
if ( head == tail && el == head−>info ) {delete head ;head = tail = 0 ;
}
Deleting the first node in an SLL of length ≥ 2:
Update head and delete the node
. . . else if (el == head−>info ) {IntSLLNode ∗tmp = head ;head = head−>next ;delete tmp ;
}
Deleting the last node in an SLL of length ≥ 2:
Same as general case, but set tail = pred
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 8 / 20
Deleting the one node in an SLL of length 1:
Delete the node and set head = tail = 0
if ( head == tail && el == head−>info ) {delete head ;head = tail = 0 ;
}
Deleting the first node in an SLL of length ≥ 2:
Update head and delete the node
. . . else if (el == head−>info ) {IntSLLNode ∗tmp = head ;head = head−>next ;delete tmp ;
}
Deleting the last node in an SLL of length ≥ 2:
Same as general case, but set tail = pred
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 8 / 20
Deleting the one node in an SLL of length 1:
Delete the node and set head = tail = 0
if ( head == tail && el == head−>info ) {delete head ;head = tail = 0 ;
}
Deleting the first node in an SLL of length ≥ 2:
Update head and delete the node
. . . else if (el == head−>info ) {IntSLLNode ∗tmp = head ;head = head−>next ;delete tmp ;
}
Deleting the last node in an SLL of length ≥ 2:
Same as general case, but set tail = pred
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 8 / 20
void IntSLList : : deleteNode ( int el ) {if ( head != 0) // i f non−empty l i s t ;
if ( head == tail && el == head−>info ) { // i f on l y one nodedelete head ;head = tail = 0 ;
}
else if ( el == head−>info ) { // i f more than one nodeIntSLLNode ∗tmp = head ; // and o l d head i s d e l e t e dhead = head−>next ;delete tmp ;
}else { // i f more than one node
IntSLLNode ∗pred , ∗tmp ; // and a non−head d e l e t e dfor ( pred = head , tmp = head−>next ;
tmp != 0 && ! ( tmp−>info == el ) ;pred = pred−>next , tmp = tmp−>next ) ;
if ( tmp != 0) {pred−>next = tmp−>next ;if ( tmp == tail )
tail = pred ;delete tmp ;
}}
}
void IntSLList : : deleteNode ( int el ) {if ( head != 0) // i f non−empty l i s t ;
if ( head == tail && el == head−>info ) { // i f on l y one nodedelete head ;head = tail = 0 ;
}else if ( el == head−>info ) { // i f more than one node
IntSLLNode ∗tmp = head ; // and o l d head i s d e l e t e dhead = head−>next ;delete tmp ;
}
else { // i f more than one nodeIntSLLNode ∗pred , ∗tmp ; // and a non−head d e l e t e dfor ( pred = head , tmp = head−>next ;
tmp != 0 && ! ( tmp−>info == el ) ;pred = pred−>next , tmp = tmp−>next ) ;
if ( tmp != 0) {pred−>next = tmp−>next ;if ( tmp == tail )
tail = pred ;delete tmp ;
}}
}
void IntSLList : : deleteNode ( int el ) {if ( head != 0) // i f non−empty l i s t ;
if ( head == tail && el == head−>info ) { // i f on l y one nodedelete head ;head = tail = 0 ;
}else if ( el == head−>info ) { // i f more than one node
IntSLLNode ∗tmp = head ; // and o l d head i s d e l e t e dhead = head−>next ;delete tmp ;
}else { // i f more than one node
IntSLLNode ∗pred , ∗tmp ; // and a non−head d e l e t e dfor ( pred = head , tmp = head−>next ;
tmp != 0 && ! ( tmp−>info == el ) ;pred = pred−>next , tmp = tmp−>next ) ;
if ( tmp != 0) {pred−>next = tmp−>next ;if ( tmp == tail )
tail = pred ;delete tmp ;
}}
}
void IntSLList : : deleteNode ( int el ) {if ( head != 0) // i f non−empty l i s t ;
if ( head == tail && el == head−>info ) { // i f on l y one nodedelete head ;head = tail = 0 ;
}else if ( el == head−>info ) { // i f more than one node
IntSLLNode ∗tmp = head ; // and o l d head i s d e l e t e dhead = head−>next ;delete tmp ;
}else { // i f more than one node
IntSLLNode ∗pred , ∗tmp ; // and a non−head d e l e t e dfor ( pred = head , tmp = head−>next ;
tmp != 0 && ! ( tmp−>info == el ) ;pred = pred−>next , tmp = tmp−>next ) ;
if ( tmp != 0) {pred−>next = tmp−>next ;if ( tmp == tail )
tail = pred ;delete tmp ;
}}
}
What is the asymptotic complexity of deleteNode?
Best case: O(1)
Worst case: This requires going into our loop n − 1 times. Thus, it isO(n), just like deleteFromTail.
Average case: Assume that every node has an equal chance of beingdeleted. How many iterations through the loop are there for each possibleposition:
First node: 0 iterations (special case)
Second node: 0 iterations
Third node: 1 iteration
...
Last node: n − 2 iterations
The average number of iterations is:
1
n(1 + · · ·+ (n − 2)) =
n
2− 3
2+
1
n= O(n)
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 10 / 20
What is the asymptotic complexity of deleteNode?
Best case:
O(1)
Worst case: This requires going into our loop n − 1 times. Thus, it isO(n), just like deleteFromTail.
Average case: Assume that every node has an equal chance of beingdeleted. How many iterations through the loop are there for each possibleposition:
First node: 0 iterations (special case)
Second node: 0 iterations
Third node: 1 iteration
...
Last node: n − 2 iterations
The average number of iterations is:
1
n(1 + · · ·+ (n − 2)) =
n
2− 3
2+
1
n= O(n)
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 10 / 20
What is the asymptotic complexity of deleteNode?
Best case: O(1)
Worst case: This requires going into our loop n − 1 times. Thus, it isO(n), just like deleteFromTail.
Average case: Assume that every node has an equal chance of beingdeleted. How many iterations through the loop are there for each possibleposition:
First node: 0 iterations (special case)
Second node: 0 iterations
Third node: 1 iteration
...
Last node: n − 2 iterations
The average number of iterations is:
1
n(1 + · · ·+ (n − 2)) =
n
2− 3
2+
1
n= O(n)
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 10 / 20
What is the asymptotic complexity of deleteNode?
Best case: O(1)
Worst case:
This requires going into our loop n − 1 times. Thus, it isO(n), just like deleteFromTail.
Average case: Assume that every node has an equal chance of beingdeleted. How many iterations through the loop are there for each possibleposition:
First node: 0 iterations (special case)
Second node: 0 iterations
Third node: 1 iteration
...
Last node: n − 2 iterations
The average number of iterations is:
1
n(1 + · · ·+ (n − 2)) =
n
2− 3
2+
1
n= O(n)
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 10 / 20
What is the asymptotic complexity of deleteNode?
Best case: O(1)
Worst case: This requires going into our loop n − 1 times. Thus, it isO(n), just like deleteFromTail.
Average case: Assume that every node has an equal chance of beingdeleted. How many iterations through the loop are there for each possibleposition:
First node: 0 iterations (special case)
Second node: 0 iterations
Third node: 1 iteration
...
Last node: n − 2 iterations
The average number of iterations is:
1
n(1 + · · ·+ (n − 2)) =
n
2− 3
2+
1
n= O(n)
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 10 / 20
What is the asymptotic complexity of deleteNode?
Best case: O(1)
Worst case: This requires going into our loop n − 1 times. Thus, it isO(n), just like deleteFromTail.
Average case:
Assume that every node has an equal chance of beingdeleted. How many iterations through the loop are there for each possibleposition:
First node: 0 iterations (special case)
Second node: 0 iterations
Third node: 1 iteration
...
Last node: n − 2 iterations
The average number of iterations is:
1
n(1 + · · ·+ (n − 2)) =
n
2− 3
2+
1
n= O(n)
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 10 / 20
What is the asymptotic complexity of deleteNode?
Best case: O(1)
Worst case: This requires going into our loop n − 1 times. Thus, it isO(n), just like deleteFromTail.
Average case: Assume that every node has an equal chance of beingdeleted. How many iterations through the loop are there for each possibleposition:
First node: 0 iterations (special case)
Second node: 0 iterations
Third node: 1 iteration
...
Last node: n − 2 iterations
The average number of iterations is:
1
n(1 + · · ·+ (n − 2)) =
n
2− 3
2+
1
n= O(n)
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 10 / 20
What is the asymptotic complexity of deleteNode?
Best case: O(1)
Worst case: This requires going into our loop n − 1 times. Thus, it isO(n), just like deleteFromTail.
Average case: Assume that every node has an equal chance of beingdeleted. How many iterations through the loop are there for each possibleposition:
First node: 0 iterations (special case)
Second node: 0 iterations
Third node: 1 iteration
...
Last node: n − 2 iterations
The average number of iterations is:
1
n(1 + · · ·+ (n − 2)) =
n
2− 3
2+
1
n= O(n)
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 10 / 20
What is the asymptotic complexity of deleteNode?
Best case: O(1)
Worst case: This requires going into our loop n − 1 times. Thus, it isO(n), just like deleteFromTail.
Average case: Assume that every node has an equal chance of beingdeleted. How many iterations through the loop are there for each possibleposition:
First node: 0 iterations (special case)
Second node: 0 iterations
Third node: 1 iteration
...
Last node: n − 2 iterations
The average number of iterations is:
1
n(1 + · · ·+ (n − 2)) =
n
2− 3
2+
1
n= O(n)
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 10 / 20
What is the asymptotic complexity of deleteNode?
Best case: O(1)
Worst case: This requires going into our loop n − 1 times. Thus, it isO(n), just like deleteFromTail.
Average case: Assume that every node has an equal chance of beingdeleted. How many iterations through the loop are there for each possibleposition:
First node: 0 iterations (special case)
Second node: 0 iterations
Third node: 1 iteration
...
Last node: n − 2 iterations
The average number of iterations is:
1
n(1 + · · ·+ (n − 2)) =
n
2− 3
2+
1
n= O(n)
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 10 / 20
What is the asymptotic complexity of deleteNode?
Best case: O(1)
Worst case: This requires going into our loop n − 1 times. Thus, it isO(n), just like deleteFromTail.
Average case: Assume that every node has an equal chance of beingdeleted. How many iterations through the loop are there for each possibleposition:
First node: 0 iterations (special case)
Second node: 0 iterations
Third node: 1 iteration
...
Last node: n − 2 iterations
The average number of iterations is:
1
n(1 + · · ·+ (n − 2)) =
n
2− 3
2+
1
n= O(n)
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 10 / 20
What is the asymptotic complexity of deleteNode?
Best case: O(1)
Worst case: This requires going into our loop n − 1 times. Thus, it isO(n), just like deleteFromTail.
Average case: Assume that every node has an equal chance of beingdeleted. How many iterations through the loop are there for each possibleposition:
First node: 0 iterations (special case)
Second node: 0 iterations
Third node: 1 iteration
...
Last node: n − 2 iterations
The average number of iterations is:
1
n(1 + · · ·+ (n − 2)) =
n
2− 3
2+
1
n= O(n)
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 10 / 20
What is the asymptotic complexity of deleteNode?
Best case: O(1)
Worst case: This requires going into our loop n − 1 times. Thus, it isO(n), just like deleteFromTail.
Average case: Assume that every node has an equal chance of beingdeleted. How many iterations through the loop are there for each possibleposition:
First node: 0 iterations (special case)
Second node: 0 iterations
Third node: 1 iteration
...
Last node: n − 2 iterations
The average number of iterations is:
1
n(1 + · · ·+ (n − 2)) =
n
2− 3
2+
1
n= O(n)
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 10 / 20
What is the asymptotic complexity of deleteNode?
Best case: O(1)
Worst case: This requires going into our loop n − 1 times. Thus, it isO(n), just like deleteFromTail.
Average case: Assume that every node has an equal chance of beingdeleted. How many iterations through the loop are there for each possibleposition:
First node: 0 iterations (special case)
Second node: 0 iterations
Third node: 1 iteration
...
Last node: n − 2 iterations
The average number of iterations is:
1
n(1 + · · ·+ (n − 2)) =
n
2− 3
2+
1
n= O(n)
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 10 / 20
What is the asymptotic complexity of deleteNode?
Best case: O(1)
Worst case: This requires going into our loop n − 1 times. Thus, it isO(n), just like deleteFromTail.
Average case: Assume that every node has an equal chance of beingdeleted. How many iterations through the loop are there for each possibleposition:
First node: 0 iterations (special case)
Second node: 0 iterations
Third node: 1 iteration
...
Last node: n − 2 iterations
The average number of iterations is:
1
n(1 + · · ·+ (n − 2)) =
n
2− 3
2+
1
n=
O(n)
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 10 / 20
What is the asymptotic complexity of deleteNode?
Best case: O(1)
Worst case: This requires going into our loop n − 1 times. Thus, it isO(n), just like deleteFromTail.
Average case: Assume that every node has an equal chance of beingdeleted. How many iterations through the loop are there for each possibleposition:
First node: 0 iterations (special case)
Second node: 0 iterations
Third node: 1 iteration
...
Last node: n − 2 iterations
The average number of iterations is:
1
n(1 + · · ·+ (n − 2)) =
n
2− 3
2+
1
n= O(n)
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 10 / 20
Other Functions
Consider the following functions of IntSLList,
The search function isInList():
bool IntSLList : : isInList ( int el ) const {IntSLLNode ∗tmp ;for ( tmp = head ; tmp != 0 && ! ( tmp−>info == el ) ;
tmp = tmp−>next ) ;return tmp != 0 ;
}
Best case: O(1)
Worst case: O(n)
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 11 / 20
Other Functions
Consider the following functions of IntSLList,
The search function isInList():
bool IntSLList : : isInList ( int el ) const {IntSLLNode ∗tmp ;for ( tmp = head ; tmp != 0 && ! ( tmp−>info == el ) ;
tmp = tmp−>next ) ;return tmp != 0 ;
}
Best case: O(1)
Worst case: O(n)
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 11 / 20
Other Functions
Consider the following functions of IntSLList,
The search function isInList():
bool IntSLList : : isInList ( int el ) const {IntSLLNode ∗tmp ;for ( tmp = head ; tmp != 0 && ! ( tmp−>info == el ) ;
tmp = tmp−>next ) ;return tmp != 0 ;
}
Best case: O(1)
Worst case: O(n)
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 11 / 20
Other Functions
Consider the following functions of IntSLList,
The search function isInList():
bool IntSLList : : isInList ( int el ) const {IntSLLNode ∗tmp ;for ( tmp = head ; tmp != 0 && ! ( tmp−>info == el ) ;
tmp = tmp−>next ) ;return tmp != 0 ;
}
Best case: O(1)
Worst case: O(n)
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 11 / 20
Other Functions
Consider the following functions of IntSLList,
The search function isInList():
bool IntSLList : : isInList ( int el ) const {IntSLLNode ∗tmp ;for ( tmp = head ; tmp != 0 && ! ( tmp−>info == el ) ;
tmp = tmp−>next ) ;return tmp != 0 ;
}
Best case: O(1)
Worst case: O(n)
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 11 / 20
The destructor:
IntSLList : : ˜ IntSLList ( ) {for ( IntSLLNode ∗p ; ! isEmpty ( ) ; ) {
p = head−>next ;delete head ;head = p ;
}}
The printing function:
void IntSLList : : printAll ( ) const {for ( IntSLLNode ∗tmp = head ; tmp != 0 ; tmp = tmp−>next )
cout << tmp−>info << " " ;cout << endl ;
}
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 12 / 20
The destructor:
IntSLList : : ˜ IntSLList ( ) {for ( IntSLLNode ∗p ; ! isEmpty ( ) ; ) {
p = head−>next ;delete head ;head = p ;
}}
The printing function:
void IntSLList : : printAll ( ) const {for ( IntSLLNode ∗tmp = head ; tmp != 0 ; tmp = tmp−>next )
cout << tmp−>info << " " ;cout << endl ;
}
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 12 / 20
The destructor:
IntSLList : : ˜ IntSLList ( ) {for ( IntSLLNode ∗p ; ! isEmpty ( ) ; ) {
p = head−>next ;
delete head ;head = p ;
}}
The printing function:
void IntSLList : : printAll ( ) const {for ( IntSLLNode ∗tmp = head ; tmp != 0 ; tmp = tmp−>next )
cout << tmp−>info << " " ;cout << endl ;
}
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 12 / 20
The destructor:
IntSLList : : ˜ IntSLList ( ) {for ( IntSLLNode ∗p ; ! isEmpty ( ) ; ) {
p = head−>next ;delete head ;
head = p ;}
}
The printing function:
void IntSLList : : printAll ( ) const {for ( IntSLLNode ∗tmp = head ; tmp != 0 ; tmp = tmp−>next )
cout << tmp−>info << " " ;cout << endl ;
}
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 12 / 20
The destructor:
IntSLList : : ˜ IntSLList ( ) {for ( IntSLLNode ∗p ; ! isEmpty ( ) ; ) {
p = head−>next ;delete head ;head = p ;
}}
The printing function:
void IntSLList : : printAll ( ) const {for ( IntSLLNode ∗tmp = head ; tmp != 0 ; tmp = tmp−>next )
cout << tmp−>info << " " ;cout << endl ;
}
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 12 / 20
The destructor:
IntSLList : : ˜ IntSLList ( ) {for ( IntSLLNode ∗p ; ! isEmpty ( ) ; ) {
p = head−>next ;delete head ;head = p ;
}}
The printing function:
void IntSLList : : printAll ( ) const {for ( IntSLLNode ∗tmp = head ; tmp != 0 ; tmp = tmp−>next )
cout << tmp−>info << " " ;cout << endl ;
}
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 12 / 20
The destructor:
IntSLList : : ˜ IntSLList ( ) {for ( IntSLLNode ∗p ; ! isEmpty ( ) ; ) {
p = head−>next ;delete head ;head = p ;
}}
The printing function:
void IntSLList : : printAll ( ) const {for ( IntSLLNode ∗tmp = head ; tmp != 0 ; tmp = tmp−>next )
cout << tmp−>info << " " ;cout << endl ;
}
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 12 / 20
The destructor:
IntSLList : : ˜ IntSLList ( ) {for ( IntSLLNode ∗p ; ! isEmpty ( ) ; ) {
p = head−>next ;delete head ;head = p ;
}}
The printing function:
void IntSLList : : printAll ( ) const {for ( IntSLLNode ∗tmp = head ; tmp != 0 ; tmp = tmp−>next )
cout << tmp−>info << " " ;cout << endl ;
}
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 12 / 20
Doubly Linked Lists
Recall that the deleteFromTail function of our SLL was O(n).
We canimprove upon this by introducing doubly linked lists.
Each node in a DLL has two pointers:
One to the previous node in the list: prev
One to the next node in the list: next
At the front of the list the prev pointer is null. Similarly, at the rear next
is null.
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 13 / 20
Doubly Linked Lists
Recall that the deleteFromTail function of our SLL was O(n). We canimprove upon this by introducing doubly linked lists.
Each node in a DLL has two pointers:
One to the previous node in the list: prev
One to the next node in the list: next
At the front of the list the prev pointer is null. Similarly, at the rear next
is null.
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 13 / 20
Doubly Linked Lists
Recall that the deleteFromTail function of our SLL was O(n). We canimprove upon this by introducing doubly linked lists.
Each node in a DLL has two pointers:
One to the previous node in the list: prev
One to the next node in the list: next
At the front of the list the prev pointer is null. Similarly, at the rear next
is null.
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 13 / 20
Doubly Linked Lists
Recall that the deleteFromTail function of our SLL was O(n). We canimprove upon this by introducing doubly linked lists.
Each node in a DLL has two pointers:
One to the previous node in the list: prev
One to the next node in the list: next
At the front of the list the prev pointer is null. Similarly, at the rear next
is null.
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 13 / 20
Doubly Linked Lists
Recall that the deleteFromTail function of our SLL was O(n). We canimprove upon this by introducing doubly linked lists.
Each node in a DLL has two pointers:
One to the previous node in the list: prev
One to the next node in the list: next
At the front of the list the prev pointer is null. Similarly, at the rear next
is null.
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 13 / 20
Doubly Linked Lists
Recall that the deleteFromTail function of our SLL was O(n). We canimprove upon this by introducing doubly linked lists.
Each node in a DLL has two pointers:
One to the previous node in the list: prev
One to the next node in the list: next
At the front of the list the prev pointer is null. Similarly, at the rear next
is null.
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 13 / 20
Doubly Linked Lists
Recall that the deleteFromTail function of our SLL was O(n). We canimprove upon this by introducing doubly linked lists.
Each node in a DLL has two pointers:
One to the previous node in the list: prev
One to the next node in the list: next
At the front of the list the prev pointer is null. Similarly, at the rear next
is null.
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 13 / 20
Doubly Linked Lists
Recall that the deleteFromTail function of our SLL was O(n). We canimprove upon this by introducing doubly linked lists.
Each node in a DLL has two pointers:
One to the previous node in the list: prev
One to the next node in the list: next
At the front of the list the prev pointer is null.
Similarly, at the rear next
is null.
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 13 / 20
Doubly Linked Lists
Recall that the deleteFromTail function of our SLL was O(n). We canimprove upon this by introducing doubly linked lists.
Each node in a DLL has two pointers:
One to the previous node in the list: prev
One to the next node in the list: next
At the front of the list the prev pointer is null. Similarly, at the rear next
is null.ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 13 / 20
The definition of a node is modified to include these pointers, as well as tostore a generic type T.
template<class T>class DLLNode {public :
DLLNode ( ) {next = prev = 0 ;
}DLLNode ( const T& el , DLLNode ∗n = 0 , DLLNode ∗p = 0) {
info = el ; next = n ; prev = p ;}T info ;DLLNode ∗next , ∗prev ;
} ;
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 14 / 20
The definition of a node is modified to include these pointers, as well as tostore a generic type T.
template<class T>class DLLNode {public :
DLLNode ( ) {next = prev = 0 ;
}
DLLNode ( const T& el , DLLNode ∗n = 0 , DLLNode ∗p = 0) {info = el ; next = n ; prev = p ;
}T info ;DLLNode ∗next , ∗prev ;
} ;
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 14 / 20
The definition of a node is modified to include these pointers, as well as tostore a generic type T.
template<class T>class DLLNode {public :
DLLNode ( ) {next = prev = 0 ;
}DLLNode ( const T& el , DLLNode ∗n = 0 , DLLNode ∗p = 0) {
info = el ; next = n ; prev = p ;}
T info ;DLLNode ∗next , ∗prev ;
} ;
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 14 / 20
The definition of a node is modified to include these pointers, as well as tostore a generic type T.
template<class T>class DLLNode {public :
DLLNode ( ) {next = prev = 0 ;
}DLLNode ( const T& el , DLLNode ∗n = 0 , DLLNode ∗p = 0) {
info = el ; next = n ; prev = p ;}T info ;
DLLNode ∗next , ∗prev ;} ;
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 14 / 20
The definition of a node is modified to include these pointers, as well as tostore a generic type T.
template<class T>class DLLNode {public :
DLLNode ( ) {next = prev = 0 ;
}DLLNode ( const T& el , DLLNode ∗n = 0 , DLLNode ∗p = 0) {
info = el ; next = n ; prev = p ;}T info ;DLLNode ∗next , ∗prev ;
} ;
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 14 / 20
Consider the process of inserting into the last position of a doubly-linkedlist.
The following steps are required:
Create a new node:
info is setnext is set to nullprev is set to tail
Modify the current nodes to link in the new one:
tail is set to point at the new nodeThe next member of the former last node is set to point at the newnode
These steps are accomplished by the following member function:
template<class T>void DoublyLinkedList<T> : : addToDLLTail ( const T& el ) {
. . .tail = new DLLNode<T>(el , 0 , tail ) ;tail−>prev−>next = tail ;. . .
}
Consider the process of inserting into the last position of a doubly-linkedlist. The following steps are required:
Create a new node:
info is setnext is set to nullprev is set to tail
Modify the current nodes to link in the new one:
tail is set to point at the new nodeThe next member of the former last node is set to point at the newnode
These steps are accomplished by the following member function:
template<class T>void DoublyLinkedList<T> : : addToDLLTail ( const T& el ) {
. . .tail = new DLLNode<T>(el , 0 , tail ) ;tail−>prev−>next = tail ;. . .
}
Consider the process of inserting into the last position of a doubly-linkedlist. The following steps are required:
Create a new node:
info is setnext is set to
nullprev is set to tail
Modify the current nodes to link in the new one:
tail is set to point at the new nodeThe next member of the former last node is set to point at the newnode
These steps are accomplished by the following member function:
template<class T>void DoublyLinkedList<T> : : addToDLLTail ( const T& el ) {
. . .tail = new DLLNode<T>(el , 0 , tail ) ;tail−>prev−>next = tail ;. . .
}
Consider the process of inserting into the last position of a doubly-linkedlist. The following steps are required:
Create a new node:
info is set
next is set to
nullprev is set to tail
Modify the current nodes to link in the new one:
tail is set to point at the new nodeThe next member of the former last node is set to point at the newnode
These steps are accomplished by the following member function:
template<class T>void DoublyLinkedList<T> : : addToDLLTail ( const T& el ) {
. . .tail = new DLLNode<T>(el , 0 , tail ) ;tail−>prev−>next = tail ;. . .
}
Consider the process of inserting into the last position of a doubly-linkedlist. The following steps are required:
Create a new node:
info is setnext is set to
null
prev is set to tail
Modify the current nodes to link in the new one:
tail is set to point at the new nodeThe next member of the former last node is set to point at the newnode
These steps are accomplished by the following member function:
template<class T>void DoublyLinkedList<T> : : addToDLLTail ( const T& el ) {
. . .tail = new DLLNode<T>(el , 0 , tail ) ;tail−>prev−>next = tail ;. . .
}
Consider the process of inserting into the last position of a doubly-linkedlist. The following steps are required:
Create a new node:
info is setnext is set to
null
prev is set to tail
Modify the current nodes to link in the new one:
tail is set to point at the new nodeThe next member of the former last node is set to point at the newnode
These steps are accomplished by the following member function:
template<class T>void DoublyLinkedList<T> : : addToDLLTail ( const T& el ) {
. . .tail = new DLLNode<T>(el , 0 , tail ) ;tail−>prev−>next = tail ;. . .
}
Consider the process of inserting into the last position of a doubly-linkedlist. The following steps are required:
Create a new node:
info is setnext is set to null
prev is set to tail
Modify the current nodes to link in the new one:
tail is set to point at the new nodeThe next member of the former last node is set to point at the newnode
These steps are accomplished by the following member function:
template<class T>void DoublyLinkedList<T> : : addToDLLTail ( const T& el ) {
. . .tail = new DLLNode<T>(el , 0 , tail ) ;tail−>prev−>next = tail ;. . .
}
Consider the process of inserting into the last position of a doubly-linkedlist. The following steps are required:
Create a new node:
info is setnext is set to nullprev is set to
tail
Modify the current nodes to link in the new one:
tail is set to point at the new nodeThe next member of the former last node is set to point at the newnode
These steps are accomplished by the following member function:
template<class T>void DoublyLinkedList<T> : : addToDLLTail ( const T& el ) {
. . .tail = new DLLNode<T>(el , 0 , tail ) ;tail−>prev−>next = tail ;. . .
}
Consider the process of inserting into the last position of a doubly-linkedlist. The following steps are required:
Create a new node:
info is setnext is set to nullprev is set to
tail
Modify the current nodes to link in the new one:
tail is set to point at the new nodeThe next member of the former last node is set to point at the newnode
These steps are accomplished by the following member function:
template<class T>void DoublyLinkedList<T> : : addToDLLTail ( const T& el ) {
. . .tail = new DLLNode<T>(el , 0 , tail ) ;tail−>prev−>next = tail ;. . .
}
Consider the process of inserting into the last position of a doubly-linkedlist. The following steps are required:
Create a new node:
info is setnext is set to nullprev is set to tail
Modify the current nodes to link in the new one:
tail is set to point at the new nodeThe next member of the former last node is set to point at the newnode
These steps are accomplished by the following member function:
template<class T>void DoublyLinkedList<T> : : addToDLLTail ( const T& el ) {
. . .tail = new DLLNode<T>(el , 0 , tail ) ;tail−>prev−>next = tail ;. . .
}
Consider the process of inserting into the last position of a doubly-linkedlist. The following steps are required:
Create a new node:
info is setnext is set to nullprev is set to tail
Modify the current nodes to link in the new one:
tail is set to point at the new nodeThe next member of the former last node is set to point at the newnode
These steps are accomplished by the following member function:
template<class T>void DoublyLinkedList<T> : : addToDLLTail ( const T& el ) {
. . .tail = new DLLNode<T>(el , 0 , tail ) ;tail−>prev−>next = tail ;. . .
}
Consider the process of inserting into the last position of a doubly-linkedlist. The following steps are required:
Create a new node:
info is setnext is set to nullprev is set to tail
Modify the current nodes to link in the new one:
tail is set to point at the new node
The next member of the former last node is set to point at the newnode
These steps are accomplished by the following member function:
template<class T>void DoublyLinkedList<T> : : addToDLLTail ( const T& el ) {
. . .tail = new DLLNode<T>(el , 0 , tail ) ;tail−>prev−>next = tail ;. . .
}
Consider the process of inserting into the last position of a doubly-linkedlist. The following steps are required:
Create a new node:
info is setnext is set to nullprev is set to tail
Modify the current nodes to link in the new one:
tail is set to point at the new nodeThe next member of the former last node is set to point at the newnode
These steps are accomplished by the following member function:
template<class T>void DoublyLinkedList<T> : : addToDLLTail ( const T& el ) {
. . .tail = new DLLNode<T>(el , 0 , tail ) ;tail−>prev−>next = tail ;. . .
}
Consider the process of inserting into the last position of a doubly-linkedlist. The following steps are required:
Create a new node:
info is setnext is set to nullprev is set to tail
Modify the current nodes to link in the new one:
tail is set to point at the new nodeThe next member of the former last node is set to point at the newnode
These steps are accomplished by the following member function:
template<class T>void DoublyLinkedList<T> : : addToDLLTail ( const T& el ) {
. . .tail = new DLLNode<T>(el , 0 , tail ) ;tail−>prev−>next = tail ;. . .
}
Consider the process of inserting into the last position of a doubly-linkedlist. The following steps are required:
Create a new node:
info is setnext is set to nullprev is set to tail
Modify the current nodes to link in the new one:
tail is set to point at the new nodeThe next member of the former last node is set to point at the newnode
These steps are accomplished by the following member function:
template<class T>void DoublyLinkedList<T> : : addToDLLTail ( const T& el ) {
. . .tail = new DLLNode<T>(el , 0 , tail ) ;tail−>prev−>next = tail ;. . .
}
Consider the process of inserting into the last position of a doubly-linkedlist. The following steps are required:
Create a new node:
info is setnext is set to nullprev is set to tail
Modify the current nodes to link in the new one:
tail is set to point at the new nodeThe next member of the former last node is set to point at the newnode
These steps are accomplished by the following member function:
template<class T>void DoublyLinkedList<T> : : addToDLLTail ( const T& el ) {
. . .tail = new DLLNode<T>(el , 0 , tail ) ;
tail−>prev−>next = tail ;. . .
}
Consider the process of inserting into the last position of a doubly-linkedlist. The following steps are required:
Create a new node:
info is setnext is set to nullprev is set to tail
Modify the current nodes to link in the new one:
tail is set to point at the new nodeThe next member of the former last node is set to point at the newnode
These steps are accomplished by the following member function:
template<class T>void DoublyLinkedList<T> : : addToDLLTail ( const T& el ) {
. . .tail = new DLLNode<T>(el , 0 , tail ) ;tail−>prev−>next = tail ;. . .
}
Special case: What if the new node has no predecessor (i.e. the list isempty).
The last step is removed. Also, we have to worry about settinghead.
The complete code for addToDLLTail is as follows:
template<class T>void DoublyLinkedList<T> : : addToDLLTail ( const T& el ) {
if ( tail != 0) {tail = new DLLNode<T>(el , 0 , tail ) ;tail−>prev−>next = tail ;
}else head = tail = new DLLNode<T>(el ) ;
}
Special case: What if the new node has no predecessor (i.e. the list isempty). The last step is removed.
Also, we have to worry about settinghead.
The complete code for addToDLLTail is as follows:
template<class T>void DoublyLinkedList<T> : : addToDLLTail ( const T& el ) {
if ( tail != 0) {tail = new DLLNode<T>(el , 0 , tail ) ;tail−>prev−>next = tail ;
}else head = tail = new DLLNode<T>(el ) ;
}
Special case: What if the new node has no predecessor (i.e. the list isempty). The last step is removed. Also, we have to worry about settinghead.
The complete code for addToDLLTail is as follows:
template<class T>void DoublyLinkedList<T> : : addToDLLTail ( const T& el ) {
if ( tail != 0) {tail = new DLLNode<T>(el , 0 , tail ) ;tail−>prev−>next = tail ;
}else head = tail = new DLLNode<T>(el ) ;
}
Special case: What if the new node has no predecessor (i.e. the list isempty). The last step is removed. Also, we have to worry about settinghead.
The complete code for addToDLLTail is as follows:
template<class T>void DoublyLinkedList<T> : : addToDLLTail ( const T& el ) {
if ( tail != 0) {tail = new DLLNode<T>(el , 0 , tail ) ;tail−>prev−>next = tail ;
}else head = tail = new DLLNode<T>(el ) ;
}
Special case: What if the new node has no predecessor (i.e. the list isempty). The last step is removed. Also, we have to worry about settinghead.
The complete code for addToDLLTail is as follows:
template<class T>void DoublyLinkedList<T> : : addToDLLTail ( const T& el ) {
if ( tail != 0) {
tail = new DLLNode<T>(el , 0 , tail ) ;tail−>prev−>next = tail ;
}else head = tail = new DLLNode<T>(el ) ;
}
Special case: What if the new node has no predecessor (i.e. the list isempty). The last step is removed. Also, we have to worry about settinghead.
The complete code for addToDLLTail is as follows:
template<class T>void DoublyLinkedList<T> : : addToDLLTail ( const T& el ) {
if ( tail != 0) {tail = new DLLNode<T>(el , 0 , tail ) ;tail−>prev−>next = tail ;
}
else head = tail = new DLLNode<T>(el ) ;}
Special case: What if the new node has no predecessor (i.e. the list isempty). The last step is removed. Also, we have to worry about settinghead.
The complete code for addToDLLTail is as follows:
template<class T>void DoublyLinkedList<T> : : addToDLLTail ( const T& el ) {
if ( tail != 0) {tail = new DLLNode<T>(el , 0 , tail ) ;tail−>prev−>next = tail ;
}else head = tail = new DLLNode<T>(el ) ;
}
Consider deletion of the last node.
This should be much easier than for aSLL because we don’t have to loop to find tail’s predecessor:
For the general case (i.e. list length ≥ 2),
Store the element to delete
Shift tail back by one: tail = tail−>prevDelete last node: delete tail−>nextUpdate the dangling pointer of the last node: tail−>next = 0
Special case: Empty list. Handle this by forbidding it with a precondition.
Special case: One node in list. Delete node and set head = tail = 0
Consider deletion of the last node. This should be much easier than for aSLL because we don’t have to loop to find tail’s predecessor:
For the general case (i.e. list length ≥ 2),
Store the element to delete
Shift tail back by one: tail = tail−>prevDelete last node: delete tail−>nextUpdate the dangling pointer of the last node: tail−>next = 0
Special case: Empty list. Handle this by forbidding it with a precondition.
Special case: One node in list. Delete node and set head = tail = 0
Consider deletion of the last node. This should be much easier than for aSLL because we don’t have to loop to find tail’s predecessor:
For the general case (i.e. list length ≥ 2),
Store the element to delete
Shift tail back by one: tail = tail−>prevDelete last node: delete tail−>nextUpdate the dangling pointer of the last node: tail−>next = 0
Special case: Empty list. Handle this by forbidding it with a precondition.
Special case: One node in list. Delete node and set head = tail = 0
Consider deletion of the last node. This should be much easier than for aSLL because we don’t have to loop to find tail’s predecessor:
For the general case (i.e. list length ≥ 2),
Store the element to delete
Shift tail back by one:
tail = tail−>prevDelete last node: delete tail−>nextUpdate the dangling pointer of the last node: tail−>next = 0
Special case: Empty list. Handle this by forbidding it with a precondition.
Special case: One node in list. Delete node and set head = tail = 0
Consider deletion of the last node. This should be much easier than for aSLL because we don’t have to loop to find tail’s predecessor:
For the general case (i.e. list length ≥ 2),
Store the element to delete
Shift tail back by one:
tail = tail−>prev
Delete last node: delete tail−>nextUpdate the dangling pointer of the last node: tail−>next = 0
Special case: Empty list. Handle this by forbidding it with a precondition.
Special case: One node in list. Delete node and set head = tail = 0
Consider deletion of the last node. This should be much easier than for aSLL because we don’t have to loop to find tail’s predecessor:
For the general case (i.e. list length ≥ 2),
Store the element to delete
Shift tail back by one:
tail = tail−>prev
Delete last node: delete tail−>nextUpdate the dangling pointer of the last node: tail−>next = 0
Special case: Empty list. Handle this by forbidding it with a precondition.
Special case: One node in list. Delete node and set head = tail = 0
Consider deletion of the last node. This should be much easier than for aSLL because we don’t have to loop to find tail’s predecessor:
For the general case (i.e. list length ≥ 2),
Store the element to delete
Shift tail back by one: tail = tail−>prev
Delete last node: delete tail−>nextUpdate the dangling pointer of the last node: tail−>next = 0
Special case: Empty list. Handle this by forbidding it with a precondition.
Special case: One node in list. Delete node and set head = tail = 0
Consider deletion of the last node. This should be much easier than for aSLL because we don’t have to loop to find tail’s predecessor:
For the general case (i.e. list length ≥ 2),
Store the element to delete
Shift tail back by one: tail = tail−>prevDelete last node:
delete tail−>next
Update the dangling pointer of the last node: tail−>next = 0
Special case: Empty list. Handle this by forbidding it with a precondition.
Special case: One node in list. Delete node and set head = tail = 0
Consider deletion of the last node. This should be much easier than for aSLL because we don’t have to loop to find tail’s predecessor:
For the general case (i.e. list length ≥ 2),
Store the element to delete
Shift tail back by one: tail = tail−>prevDelete last node:
delete tail−>next
Update the dangling pointer of the last node: tail−>next = 0
Special case: Empty list. Handle this by forbidding it with a precondition.
Special case: One node in list. Delete node and set head = tail = 0
Consider deletion of the last node. This should be much easier than for aSLL because we don’t have to loop to find tail’s predecessor:
For the general case (i.e. list length ≥ 2),
Store the element to delete
Shift tail back by one: tail = tail−>prevDelete last node: delete tail−>next
Update the dangling pointer of the last node: tail−>next = 0
Special case: Empty list. Handle this by forbidding it with a precondition.
Special case: One node in list. Delete node and set head = tail = 0
Consider deletion of the last node. This should be much easier than for aSLL because we don’t have to loop to find tail’s predecessor:
For the general case (i.e. list length ≥ 2),
Store the element to delete
Shift tail back by one: tail = tail−>prevDelete last node: delete tail−>nextUpdate the dangling pointer of the last node:
tail−>next = 0
Special case: Empty list. Handle this by forbidding it with a precondition.
Special case: One node in list. Delete node and set head = tail = 0
Consider deletion of the last node. This should be much easier than for aSLL because we don’t have to loop to find tail’s predecessor:
For the general case (i.e. list length ≥ 2),
Store the element to delete
Shift tail back by one: tail = tail−>prevDelete last node: delete tail−>nextUpdate the dangling pointer of the last node:
tail−>next = 0
Special case: Empty list. Handle this by forbidding it with a precondition.
Special case: One node in list. Delete node and set head = tail = 0
Consider deletion of the last node. This should be much easier than for aSLL because we don’t have to loop to find tail’s predecessor:
For the general case (i.e. list length ≥ 2),
Store the element to delete
Shift tail back by one: tail = tail−>prevDelete last node: delete tail−>nextUpdate the dangling pointer of the last node: tail−>next = 0
Special case: Empty list. Handle this by forbidding it with a precondition.
Special case: One node in list. Delete node and set head = tail = 0
Consider deletion of the last node. This should be much easier than for aSLL because we don’t have to loop to find tail’s predecessor:
For the general case (i.e. list length ≥ 2),
Store the element to delete
Shift tail back by one: tail = tail−>prevDelete last node: delete tail−>nextUpdate the dangling pointer of the last node: tail−>next = 0
Special case: Empty list. Handle this by forbidding it with a precondition.
Special case: One node in list. Delete node and set head = tail = 0
Consider deletion of the last node. This should be much easier than for aSLL because we don’t have to loop to find tail’s predecessor:
For the general case (i.e. list length ≥ 2),
Store the element to delete
Shift tail back by one: tail = tail−>prevDelete last node: delete tail−>nextUpdate the dangling pointer of the last node: tail−>next = 0
Special case: Empty list.
Handle this by forbidding it with a precondition.
Special case: One node in list. Delete node and set head = tail = 0
Consider deletion of the last node. This should be much easier than for aSLL because we don’t have to loop to find tail’s predecessor:
For the general case (i.e. list length ≥ 2),
Store the element to delete
Shift tail back by one: tail = tail−>prevDelete last node: delete tail−>nextUpdate the dangling pointer of the last node: tail−>next = 0
Special case: Empty list. Handle this by forbidding it with a precondition.
Special case: One node in list. Delete node and set head = tail = 0
Consider deletion of the last node. This should be much easier than for aSLL because we don’t have to loop to find tail’s predecessor:
For the general case (i.e. list length ≥ 2),
Store the element to delete
Shift tail back by one: tail = tail−>prevDelete last node: delete tail−>nextUpdate the dangling pointer of the last node: tail−>next = 0
Special case: Empty list. Handle this by forbidding it with a precondition.
Special case: One node in list.
Delete node and set head = tail = 0
Consider deletion of the last node. This should be much easier than for aSLL because we don’t have to loop to find tail’s predecessor:
For the general case (i.e. list length ≥ 2),
Store the element to delete
Shift tail back by one: tail = tail−>prevDelete last node: delete tail−>nextUpdate the dangling pointer of the last node: tail−>next = 0
Special case: Empty list. Handle this by forbidding it with a precondition.
Special case: One node in list. Delete node and set head = tail = 0
template<class T>T DoublyLinkedList<T> : : deleteFromDLLTail ( ) {
T el = tail−>info ;if ( head == tail ) { // o n l y one node i n l i s t
delete head ;head = tail = 0 ;
}
else { // more than one nodetail = tail−>prev ;delete tail−>next ;tail−>next = 0 ;
}}
What is the asymptotic complexity of deleteFromDLLTail? O(1)
DLL’s may also tend be more efficient than SLL’s in situations whereoperations occur repeatedly around one part of the list (e.g. text editingwhere each word is represented by a node).
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 18 / 20
template<class T>T DoublyLinkedList<T> : : deleteFromDLLTail ( ) {
T el = tail−>info ;if ( head == tail ) { // o n l y one node i n l i s t
delete head ;head = tail = 0 ;
}else { // more than one node
tail = tail−>prev ;delete tail−>next ;tail−>next = 0 ;
}}
What is the asymptotic complexity of deleteFromDLLTail? O(1)
DLL’s may also tend be more efficient than SLL’s in situations whereoperations occur repeatedly around one part of the list (e.g. text editingwhere each word is represented by a node).
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 18 / 20
template<class T>T DoublyLinkedList<T> : : deleteFromDLLTail ( ) {
T el = tail−>info ;if ( head == tail ) { // o n l y one node i n l i s t
delete head ;head = tail = 0 ;
}else { // more than one node
tail = tail−>prev ;delete tail−>next ;tail−>next = 0 ;
}}
What is the asymptotic complexity of deleteFromDLLTail?
O(1)
DLL’s may also tend be more efficient than SLL’s in situations whereoperations occur repeatedly around one part of the list (e.g. text editingwhere each word is represented by a node).
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 18 / 20
template<class T>T DoublyLinkedList<T> : : deleteFromDLLTail ( ) {
T el = tail−>info ;if ( head == tail ) { // o n l y one node i n l i s t
delete head ;head = tail = 0 ;
}else { // more than one node
tail = tail−>prev ;delete tail−>next ;tail−>next = 0 ;
}}
What is the asymptotic complexity of deleteFromDLLTail? O(1)
DLL’s may also tend be more efficient than SLL’s in situations whereoperations occur repeatedly around one part of the list (e.g. text editingwhere each word is represented by a node).
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 18 / 20
template<class T>T DoublyLinkedList<T> : : deleteFromDLLTail ( ) {
T el = tail−>info ;if ( head == tail ) { // o n l y one node i n l i s t
delete head ;head = tail = 0 ;
}else { // more than one node
tail = tail−>prev ;delete tail−>next ;tail−>next = 0 ;
}}
What is the asymptotic complexity of deleteFromDLLTail? O(1)
DLL’s may also tend be more efficient than SLL’s in situations whereoperations occur repeatedly around one part of the list (e.g. text editingwhere each word is represented by a node).
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 18 / 20
Circular lists
In some cases, we may have a list of items that we wish to continuouslycircle through.
e.g. processes sharing some resource such as CPU time.
A circular list (a.k.a circular linked list) would be a suitable data structurefor this purpose,
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 19 / 20
Circular lists
In some cases, we may have a list of items that we wish to continuouslycircle through. e.g. processes sharing some resource such as CPU time.
A circular list (a.k.a circular linked list) would be a suitable data structurefor this purpose,
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 19 / 20
Circular lists
In some cases, we may have a list of items that we wish to continuouslycircle through. e.g. processes sharing some resource such as CPU time.
A circular list (a.k.a circular linked list) would be a suitable data structurefor this purpose,
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 19 / 20
Circular lists
In some cases, we may have a list of items that we wish to continuouslycircle through. e.g. processes sharing some resource such as CPU time.
A circular list (a.k.a circular linked list) would be a suitable data structurefor this purpose,
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 19 / 20
Circular lists
In some cases, we may have a list of items that we wish to continuouslycircle through. e.g. processes sharing some resource such as CPU time.
A circular list (a.k.a circular linked list) would be a suitable data structurefor this purpose,
ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 19 / 20
Linked lists vs. arrays
We compare arrays with DLL’s.
DLL’s have the best asymptoticperformance for all operations over other LL variants. (Although they maybe less efficient in some cases due to the overhead of additional pointers)
Operation Array Linked list
Indexing 1 O(1) O(n)Search O(n) / O(lg n) 2 O(n)Inserting / Deleting at beginning O(n) O(1)Inserting / Deleting at end O(n) 3 O(1)Inserting / Deleting in middle 4 O(n) O(1)
1Indexing means to access a particular element via an index. Accessing the 100th
element in an array is done by pointer arithmetic. Accessing the 100th node in a linkedlist requires iterating through the first 99 nodes.
2Searching in an ordered array can be done in O(lg n) using binary search.3O(1) if space is available at the end of the array.4Not counting search cost
Linked lists vs. arrays
We compare arrays with DLL’s. DLL’s have the best asymptoticperformance for all operations over other LL variants.
(Although they maybe less efficient in some cases due to the overhead of additional pointers)
Operation Array Linked list
Indexing 1 O(1) O(n)Search O(n) / O(lg n) 2 O(n)Inserting / Deleting at beginning O(n) O(1)Inserting / Deleting at end O(n) 3 O(1)Inserting / Deleting in middle 4 O(n) O(1)
1Indexing means to access a particular element via an index. Accessing the 100th
element in an array is done by pointer arithmetic. Accessing the 100th node in a linkedlist requires iterating through the first 99 nodes.
2Searching in an ordered array can be done in O(lg n) using binary search.3O(1) if space is available at the end of the array.4Not counting search cost
Linked lists vs. arrays
We compare arrays with DLL’s. DLL’s have the best asymptoticperformance for all operations over other LL variants. (Although they maybe less efficient in some cases due to the overhead of additional pointers)
Operation Array Linked list
Indexing 1 O(1) O(n)Search O(n) / O(lg n) 2 O(n)Inserting / Deleting at beginning O(n) O(1)Inserting / Deleting at end O(n) 3 O(1)Inserting / Deleting in middle 4 O(n) O(1)
1Indexing means to access a particular element via an index. Accessing the 100th
element in an array is done by pointer arithmetic. Accessing the 100th node in a linkedlist requires iterating through the first 99 nodes.
2Searching in an ordered array can be done in O(lg n) using binary search.3O(1) if space is available at the end of the array.4Not counting search cost
Linked lists vs. arrays
We compare arrays with DLL’s. DLL’s have the best asymptoticperformance for all operations over other LL variants. (Although they maybe less efficient in some cases due to the overhead of additional pointers)
Operation Array Linked list
Indexing 1 O(1) O(n)Search O(n) / O(lg n) 2 O(n)Inserting / Deleting at beginning O(n) O(1)Inserting / Deleting at end O(n) 3 O(1)Inserting / Deleting in middle 4 O(n) O(1)
1Indexing means to access a particular element via an index. Accessing the 100th
element in an array is done by pointer arithmetic. Accessing the 100th node in a linkedlist requires iterating through the first 99 nodes.
2Searching in an ordered array can be done in O(lg n) using binary search.3O(1) if space is available at the end of the array.4Not counting search cost
Linked lists vs. arrays
We compare arrays with DLL’s. DLL’s have the best asymptoticperformance for all operations over other LL variants. (Although they maybe less efficient in some cases due to the overhead of additional pointers)
Operation Array Linked list
Indexing 1 O(1) O(n)
Search O(n) / O(lg n) 2 O(n)Inserting / Deleting at beginning O(n) O(1)Inserting / Deleting at end O(n) 3 O(1)Inserting / Deleting in middle 4 O(n) O(1)
1Indexing means to access a particular element via an index. Accessing the 100th
element in an array is done by pointer arithmetic. Accessing the 100th node in a linkedlist requires iterating through the first 99 nodes.
2Searching in an ordered array can be done in O(lg n) using binary search.3O(1) if space is available at the end of the array.4Not counting search cost
Linked lists vs. arrays
We compare arrays with DLL’s. DLL’s have the best asymptoticperformance for all operations over other LL variants. (Although they maybe less efficient in some cases due to the overhead of additional pointers)
Operation Array Linked list
Indexing 1 O(1) O(n)Search O(n) / O(lg n) 2 O(n)
Inserting / Deleting at beginning O(n) O(1)Inserting / Deleting at end O(n) 3 O(1)Inserting / Deleting in middle 4 O(n) O(1)
1Indexing means to access a particular element via an index. Accessing the 100th
element in an array is done by pointer arithmetic. Accessing the 100th node in a linkedlist requires iterating through the first 99 nodes.
2Searching in an ordered array can be done in O(lg n) using binary search.3O(1) if space is available at the end of the array.4Not counting search cost
Linked lists vs. arrays
We compare arrays with DLL’s. DLL’s have the best asymptoticperformance for all operations over other LL variants. (Although they maybe less efficient in some cases due to the overhead of additional pointers)
Operation Array Linked list
Indexing 1 O(1) O(n)Search O(n) / O(lg n) 2 O(n)Inserting / Deleting at beginning O(n) O(1)
Inserting / Deleting at end O(n) 3 O(1)Inserting / Deleting in middle 4 O(n) O(1)
1Indexing means to access a particular element via an index. Accessing the 100th
element in an array is done by pointer arithmetic. Accessing the 100th node in a linkedlist requires iterating through the first 99 nodes.
2Searching in an ordered array can be done in O(lg n) using binary search.3O(1) if space is available at the end of the array.4Not counting search cost
Linked lists vs. arrays
We compare arrays with DLL’s. DLL’s have the best asymptoticperformance for all operations over other LL variants. (Although they maybe less efficient in some cases due to the overhead of additional pointers)
Operation Array Linked list
Indexing 1 O(1) O(n)Search O(n) / O(lg n) 2 O(n)Inserting / Deleting at beginning O(n) O(1)Inserting / Deleting at end O(n) 3 O(1)
Inserting / Deleting in middle 4 O(n) O(1)
1Indexing means to access a particular element via an index. Accessing the 100th
element in an array is done by pointer arithmetic. Accessing the 100th node in a linkedlist requires iterating through the first 99 nodes.
2Searching in an ordered array can be done in O(lg n) using binary search.3O(1) if space is available at the end of the array.4Not counting search cost
Linked lists vs. arrays
We compare arrays with DLL’s. DLL’s have the best asymptoticperformance for all operations over other LL variants. (Although they maybe less efficient in some cases due to the overhead of additional pointers)
Operation Array Linked list
Indexing 1 O(1) O(n)Search O(n) / O(lg n) 2 O(n)Inserting / Deleting at beginning O(n) O(1)Inserting / Deleting at end O(n) 3 O(1)Inserting / Deleting in middle 4 O(n) O(1)
1Indexing means to access a particular element via an index. Accessing the 100th
element in an array is done by pointer arithmetic. Accessing the 100th node in a linkedlist requires iterating through the first 99 nodes.
2Searching in an ordered array can be done in O(lg n) using binary search.3O(1) if space is available at the end of the array.4Not counting search cost