|
|
|
Operations for Lists. |
|
Implementation of Linked Lists. |
|
Double Linked Lists. |
|
|
|
|
|
(informally:) |
|
By size: |
|
Static (e.g. arrays) |
|
Dynamic (e.g. vectors) |
|
|
|
By ordering: |
|
Last In First Out (LIFO) |
|
First In First Out (FIFO) |
|
Priority |
|
|
|
|
|
|
|
|
|
Types of dynamic generic data structures are: |
|
|
|
Stacks |
|
Queues |
|
Lists |
|
Trees |
|
|
|
|
|
|
|
Memory which is allocated and de-allocated
during the execution of the program. |
|
Types: |
|
data in run-time stack |
|
dynamic data in the heap |
|
|
|
|
|
|
Go to a position in the list. |
|
Insert an item at a position in the list. |
|
Delete an item from a position in the list. |
|
Retrieve an item from a position. |
|
Replace an item at a position. |
|
Traverse a list. |
|
|
|
|
|
Linked Storage |
|
Unknown list size. |
|
Flexibility is needed. |
|
|
|
Contiguous Storage |
|
Known list size. |
|
Few insertions and deletions are made within the
list. |
|
Random access |
|
|
|
|
|
Public IntNode (int initial Data, IntNode
initial Link) |
|
{ |
|
data = initialData; |
|
link = initialLink: |
|
} |
|
|
|
|
|
check if position is within range |
|
start with address of head node |
|
set count to 0 |
|
while count is less than position |
|
follow link to next node |
|
increment count |
|
return address of current node |
|
|
|
|
|
|
|
|
public static IntNode listPosition (IntNode
head, int position) |
|
{ |
|
IntNode cursor; |
|
int i; |
|
if (position <=0) |
|
throw new IllegalArgumentException (position is
invalid); |
|
cursor = head; |
|
for (i=1; (i < position) &&
(cursor != null); i++) |
|
cursor = cursor.link; |
|
return cursor; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Activate the following method: |
|
selection.removeNodeAfter( ); |
|
where selection is a reference to a node of a
linked list. |
|
|
|
Implementation needs only one statement: |
|
link = link.link |
|
|
|
|
Go to a position in the list. |
|
Insert an item in a position in the list. |
|
Delete an item from a position in the list. |
|
Retrieve an item from a position. |
|
Replace an item at a position. |
|
Traverse a list, in both directions. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|