Merge branch 'imple-linked' of https://github.com/vinay-sagar123/learn-python into imple-linked

pull/615/head
Vinay 2024-05-26 14:35:21 +05:30
commit 48380b7363
1 zmienionych plików z 9 dodań i 6 usunięć

Wyświetl plik

@ -25,12 +25,12 @@ Let's code something
The smallest Unit: Node The smallest Unit: Node
```python ```python
class Node: class Node:
def __init__(self, data): def __init__(self, data):
self.data = data # Assigns the given data to the node self.data = data # Assigns the given data to the node
self.next = None # Initialize the next attribute to null self.next = None # Initialize the next attribute to null
``` ```
Now, we will see the types of linked list. Now, we will see the types of linked list.
@ -46,20 +46,20 @@ There are mainly four types of linked list,
Simply think it is a chain of nodes in which each node remember(contains) the addresses of it next node. Simply think it is a chain of nodes in which each node remember(contains) the addresses of it next node.
### Creating a linked list class ### Creating a linked list class
```python ```python
class LinkedList: class LinkedList:
def __init__(self): def __init__(self):
self.head = None # Initialize head as None self.head = None # Initialize head as None
``` ```
### Inserting a new node at the beginning of a linked list ### Inserting a new node at the beginning of a linked list
```python ```python
def insertAtBeginning(self, new_data): def insertAtBeginning(self, new_data):
new_node = Node(new_data) # Create a new node new_node = Node(new_data) # Create a new node
new_node.next = self.head # Next for new node becomes the current head new_node.next = self.head # Next for new node becomes the current head
self.head = new_node # Head now points to the new node self.head = new_node # Head now points to the new node
``` ```
### Inserting a new node at the end of a linked list ### Inserting a new node at the end of a linked list
@ -171,6 +171,7 @@ check the list is empty otherwise shift the head to next node.
current = current.next current = current.next
position += 1 position += 1
return f"Value '{value}' not found in the list" return f"Value '{value}' not found in the list"
<<<<<<< HEAD
``` ```
```python ```python
@ -218,3 +219,5 @@ and more....
=======
>>>>>>> beba33d28736209440c9d9cb4784426a9d38ce9b