• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 2 out of 7 pages
Other

Operations on Linked Lists: Insertion, Deletion, and Traversal

Document preview thumbnail
Preview 2 out of 7 pages

This document covers the essential operations on linked lists, including insertion, deletion, and traversal techniques for singly and doubly linked lists, explained with practical examples and code snippets.

Content preview

Operations on Linked Lists
Linked lists support several essential operations that allow you to manipulate the
data in the list. These operations vary depending on the type of linked list (singly,
doubly, or circular). Below are some of the most common operations on linked
lists.



1. Basic Operations
1.1 Traversal

Traversal refers to the process of visiting each node in the linked list. This
operation is typically used to display all elements of the list or search for a
particular element.

 Singly Linked List: Start from the head and follow the next pointers until
reaching null.
 Doubly Linked List: Traverse from the head to the last node and back using
both next and prev pointers.
 Circular Linked List: Traverse starting from the head and continue until
reaching the head again.

Example (Python - Singly Linked List):

def traverse(self):
current = self.head
while current:
print(current.data, end=" -> ")
current = current.next
print("None")


1.2 Insertion

Insertion involves adding a new node at a specific position in the linked list.
Common insertion operations include:

,  At the beginning: Insert the node at the head of the list.
 At the end: Insert the node at the last position.
 At a specific position: Insert the node at a given index.

Example (Python - Insert at the Beginning for Singly Linked List):

def insert_at_beginning(self, data):
new_node = Node(data)
new_node.next = self.head
self.head = new_node

Insert at the End for Singly Linked List:

def insert_at_end(self, data):
new_node = Node(data)
if not self.head:
self.head = new_node
return
current = self.head
while current.next:
current = current.next
current.next = new_node


1.3 Deletion

Deletion involves removing a node from the linked list. The most common
deletions are:

 Delete from the beginning: Remove the head node.
 Delete from the end: Remove the last node.
 Delete by value: Remove a specific node containing a given value.
 Delete by position: Remove the node at a specific index.

Example (Python - Delete from the Beginning for Singly Linked List):

def delete_from_beginning(self):
if self.head:
self.head = self.head.next

Document information

Uploaded on
January 28, 2025
Number of pages
7
Written in
2024/2025
Type
Other
Person
Unknown
$6.59

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Sold
0
Followers
0
Items
252
Last sold
-




Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions