0% found this document useful (0 votes)
5 views13 pages

Linked Lists

The document provides detailed instructions for inserting and deleting nodes in Singly Linked Lists (SLL), Doubly Linked Lists (DLL), and Circular Linked Lists (CLL). It includes specific methods for adding nodes at the beginning, at a specified position, and at the end, as well as corresponding deletion methods. Additionally, it outlines the structure for node creation in each type of linked list.

Uploaded by

uit5832
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)
5 views13 pages

Linked Lists

The document provides detailed instructions for inserting and deleting nodes in Singly Linked Lists (SLL), Doubly Linked Lists (DLL), and Circular Linked Lists (CLL). It includes specific methods for adding nodes at the beginning, at a specified position, and at the end, as well as corresponding deletion methods. Additionally, it outlines the structure for node creation in each type of linked list.

Uploaded by

uit5832
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/ 13

Singly Linked List (SLL)

Insertion

At Beginning:

New_node = Node(data)

New_node.next = head

Head = new_node

At Position (n):

Temp = head

For _ in range(n-1):

Temp = temp.next

New_node = Node(data)

New_node.next = temp.next

Temp.next = new_node

At End:

Temp = head

While temp.next:

Temp = temp.next

New_node = Node(data)

Temp.next = new_node
Deletion

At Beginning:

Head = head.next

At Position (n):

Temp = head

For _ in range(n-1):

Temp = temp.next

Temp.next = temp.next.next

At End:

Temp = head

While temp.next.next:

Temp = temp.next

Temp.next = None

Doubly Linked List (DLL)

Insertion
At Beginning:

New_node = Node(data)

New_node.next = head

Head.prev = new_node

Head = new_node

At Position (n):

Temp = head

For _ in range(n-1):

Temp = temp.next

New_node = Node(data)

New_node.next = temp.next

New_node.prev = temp

Temp.next.prev = new_node

Temp.next = new_node

At End:

Temp = head

While temp.next:

Temp = temp.next

New_node = Node(data)

Temp.next = new_node
New_node.prev = temp

Deletion

At Beginning:

Head = head.next

Head.prev = None

At Position (n):

Temp = head

For _ in range(n):

Temp = temp.next

Temp.prev.next = temp.next

Temp.next.prev = temp.prev

At End:

Temp = head

While temp.next:

Temp = temp.next

Temp.prev.next = None
Circular Linked List (CLL)

Insertion

At Beginning:

New_node = Node(data)

New_node.next = head

Temp = head

While temp.next != head:

Temp = temp.next

Temp.next = new_node

Head = new_node

At Position (n):

Temp = head

For _ in range(n-1):

Temp = temp.next

New_node = Node(data)

New_node.next = temp.next

Temp.next = new_node

At End:

Temp = head
While temp.next != head:

Temp = temp.next

New_node = Node(data)

Temp.next = new_node

New_node.next = head

Deletion

At Beginning:

Temp = head

While temp.next != head:

Temp = temp.next

Temp.next = head.next

Head = head.next

At Position (n):

Temp = head

For _ in range(n-1):

Temp = temp.next

Temp.next = temp.next.next

At End:

Temp = head
While temp.next.next != head:

Temp = temp.next

Temp.next = head

Singly Linked List – Node Creation

Class Node:

Def __init__(self, data):

Self.data = data

Self.next = None

Doubly Linked List – Node Creation

Class Node:

Def __init__(self, data):

Self.data = data

Self.next = None

Self.prev = None

Circular Linked List – Node Creation

Class Node:

Def __init__(self, data):

Self.data = data

Self.next = self # Points to itself in a circular list


Adding Objects (Nodes) at the End

Singly Linked List (SLL):

If head is None:

Head = Node(data)

Else:

Temp = head

While temp.next:

Temp = temp.next

Temp.next = Node(data)

Doubly Linked List (DLL):

If head is None:

Head = Node(data)

Else:

Temp = head

While temp.next:

Temp = temp.next

New_node = Node(data)

Temp.next = new_node

New_node.prev = temp
Circular Linked List (CLL):

If head is None:

Head = Node(data)

Else:

Temp = head

While temp.next != head:

Temp = temp.next

New_node = Node(data)

Temp.next = new_node

New_node.next = head

TEXT FORMAT LOGIC

1. Singly Linked List (SLL)


Insertion
At Beginning.
1. Create new node.
2. Point new node’s next to head.
3. Update head to new node.
At Position (n):
1. Traverse to (n-1)th node.
2. Create new node.
3. Point new node’s next to (n)th node.
4. Update (n-1)th node’s next to new node.
At End:
1. Traverse to last node.
2. Create new node.
3. Update last node’s next to new node.

Deletion
At Beginning:
1. Update head to head.next
At Position (n):
1. Traverse to (n-1)th node.
2. Update (n-1)th node’s next to (n+1)th node.
At End:
1. Traverse to second-last node.
2. Set second-last node’s next = None.

Doubly Linked List (DLL)


Insertion
At Beginning:
1. Create new node.
2. Point new node’s next to head.
3. Update head.prev to new node.
4. Update head to new node.
At Position (n):
1. Traverse to (n-1)th node.
2. Create new node.
3. Update new node’s next to (n)th node.
4. Update new node’s prev to (n-1)th node.
5. Adjust (n-1)th.next and (n)th.prev.
At End:
1. Traverse to last node.
2. Create new node.
3. Update last node’s next to new node.
4. Update new node’s prev to last node.

Deletion
At Beginning:
1. Update head to head.next.
2. Set head.prev = None.
At Position (n):
1. Traverse to (n)th node.
2. Update (n-1)th node’s next to (n+1)th node.
3. Update (n+1)th node’s prev to (n-1)th node.
At End:
1. Traverse to last node.
2. Update second-last node’s next = None.

3. Circular Linked List (CLL)


Insertion
At Beginning:
1. Create new node.
2. Point new node’s next to head.
3. Traverse to last node and update last.next to new node.
4. Update head to new node.
At Position (n):
1. Traverse to (n-1)th node.
2. Create new node.
3. Update new node’s next to (n)th node.
4. Update (n-1)th node’s next to new node.
At End:
1. Traverse to last node.
2. Create new node.
3. Update last node’s next to new node.
4. Point new node’s next to head.

Deletion
At Beginning:
1. Traverse to last node.
2. Update last.next to head.next.
3. Update head to head.next.
At Position (n):
1. Traverse to (n-1)th node.
2. Update (n-1)th node’s next to (n+1)th node.
At End:
1. Traverse to second-last node.
2. Update second-last node’s next to head.

You might also like