Академический Документы
Профессиональный Документы
Культура Документы
Headptr
Finding a List Item (cont.)
Headptr
Inserting into a Doubly Linked
List
Headptr
Headptr
If(location->left != NULL)
location = location->left
Finding a List Item (cont.)
void FindItem( Node* listData, string item,
struct Node *&location, bool &found)
{
// precondition: list is not empty
Headptr
Special case: inserting in the
beginning
Headptr
Inserting into a Doubly
Linked List
void InsertItem(string item)
{
Headptr
struct Node *newNode;
struct Node *location;
bool found;
newNode = new Node;
newNode->data = item;
if (listData != NULL) {
FindItem(listData, item, location, found);
if (location->data > item) {
newNode->right = location->right; else
newNode->left = location; headptr = newNode; (3)
(4)
(1) location->back = newNode;
if (location != headptr) // special case
(location->right)->left = newNode; } Headptr
(2)
(3)
Inserting into a Doubly Linked
List
else { // insert at the end
newNode->right = location;
location->left= newNode;
newNode->left = NULL;
}
}
else { // insert into an empty list
listData = newNode;
newNode->left = NULL; Headptr
newNode->right = NULL;
}
length++;
}
Deleting from a Doubly Linked
List
Headptr
Headptr
A linked list as
an array of records
What are the advantages of using
linked lists?
(1) Dynamic memory allocation
(2) Efficient insertion-deletion (for sorted
lists)
Can we implement a linked list without
dynamic memory allocation ?
A linked
list as an
array of
records
(cont.)
Case Study: Implementing a
large integer ADT
The range of integer values varies from
one computer to another
For long integers, the range is
[-2,147,483,648 to 2,147,483,647]
How can we manipulate larger integers?
Case Study: Implementing a
large integer ADT (cont.)