Software and Systems Engineering Standards: Verification and Validation of Sy...
Â
LINKED LISTS
1.
2.
3. data next
A singly linked list is a concrete data
structure consisting of a sequence of
nodes.
Each node stores:-
ï±element
ï±link to the next node
struct Node
{ int item
Node *next;
};
4. Inserting at the Head
1. Allocate a new
node
2. Insert new
element
3. Make new node
point to old
head
4. Update head to
point to new
node
void insertLL(Struct Node
*newNode)
{
if (newNode == NULL)
return;
else {
if (head == NULL) {
newNode->next = NULL;
head = newNode;
tail = newNode;
}
else {
newNode->next = head;
head = newNode;
}
}
}
8. Removing at the Head
void removeFirst()
{
if (head == NULL)
return;
else {
Struct Node *removedNode;
removedNode = head;
if (head == tail) {
head = NULL;
tail = NULL;
} else {
head = head->next;
}
delete removedNode;
}
}
1. Update head to
point to next node
in the list
2. Allow garbage
collector to reclaim
the former first
node
12. void insert(Struct Node*newNode)
{
if (newNode == NULL)
return;
else {
newNode->next = NULL;
if (head == NULL) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
}
}
1. Allocate a new
node
2. Insert new element
3. Have new node
point to null
4. Have old last node
point to new node
5. Update tail to point
to new node
Inserting at the Tail
13. Update the next link of the current tail
node, to point to the new node.
15. Insertion between two nodes
New node is always inserted between two nodes, which
are already in the list. Head and tail links are not updated
in this case.
16. Update link of the "previous" node,
to point to the new node.
17. Update link of the new node, to
point to the "next" node.
18. ï§ It is a way of going both directions in a
linked list, forward and reverse.
ï§ Many applications require a quick access
to the predecessor node of some node in
list.
Doubly-Linked Lists
prevnext
19. Algorithm addFirst(v):
w = header.getNext() // the
current first node
v.setNext(w)
w.setPrev(v)
header.setNext(v)
v.setPrev(header)
size++
20. Algorithm removeLast():
v = trailer.getPrev() // the
current last node
if (v = = header) then
Indicate an error: the list
is empty
prev = v.getPrev()
prev.setNext(trailer)
trailer.setPrev(prev)
v.setPrev(null)
v.setNext(null)
size--
21. âąIf programming in C, there are no âgrow able-arraysâ, so
typically linked lists used when# elements in a collection
varies, isnât known, canât be fixed at compile time
ïCould grow array, potentially expensive/wasteful especially if #
elements is small.
ïAlso need# elements in array, requires extra parameter
ïWith linked list, one pointer used to access all the elements in a
collection
âąSimulation/modelling of DNA gene-splicing
ïGiven list of millions of CGTA... for DNA strand, find locations where
new DNA/gene can be spliced
22. âąRemove element from middle of a collection,
maintain order, no shifting. Add an element in the
middle, no shifting
ïWhatâs the problem with a vector (array)?
ïEmacs visits several files, internally keeps a linked-list of
ïbuffers
ïNaively keep characters in a linked list, but in practice too much storage,
ï need more esoteric data structures
âąWhatâs (3x5+ 2x3+ x + 5) + (2x4+ 5x3+ x2+4x) ?
ïAs a vector (3, 0, 2, 0, 1, 5)and (0, 2, 5, 1, 4, 0)
ïAs a list ((3,5), (2,3), (1,1), (5,0))and ________?
ïMost polynomial operations sequentially visit terms, donât need random
access, do need âsplicingâ