Topics
 Meaning of a Linked List
 Meaning of a Dynamic Linked List
 Traversal, Insertion and Deletion of Elements in a
Dynamic Linked List
 Specification of a Dynamic Linked Sorted List
 Insertion and Deletion of Elements in a Dynamic
Linked Sorted List
To implement the List ADT
The programmer must
1) choose a concrete data
representation for the list, and
2) implement the list operations
Recall:
4 Basic Kinds of ADT Operations
 Constructors -- create a new instance
(object) of an ADT
 Transformers -- change the state of one or
more of the data values of an instance
 Observers -- allow client to observe the
state of one or more of the data values of
an instance without changing them
 Iterators -- allow client to access the data
values in sequence
List Operations
Transformers
 Insert
 Delete
 Sort
Observers
 IsEmpty
 IsFull
 Length
 IsPresent
change state
observe state
4
ADT List Operations
Iterator
 Reset
 GetNextItem
 Reset prepares for the iteration
 GetNextItem returns the next item in
sequence
 No transformer can be called between
calls to GetNextItem (Why?)
Iteration Pair
Array-based class List
Reset
IsFull
Length
IsPresent
Delete
IsEmpty
Insert
GetNexItem
Private data:
length
data [0]
[1]
[2]
[MAX_LENGTH-1]
currentPos
SelSort
// Specification file array-based list (list.h)
const int MAX_LENGTH = 50;
typedef int ItemType;
class List // Declares a class data type
{
public: // Public member functions
List(); // constructor
bool IsEmpty () const;
bool IsFull () const;
int Length () const; // Returns length of list
void Insert (ItemType item);
void Delete (ItemType item);
bool IsPresent(ItemType item) const;
void SelSort ();
void Reset ();
ItemType GetNextItem ();
private: // Private data members
int length; // Number of values currently stored
ItemType data[MAX_LENGTH];
int CurrentPos; // Used in iteration
};
7
Implementation Structures
 Use a built-in array stored in contiguous
memory locations, implementing operations
Insert and Delete by moving list items
around in the array, as needed
 Use a linked list in which items are not
necessarily stored in contiguous memory
locations
 A linked list avoids excessive data movement
from insertions and deletions
Implementation Possibilities for a List
List
Linked list
Built-in array
Built-in
dynamic data
and pointers
Built-in array
of structs
A Linked List
 A linked list is a list in which the order of
the components is determined by an
explicit link member in each node
 Each node is a struct containing a data
member and a link member that gives the
location of the next node in the list
head ‘X’ ‘C’ ‘L’
Dynamic Linked List
head “Ted” “Irv” “Lee”
 A dynamic linked list is one in which the
nodes are linked together by pointers and
an external pointer (or head pointer)
points to the first node in the list
Nodes can be located anywhere in memory
 The link member holds the memory
address of the next node in the list
head 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
// Type declarations
struct NodeType
{
char info;
NodeType* link;
}
typedef NodeType* NodePtr;
// Variable DECLARATIONS
NodePtr head;
NodePtr ptr;
13
Declarations for a Dynamic Linked List
.info .link
‘A’ 6000
Pointer Dereferencing and Member Selection
.info .link
‘A’ 6000
ptr
ptr
ptr
.info .link
‘A’ 6000
*ptr
ptr
.info .link
(*ptr).info
ptr->info
‘A’ 6000
15
ptr is a pointer to a node
.info .link
‘A’ 6000
ptr
ptr
16
*ptr is the entire node pointed to by ptr
ptr
.info .link
‘A’ 6000
*ptr
17
ptr->info is a node member
ptr
.info .link
ptr->info
(*ptr).info // Equivalent
‘A’ 6000
18
ptr->link is a node member
ptr
.info .link
ptr->link
(*ptr).link // Equivalent
‘A’ 6000
Traversing a Dynamic Linked List
// Pre: head points to a dynamic linked list
ptr = head;
while (ptr != NULL)
{
cout << ptr->info;
// Or, do something else with node *ptr
ptr = ptr->link;
}
ptr
3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
head
// Pre: head points to a dynamic linked list
ptr = head;
while (ptr != NULL)
{
cout << ptr->info;
// Or, do something else with node *ptr
ptr = ptr->link;
}
ptr 3000
3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
head
Traversing a Dynamic Linked List
// Pre: head points to a dynamic linked list
ptr = head;
while (ptr != NULL)
{
cout << ptr->info;
// Or, do something else with node *ptr
ptr = ptr->link;
}
ptr 3000
3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
head
Traversing a Dynamic Linked List
// Pre: head points to a dynamic linked list
ptr = head;
while (ptr != NULL)
{
cout << ptr->info;
// Or, do something else with node *ptr
ptr = ptr->link;
}
ptr 3000
3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
head
Traversing a Dynamic Linked List
// Pre: head points to a dynamic linked list
ptr = head;
while (ptr != NULL)
{
cout << ptr->info;
// Or, do something else with node *ptr
ptr = ptr->link;
}
ptr 5000
3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
head
Traversing a Dynamic Linked List
// Pre: head points to a dynamic linked list
ptr = head;
while (ptr != NULL)
{
cout << ptr->info;
// Or, do something else with node *ptr
ptr = ptr->link;
}
ptr 5000
3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
head
Traversing a Dynamic Linked List
// Pre: head points to a dynamic linked list
ptr = head;
while (ptr != NULL)
{
cout << ptr->info;
// Or, do something else with node *ptr
ptr = ptr->link;
}
ptr 5000
3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
head
Traversing a Dynamic Linked List
// Pre: head points to a dynamic linked list
ptr = head;
while (ptr != NULL)
{
cout << ptr->info;
// Or, do something else with node *ptr
ptr = ptr->link;
}
ptr 2000
3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
head
Traversing a Dynamic Linked List
// Pre: head points to a dynamic linked list
ptr = head;
while (ptr != NULL)
{
cout << ptr->info;
// Or, do something else with node *ptr
ptr = ptr->link;
}
ptr 2000
3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
head
Traversing a Dynamic Linked List
// Pre: head points to a dynamic linked list
ptr = head;
while (ptr != NULL)
{
cout << ptr->info;
// Or, do something else with node *ptr
ptr = ptr->link;
}
ptr 2000
3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
head
Traversing a Dynamic Linked List
// Pre: head points to a dynamic linked list
ptr = head;
while (ptr != NULL)
{
cout << ptr->info;
// Or, do something else with node *ptr
ptr = ptr->link;
}
ptr NULL
3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
head
Traversing a Dynamic Linked List
// Pre: head points to a dynamic linked list
ptr = head;
while (ptr != NULL)
{
cout << ptr->info;
// Or, do something else with node *ptr
ptr = ptr->link;
}
ptr NULL
3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
head
Traversing a Dynamic Linked List
Using Operator new
Recall
 If memory is available in the free store (or
heap), operator new allocates the requested
object and returns a pointer to the memory
allocated
 The dynamically allocated object exists until
the delete operator destroys it
31
Inserting a Node at the Front of a List
char item = ‘B’;
NodePtr location;
location = new NodeType;
location->info = item;
location->link = head;
head = location;
head ‘X’ ‘C’ ‘L’
‘B’
item
Inserting a Node at the Front of a List
char item = ‘B’;
NodePtr location;
location = new NodeType;
location->info = item;
location->link = head;
head = location;
head ‘X’ ‘C’ ‘L’
‘B’
item
location
Inserting a Node at the Front of a List
char item = ‘B’;
NodePtr location;
location = new NodeType;
location->info = item;
location->link = head;
head = location;
head ‘X’ ‘C’ ‘L’
‘B’
item
location
Inserting a Node at the Front of a List
char item = ‘B’;
NodePtr location;
location = new NodeType;
location->info = item;
location->link = head;
head = location;
head ‘X’ ‘C’ ‘L’
‘B’
item
location ‘B’
Inserting a Node at the Front of a List
char item = ‘B’;
NodePtr location;
location = new NodeType;
location->info = item;
location->link = head;
head = location;
head ‘X’ ‘C’ ‘L’
‘B’
item
location ‘B’
Inserting a Node at the Front of a List
char item = ‘B’;
NodePtr location;
location = new NodeType;
location->info = item;
location->link = head;
head = location;
head ‘X’ ‘C’ ‘L’
‘B’
item
location ‘B’
When you use the operator delete
 The object currently pointed to by the
pointer is deallocated and the pointer is
considered undefined
 The object’s memory is returned to the free
store
Using Operator delete
38
Deleting the First Node from the List
NodePtr tempPtr;
item = head->info;
tempPtr = head;
head = head->link;
delete tempPtr;
head
item
‘B’ ‘X’ ‘C’ ‘L’
tempPtr
Deleting the First Node from the List
NodeType * tempPtr;
item = head->info;
tempPtr = head;
head = head->link;
delete tempPtr;
head ‘B’ ‘X’ ‘C’ ‘L’
tempPtr
item ‘B’
Deleting the First Node from the List
NodeType * tempPtr;
item = head->info;
tempPtr = head;
head = head->link;
delete tempPtr;
head ‘B’ ‘X’ ‘C’ ‘L’
tempPtr
item ‘B’
Deleting the First Node from the List
NodeType * tempPtr;
item = head->info;
tempPtr = head;
head = head->link;
delete tempPtr;
head ‘B’ ‘X’ ‘C’ ‘L’
tempPtr
item ‘B’
Deleting the First Node from the List
NodeType * tempPtr;
item = head->info;
tempPtr = head;
head = head->link;
delete tempPtr;
head ‘X’ ‘C’ ‘L’
tempPtr
item ‘B’
What is a Sorted List?
A sorted list is a variable-length,
linear collection of homogeneous
elements, ordered according to the
value of one or more data members
The transformer operations must
maintain the ordering
In addition to Insert and Delete, let’s
add two new operations to our list
InsertAsFirst and RemoveFirst
ADT HybridList Operations
Transformers
 InsertAsFirst
 Insert
 RemoveFirst
 Delete
Same observers and iterators as ADT List
Since we have two insertion and two deletion
operations, let’s call this a Hybrid List
change state
45

Lec6 mod linked list

  • 1.
    Topics  Meaning ofa Linked List  Meaning of a Dynamic Linked List  Traversal, Insertion and Deletion of Elements in a Dynamic Linked List  Specification of a Dynamic Linked Sorted List  Insertion and Deletion of Elements in a Dynamic Linked Sorted List
  • 2.
    To implement theList ADT The programmer must 1) choose a concrete data representation for the list, and 2) implement the list operations
  • 3.
    Recall: 4 Basic Kindsof ADT Operations  Constructors -- create a new instance (object) of an ADT  Transformers -- change the state of one or more of the data values of an instance  Observers -- allow client to observe the state of one or more of the data values of an instance without changing them  Iterators -- allow client to access the data values in sequence
  • 4.
    List Operations Transformers  Insert Delete  Sort Observers  IsEmpty  IsFull  Length  IsPresent change state observe state 4
  • 5.
    ADT List Operations Iterator Reset  GetNextItem  Reset prepares for the iteration  GetNextItem returns the next item in sequence  No transformer can be called between calls to GetNextItem (Why?) Iteration Pair
  • 6.
    Array-based class List Reset IsFull Length IsPresent Delete IsEmpty Insert GetNexItem Privatedata: length data [0] [1] [2] [MAX_LENGTH-1] currentPos SelSort
  • 7.
    // Specification filearray-based list (list.h) const int MAX_LENGTH = 50; typedef int ItemType; class List // Declares a class data type { public: // Public member functions List(); // constructor bool IsEmpty () const; bool IsFull () const; int Length () const; // Returns length of list void Insert (ItemType item); void Delete (ItemType item); bool IsPresent(ItemType item) const; void SelSort (); void Reset (); ItemType GetNextItem (); private: // Private data members int length; // Number of values currently stored ItemType data[MAX_LENGTH]; int CurrentPos; // Used in iteration }; 7
  • 8.
    Implementation Structures  Usea built-in array stored in contiguous memory locations, implementing operations Insert and Delete by moving list items around in the array, as needed  Use a linked list in which items are not necessarily stored in contiguous memory locations  A linked list avoids excessive data movement from insertions and deletions
  • 9.
    Implementation Possibilities fora List List Linked list Built-in array Built-in dynamic data and pointers Built-in array of structs
  • 10.
    A Linked List A linked list is a list in which the order of the components is determined by an explicit link member in each node  Each node is a struct containing a data member and a link member that gives the location of the next node in the list head ‘X’ ‘C’ ‘L’
  • 11.
    Dynamic Linked List head“Ted” “Irv” “Lee”  A dynamic linked list is one in which the nodes are linked together by pointers and an external pointer (or head pointer) points to the first node in the list
  • 12.
    Nodes can belocated anywhere in memory  The link member holds the memory address of the next node in the list head 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000
  • 13.
    // Type declarations structNodeType { char info; NodeType* link; } typedef NodeType* NodePtr; // Variable DECLARATIONS NodePtr head; NodePtr ptr; 13 Declarations for a Dynamic Linked List .info .link ‘A’ 6000
  • 14.
    Pointer Dereferencing andMember Selection .info .link ‘A’ 6000 ptr ptr ptr .info .link ‘A’ 6000 *ptr ptr .info .link (*ptr).info ptr->info ‘A’ 6000
  • 15.
    15 ptr is apointer to a node .info .link ‘A’ 6000 ptr ptr
  • 16.
    16 *ptr is theentire node pointed to by ptr ptr .info .link ‘A’ 6000 *ptr
  • 17.
    17 ptr->info is anode member ptr .info .link ptr->info (*ptr).info // Equivalent ‘A’ 6000
  • 18.
    18 ptr->link is anode member ptr .info .link ptr->link (*ptr).link // Equivalent ‘A’ 6000
  • 19.
    Traversing a DynamicLinked List // Pre: head points to a dynamic linked list ptr = head; while (ptr != NULL) { cout << ptr->info; // Or, do something else with node *ptr ptr = ptr->link; } ptr 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000 head
  • 20.
    // Pre: headpoints to a dynamic linked list ptr = head; while (ptr != NULL) { cout << ptr->info; // Or, do something else with node *ptr ptr = ptr->link; } ptr 3000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000 head Traversing a Dynamic Linked List
  • 21.
    // Pre: headpoints to a dynamic linked list ptr = head; while (ptr != NULL) { cout << ptr->info; // Or, do something else with node *ptr ptr = ptr->link; } ptr 3000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000 head Traversing a Dynamic Linked List
  • 22.
    // Pre: headpoints to a dynamic linked list ptr = head; while (ptr != NULL) { cout << ptr->info; // Or, do something else with node *ptr ptr = ptr->link; } ptr 3000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000 head Traversing a Dynamic Linked List
  • 23.
    // Pre: headpoints to a dynamic linked list ptr = head; while (ptr != NULL) { cout << ptr->info; // Or, do something else with node *ptr ptr = ptr->link; } ptr 5000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000 head Traversing a Dynamic Linked List
  • 24.
    // Pre: headpoints to a dynamic linked list ptr = head; while (ptr != NULL) { cout << ptr->info; // Or, do something else with node *ptr ptr = ptr->link; } ptr 5000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000 head Traversing a Dynamic Linked List
  • 25.
    // Pre: headpoints to a dynamic linked list ptr = head; while (ptr != NULL) { cout << ptr->info; // Or, do something else with node *ptr ptr = ptr->link; } ptr 5000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000 head Traversing a Dynamic Linked List
  • 26.
    // Pre: headpoints to a dynamic linked list ptr = head; while (ptr != NULL) { cout << ptr->info; // Or, do something else with node *ptr ptr = ptr->link; } ptr 2000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000 head Traversing a Dynamic Linked List
  • 27.
    // Pre: headpoints to a dynamic linked list ptr = head; while (ptr != NULL) { cout << ptr->info; // Or, do something else with node *ptr ptr = ptr->link; } ptr 2000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000 head Traversing a Dynamic Linked List
  • 28.
    // Pre: headpoints to a dynamic linked list ptr = head; while (ptr != NULL) { cout << ptr->info; // Or, do something else with node *ptr ptr = ptr->link; } ptr 2000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000 head Traversing a Dynamic Linked List
  • 29.
    // Pre: headpoints to a dynamic linked list ptr = head; while (ptr != NULL) { cout << ptr->info; // Or, do something else with node *ptr ptr = ptr->link; } ptr NULL 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000 head Traversing a Dynamic Linked List
  • 30.
    // Pre: headpoints to a dynamic linked list ptr = head; while (ptr != NULL) { cout << ptr->info; // Or, do something else with node *ptr ptr = ptr->link; } ptr NULL 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000 head Traversing a Dynamic Linked List
  • 31.
    Using Operator new Recall If memory is available in the free store (or heap), operator new allocates the requested object and returns a pointer to the memory allocated  The dynamically allocated object exists until the delete operator destroys it 31
  • 32.
    Inserting a Nodeat the Front of a List char item = ‘B’; NodePtr location; location = new NodeType; location->info = item; location->link = head; head = location; head ‘X’ ‘C’ ‘L’ ‘B’ item
  • 33.
    Inserting a Nodeat the Front of a List char item = ‘B’; NodePtr location; location = new NodeType; location->info = item; location->link = head; head = location; head ‘X’ ‘C’ ‘L’ ‘B’ item location
  • 34.
    Inserting a Nodeat the Front of a List char item = ‘B’; NodePtr location; location = new NodeType; location->info = item; location->link = head; head = location; head ‘X’ ‘C’ ‘L’ ‘B’ item location
  • 35.
    Inserting a Nodeat the Front of a List char item = ‘B’; NodePtr location; location = new NodeType; location->info = item; location->link = head; head = location; head ‘X’ ‘C’ ‘L’ ‘B’ item location ‘B’
  • 36.
    Inserting a Nodeat the Front of a List char item = ‘B’; NodePtr location; location = new NodeType; location->info = item; location->link = head; head = location; head ‘X’ ‘C’ ‘L’ ‘B’ item location ‘B’
  • 37.
    Inserting a Nodeat the Front of a List char item = ‘B’; NodePtr location; location = new NodeType; location->info = item; location->link = head; head = location; head ‘X’ ‘C’ ‘L’ ‘B’ item location ‘B’
  • 38.
    When you usethe operator delete  The object currently pointed to by the pointer is deallocated and the pointer is considered undefined  The object’s memory is returned to the free store Using Operator delete 38
  • 39.
    Deleting the FirstNode from the List NodePtr tempPtr; item = head->info; tempPtr = head; head = head->link; delete tempPtr; head item ‘B’ ‘X’ ‘C’ ‘L’ tempPtr
  • 40.
    Deleting the FirstNode from the List NodeType * tempPtr; item = head->info; tempPtr = head; head = head->link; delete tempPtr; head ‘B’ ‘X’ ‘C’ ‘L’ tempPtr item ‘B’
  • 41.
    Deleting the FirstNode from the List NodeType * tempPtr; item = head->info; tempPtr = head; head = head->link; delete tempPtr; head ‘B’ ‘X’ ‘C’ ‘L’ tempPtr item ‘B’
  • 42.
    Deleting the FirstNode from the List NodeType * tempPtr; item = head->info; tempPtr = head; head = head->link; delete tempPtr; head ‘B’ ‘X’ ‘C’ ‘L’ tempPtr item ‘B’
  • 43.
    Deleting the FirstNode from the List NodeType * tempPtr; item = head->info; tempPtr = head; head = head->link; delete tempPtr; head ‘X’ ‘C’ ‘L’ tempPtr item ‘B’
  • 44.
    What is aSorted List? A sorted list is a variable-length, linear collection of homogeneous elements, ordered according to the value of one or more data members The transformer operations must maintain the ordering In addition to Insert and Delete, let’s add two new operations to our list InsertAsFirst and RemoveFirst
  • 45.
    ADT HybridList Operations Transformers InsertAsFirst  Insert  RemoveFirst  Delete Same observers and iterators as ADT List Since we have two insertion and two deletion operations, let’s call this a Hybrid List change state 45