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;
};
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;
}
}
}
Inserting at the Head
Update the next link of a new node, to
point to the current head node.
Update head link to point to the new
node.
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
Update head link to point to the node,
next to the head.
Dispose removed node.
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
Update the next link of the current tail
node, to point to the new node.
Update tail link to point to the new
node
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.
Update link of the "previous" node,
to point to the new node.
Update link of the new node, to
point to the "next" node.
 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
Algorithm addFirst(v):
w = header.getNext() // the
current first node
v.setNext(w)
w.setPrev(v)
header.setNext(v)
v.setPrev(header)
size++
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--
•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
•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”

LINKED LISTS

  • 3.
    data next A singlylinked 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 theHead 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; } } }
  • 5.
  • 6.
    Update the nextlink of a new node, to point to the current head node.
  • 7.
    Update head linkto point to the new node.
  • 8.
    Removing at theHead 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
  • 9.
    Update head linkto point to the node, next to the head.
  • 10.
  • 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 nextlink of the current tail node, to point to the new node.
  • 14.
    Update tail linkto point to the new node
  • 15.
    Insertion between twonodes 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 ofthe "previous" node, to point to the new node.
  • 17.
    Update link ofthe new node, to point to the "next" node.
  • 18.
     It isa 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 inC, 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 frommiddle 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”