Notes
Outline
Linked Lists Implementation
Operations for Lists.
Implementation of Linked Lists.
Double Linked Lists.
Data structures
(informally:)
By size:
Static (e.g. arrays)
Dynamic (e.g. vectors)
By ordering:
Last In First Out (LIFO)
First In First Out (FIFO)
Priority
Data Structures
Types of dynamic generic data structures are:
Stacks
Queues
Lists
Trees
What is Dynamic Memory?
Memory which is allocated and de-allocated during the execution of the program.
Types:
data in run-time stack
dynamic data in the heap
List Operations
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.
Comparison
Linked Storage
Unknown list size.
Flexibility is needed.
Contiguous Storage
Known list size.
Few insertions and deletions are made within the list.
Random access
Linked List
Constructor for the NodeClass
Public IntNode (int initial Data, IntNode initial Link)
{
data = initialData;
link = initialLink:
}
Set Position
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
Set Position
Set Position
Set Position
Set Position
Find a Position in a Linked List
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;
}
Insert
Insert
Slide 17
Slide 18
Inserting – New List
Inserting – New List
Inserting – Start of List
Inserting – Start of List
Inserting – Start of List
Inserting – Inside the List
Inserting – Inside the List
Inserting – Inside the List
Delete
Slide 28
Slide 29
Slide 30
Deleting – 1st Node
Deleting – 1st Node
Deleting – 1st Node
Deleting – Middle Node
Deleting – Middle Node
Deleting – Middle Node
Deleting a Node that is not at the Head
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
Double Linked List Operations
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.
Double Linked List
Insert at end
Insert at end
Insert at end
Insert inside the list
Insert inside the list
Insert inside the list
Insert inside the list
Insert inside the list
Delete from end
Delete from end
Delete from end
Delete from inside list
Delete from inside list
Delete from inside list
Delete from inside list