Linked List
CSIT203 : Data Structures And
Algorithms
What is a linked list?
- is a collection of nodes that
together form a linear ordering
- each node is a compound object
that stores a reference to an
element and a reference, called
next, to another
College of Computer Studies
Diagram
Head Tail
next
Rose Ann Ø
College of Computer Studies
Inserting a Node
Head Tail
next
Rose Ann Marie Ø
College of Computer Studies
After Inserting a Node
Head Tail
next next
Rose Ann Marie Ø
College of Computer Studies
Example:
Do the following:
• insert feet (head), remove(feet),
insert (stomach), insert (arms),
insert legs (head), remove (arms),
insert fingers (tail)
College of Computer Studies
Do the following:
1. insert (keyboard), insert( mouse),
remove(mouse), insert (mousepad),
insert (monitor), remove(mousepad),
insert (unit)
2. insert (ab), insert( cd), insert(ef),
remove(ef), insert(xy), insert(op),
insert(rs), remove (ab), insert(tu)
College of Computer Studies
Linked List Methods
Methods Description
AddFirst() Adds the specified new node at
the start
AddLast() Adds the specified new node at
the end
AddBefore() Adds the specified new node
before the existing node
AddAfter() Adds the specified new node
after the existing node
College of Computer Studies
Linked List Methods
Methods Description
Clear() Removes all nodes
Contains() Determines whether the value is
in the linklist
Count Get the number of nodes actually
in the linklist
Equals() Determines whether the
specified object is equal to the
current object
College of Computer Studies
Linked List Methods
Methods Description
Find() Find the first node that contains
the specified value
FindLast() Find the last node that contains
the specified value
First Get the first node of the linklist
Last Get the last node of the linklist
College of Computer Studies
Linked List Methods
Methods Description
Remove() Removes the specified node
from the linklist
RemoveFirst() Removes the node at the start
of the linklist
RemoveLast() Removes the node at the end of
the linklist
ToString() Returns a string that represents
the current object
College of Computer Studies
Example # 1
LinkedList<string> st = new LinkedList<string>();
st.AddFirst("Rose");
st.AddBefore(st.First, "Gumamela");
st.AddLast("Adelfa");
st.AddAfter(st.First, "Sunflower");
st.Contains("Gumamela");
College of Computer Studies
Example # 1 (cont..)
Console.WriteLine("No. of elements inside the
linklist: " + st.Count);
Console.WriteLine("\n<<Elements inside the linklist>>");
foreach(string value in st)
Console.WriteLine(value);
College of Computer Studies
Example # 2 (cont..)
st.Remove("Gumamela");
st.RemoveLast();
Console.WriteLine("\n<<NEW Elements inside
the linklist>>");
foreach (string value in st)
Console.WriteLine(value);
College of Computer Studies