0% found this document useful (0 votes)
11 views22 pages

Lec 6

Uploaded by

ba.at.al.2924
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views22 pages

Lec 6

Uploaded by

ba.at.al.2924
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

DATA STRUCTURES

AND ALGORITHMS

DR. FATMA ELGENDY


LINKED LIST

• 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.

• Each node contains two parts.

1. Data: Each node of a linked list can store data.

2. Address: Each node of a linked list contains an address to the next node, called “Next”.

The LinkedList<T> belongs to the System.Collections.Generic namespace.


ADVANTAGES OF LINKED LIST

• They are dynamic in nature and allocate memory as and when required.

• Insertion and deletion is easy to implement, O(1) operations.

• 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.

• Backtracking is possible in doubly linked lists.


TYPES OF LINKED LIST

1. Singly Linked List


2. Doubly Linked List
3. Circular Linked List
4. Doubly Circular Linked List
SINGLY LINKED LIST

• It contains nodes which have a data part and an address part(Next)


• Next: points to the next node in the sequence of nodes.
• The next pointer of the last node will point to null.
DOUBLY LINKED LIST

• In a doubly linked list, each node contains two links:


• Prev: points to the previous node
• Next: points to the next node in the sequence.
• The prev pointer of the first node and next pointer of the last node will point to null.
• Circular Linked List: In the circular linked list, the next of the last node will point
to the first node, thus forming a circular chain.

• 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

• The LinkedList<T> belongs to the System.Collections.Generic namespace.


• The LinkedList<T> is a Generic Collection Class in C# which implements a
double-ended linked list
• To create an instance of the LinkedList<T>
• To Add Elements into a Generic LinkedList<T> Collection in C#
1. AddAfter(LinkedListNode<T> node, LinkedListNode<T> newNode)

2. AddAfter(LinkedListNode<T> node, T value)


3. AddBefore(LinkedListNode<T> node, LinkedListNode<T> newNode)
4. AddBefore(LinkedListNode<T> node, T value)

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. create a new node.


2. The next of the new node will point to the head of the Linked list.
3. First is assigned to the new node.
INSERT DATA AT END 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

You might also like