Lec 6
Lec 6
AND ALGORITHMS
• A LinkedList is a linear data structure used for storing the elements in a non-contiguous
manner.
• The elements in a linked list are linked with each other using pointers.
• LinkedList consists of nodes where each node contains a data field and a reference(link) to
the next node in the list.
2. Address: Each node of a linked list contains an address to the next node, called “Next”.
• They are dynamic in nature and allocate memory as and when required.
• Other data structures such as Stack and Queue can also be implemented easily using Linked
List.
• It has faster access time and can be expanded in constant time without memory overhead.
• Since there is no need to define an initial size for a linked list, hence memory utilization is
effective.
• Doubly Circular Linked List: In this type of linked list, the next of the last node will
point to the first node and the previous pointer of the first node will point to the last
node.
LINKEDLIST IMPLEMENTATION IN .NET FRAMEWORK
5. AddFirst(LinkedListNode<T> node)
6. AddFirst(T value)
7. AddLast(LinkedListNode<T> node)
8. AddLast(T value)
• Example:
• You can access the elements of a Generic LinkedList<T> Collection in C# using for each loop
as follows:
• To Remove Elements from the LinkedList<T> collection in C#
1. Remove(LinkedListNode<T> node):
2. Remove(T value)
3. RemoveFirst()
4. RemoveLast()
5. Clear()
LINKEDLIST IMPLEMENTATION
• The node of a singly linked list contains a data part and an address part.
• The address will contain the address of next node and is initialized to null.
• we will create class definition of node for singly linked list as follows:
• The node for a doubly Linked list will contain one data part and two address parts: previous address and
next address.
• we create a class definition of a node for the doubly linked list as:
• Singly linked list doubly linked list
INSERT DATA AT FRONT OF THE LINKED LIST
1. If the Linked List is empty, then we add the new node as the First of the Linked List.
2. If the Linked List is not empty, then we find the last node and make next of the last
node to the new node, hence the new node is the last node now.
INSERT DATA AFTER A SPECIFIC NODE IN THE LINKED
LIST
• We will set the next of new node to the next of given node.
• Then we will set the next of given node to new node
• To perform this operation on doubly linked list we need to follow two extra steps
1.Set the previous of new node to given node.
2.Set the previous of the next node of given node to the new node.
DELETE A NODE FROM LINKED LIST USING A GIVEN
KEY VALUE
1. First step is to find the node having the key value.
2. We will traverse through the Linked list, and use one extra pointer to keep track of the previous
node while traversing the linked list.
3. If the node to be deleted is the first node, then simply set the Next pointer of the Head to point to
the next element from the Node to be deleted.
4. If the node is in the middle somewhere, then find the node before it, and make the Node before
it point to the Node next to it.
5. If the node to be deleted is last node, then find the node before it, and set it to point to null.
• Delete from singly linked list
• Delete from doubly linked list