0% found this document useful (0 votes)
4 views3 pages

Wa0001

The document outlines a Python program for implementing basic operations on a singly linked list, including inserting nodes at the end or beginning, deleting a node, and displaying the list. It defines two classes: Node for individual elements and LinkedList for managing the list operations. Sample output demonstrates the functionality of inserting and deleting nodes in the linked list.

Uploaded by

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

Wa0001

The document outlines a Python program for implementing basic operations on a singly linked list, including inserting nodes at the end or beginning, deleting a node, and displaying the list. It defines two classes: Node for individual elements and LinkedList for managing the list operations. Sample output demonstrates the functionality of inserting and deleting nodes in the linked list.

Uploaded by

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

6.

PROGRAM TO IMPLEMENT THE OPERATIONS IN A SINGLY


LINKED LIST

AIM:
To develop a program in python that implements basic operations on a
singly linked list.
ALGORITHM:
1. Define a class Node to represent a node in the linked list. Each node has a
data element and a reference to the next node in the list.
2. Define a class LinkedList that contains methods to perform operations on the
linked list:
o __init__: Initialize an empty linked list.
o insert_at_end: Insert a new node at the end of the linked list.
o insert_at_beginning: Insert a new node at the beginning of the linked
list.
o delete_node: Delete a node with a given data value from the linked
list.
o display: Display the elements of the linked list.

PROGRAM:
class Node:
def __init__(self, data=None):
self.data = data
self.next = None

class LinkedList:
def __init__(self):
self.head = None

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

def insert_at_beginning(self, data):


new_node = Node(data)
new_node.next = self.head
self.head = new_node

def delete_node(self, data):


current = self.head
if current and current.data == data:
self.head = current.next
return
while current.next:
if current.next.data == data:
current.next = current.next.next
return
current = current.next

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

linked_list = LinkedList()

linked_list.insert_at_end(1)
linked_list.insert_at_end(2)
linked_list.insert_at_end(3)
linked_list.display()

linked_list.insert_at_beginning(0)
linked_list.display()

linked_list.delete_node(2)
linked_list.display()

SAMPLE OUTPUT:

1 -> 2 -> 3 -> None


0 -> 1 -> 2 -> 3 -> None
0 -> 1 -> 3 -> None

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy