June 16, 2018
1 likes
606 times viewed

#### Content

A linked list is a linear data structure where each element is a separate object. Each element (we will call it a node) of a list is comprising of two items - the data and a reference to the next node.

Creating a node

A node in linked list contains the data field and a pointer to the next node.

``````class Node(object):
"""docstring for Node."""
def __init__(self, data):
super(Node, self).__init__()
self.data = data
self.next = None
``````

``````class LinkedList(object):
def __init__(self):
``````

``````def print_list(self):
while(temp):
print(temp.data)
temp = temp.next
``````

Pushing a new node at front

``````def push(self, new_data):
new_node = Node(new_data)
``````

Driver Program

``````sll = LinkedList()
sll.push(15); sll.push(2); sll.push(7); sll.push(3)

sll.print_list()

sll.push(5)
sll.print_list()
``````

Output

Appending a new node at back

``````def append(self, new_data):
new_node = Node(new_data)

return

while(last.next):
last = last.next

last.next = new_node
``````

Driver Program

``````sll = LinkedList()
sll.push(15); sll.push(2); sll.push(7); sll.push(3)
sll.print_list()

sll.append(5)
sll.print_list()
``````

Pushing a new node at a given position

``````def insertAt(self, position, new_data):
if(position==0):
self.push(new_data)
return

while(prev_node.next and position-1!=0):
prev_node = prev_node.next
position -= 1

if(position-1!=0):
print("No suffiecient Element")
return

new_node = Node(new_data)
new_node.next = prev_node.next
prev_node.next = new_node
``````

Driver Program

``````sll = LinkedList()
sll.push(15); sll.push(2); sll.push(7); sll.push(3)

sll.print_list()

sll.insertAt(2,5)
sll.print_list()
``````

Output

Deleting a node with given value

``````def delete(self, data):
if(temp):
if(temp.data==data):
temp = None
return

while(temp):
if(temp.data==data):
break
prev = temp
temp = temp.next

if(not temp):
print("Data to be deleted not present")
return

prev.next = temp.next
temp = None
``````

Driver Program

``````sll = LinkedList()
sll.push(15); sll.push(2); sll.push(7); sll.push(3)

sll.print_list()

sll.delete(2)
sll.print_list()
``````

Output

Complete Driver Program

``````class Node(object):
"""docstring for Node."""
def __init__(self, data):
super(Node, self).__init__()
self.data = data
self.next = None

def __init__(self):

def push(self, new_data):
new_node = Node(new_data)

def insertAt(self, position, new_data):
if(position==0):
self.push(new_data)
return

while(prev_node.next and position-1!=0):
prev_node = prev_node.next
position -= 1

if(position-1!=0):
print("No suffiecient Element")
return

new_node = Node(new_data)
new_node.next = prev_node.next
prev_node.next = new_node

def append(self, new_data):
new_node = Node(new_data)

return

while(last.next):
last = last.next

last.next = new_node

def delete(self, data):
if(temp):
if(temp.data==data):
temp = None
return

while(temp):
if(temp.data==data):
break
prev = temp
temp = temp.next

if(not temp):
print("Data to be deleted not present")
return

prev.next = temp.next
temp = None

def print_list(self):
while(temp):
print(temp.data, end="")
print("---->", end="")
temp = temp.next
print("NULL")

sll.append(6)
sll.push(7)
sll.push(1)
sll.push(9)
sll.append(4)
sll.insertAt(2,8)
sll.delete(9)

sll.print_list()

``````

Final Output

## Join the discussion

Nothing to preview