unit 3: linked lists part 2: more on linked lists · 1 deletion in singly linked lists (cont’d) 1...

150
Unit 3: Linked Lists Part 2: More on Linked Lists Engineering 4892: Data Structures Faculty of Engineering & Applied Science Memorial University of Newfoundland May 30, 2011 ENGI 4892 (MUN) Unit 3, Part 2 May 30, 2011 1 / 20

Upload: others

Post on 18-Jul-2020

24 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 2: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 3: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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.

Page 4: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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.

Page 5: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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.

Page 6: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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.

Page 7: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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.

Page 8: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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.

Page 9: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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.

Page 10: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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.

Page 11: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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.

Page 12: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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.

Page 13: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 14: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 15: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 16: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 17: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 18: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 19: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 20: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 21: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 22: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 23: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 24: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 25: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 26: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 27: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 28: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 29: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 30: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 31: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 32: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 33: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 34: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 35: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 36: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 37: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 38: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 39: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 40: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 41: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 42: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 43: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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 ;

}}

}

Page 44: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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 ;

}}

}

Page 45: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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 ;

}}

}

Page 46: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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 ;

}}

}

Page 47: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 48: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 49: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 50: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 51: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 52: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 53: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 54: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 55: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 56: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 57: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 58: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 59: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 60: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 61: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 62: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 63: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 64: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 65: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 66: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 67: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 68: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 69: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 70: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 71: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 72: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 73: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 74: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 75: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 76: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 77: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 78: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 79: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 80: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 81: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 82: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 83: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 84: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 85: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 86: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 87: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 88: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 89: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 90: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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 ;. . .

}

Page 91: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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 ;. . .

}

Page 92: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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 ;. . .

}

Page 93: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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 ;. . .

}

Page 94: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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 ;. . .

}

Page 95: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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 ;. . .

}

Page 96: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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 ;. . .

}

Page 97: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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 ;. . .

}

Page 98: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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 ;. . .

}

Page 99: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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 ;. . .

}

Page 100: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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 ;. . .

}

Page 101: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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 ;. . .

}

Page 102: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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 ;. . .

}

Page 103: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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 ;. . .

}

Page 104: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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 ;. . .

}

Page 105: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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 ;. . .

}

Page 106: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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 ;. . .

}

Page 107: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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 ) ;

}

Page 108: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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 ) ;

}

Page 109: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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 ) ;

}

Page 110: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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 ) ;

}

Page 111: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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 ) ;

}

Page 112: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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 ) ;}

Page 113: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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 ) ;

}

Page 114: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 115: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 116: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 117: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 118: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 119: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 120: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 121: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 122: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 123: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 124: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 125: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 126: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 127: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 128: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 129: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 130: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 131: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 132: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 133: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 134: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 135: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 136: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 137: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 138: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 139: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 140: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 141: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 142: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 143: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 144: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 145: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 146: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 147: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 148: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 149: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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

Page 150: Unit 3: Linked Lists Part 2: More on Linked Lists · 1 Deletion in singly linked lists (cont’d) 1 Other Functions 1 Doubly Linked Lists 1 Circular lists 1 Linked lists vs. arrays

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