Created
July 16, 2018 04:18
-
-
Save shailendra9292/d35cec47a9e2413bf4e054a15d9124b3 to your computer and use it in GitHub Desktop.
Link list insertion and deletion implementation in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class node: | |
| def __init__(self, data=None): | |
| self.data = data | |
| self.next = None | |
| class linked_list: | |
| def __init__(self): | |
| self.head = None | |
| def insertBeg(self, data): | |
| temp = node(data) | |
| temp.next = self.head | |
| self.head = temp | |
| def insertEnd(self, data): | |
| temp = node(data) | |
| p = self.head | |
| while p.next is not None: | |
| p = p.next | |
| p.next = temp | |
| temp.next = None | |
| def insertAtPos(self, value, pos): | |
| c = 1 | |
| p = self.head | |
| while p.next is not None: | |
| if pos - 1 == c: | |
| temp = node(value) | |
| temp.next = p.next | |
| p.next = temp | |
| return | |
| c += 1 | |
| p = p.next | |
| def delBeg(self): | |
| if self.head is None: | |
| print("list is already empty") | |
| return | |
| print("\nDeleted element is {} and new list becomes...".format(self.head.data)) | |
| self.head = self.head.next | |
| def delLast(self): | |
| if self.head is None: | |
| print("list is already empty") | |
| return | |
| p = self.head | |
| while p.next.next: | |
| p = p.next | |
| print("\nDeleted element is {} and new list becomes...".format(p.next.data)) | |
| p.next = None | |
| def delPos(self, pos): | |
| if self.head is None: | |
| print("list is already empty") | |
| return | |
| elif pos > self.length(): | |
| print("range outside of node length") | |
| return | |
| c = 1 | |
| p = self.head | |
| while p.next is not None: | |
| if pos - 1 == c: | |
| temp = node(None) | |
| temp.next = p.next.next | |
| print("\nDeleted element is {} at position {} and new list becomes...".format( | |
| p.next.data, c + 1)) | |
| p.next = temp | |
| p.next = temp.next | |
| return | |
| c += 1 | |
| p = p.next | |
| def printList(self, head): | |
| p = head | |
| while p: | |
| print(p.data, end=' ') | |
| if p.next is not None: | |
| print('->',end=' ') | |
| p = p.next | |
| print() | |
| def length(self): | |
| p = self.head | |
| c = 0 | |
| while p is not None: | |
| c += 1 | |
| p = p.next | |
| return c | |
| l = linked_list() | |
| l.insertBeg(9) | |
| l.insertBeg(4) | |
| l.insertBeg(5) | |
| l.insertBeg(3) | |
| l.insertBeg(2) | |
| l.insertBeg(7) | |
| l.insertBeg(1) | |
| l.insertEnd(2) | |
| l.insertEnd(99) | |
| l.insertAtPos(13,4) | |
| print("\nnode length is {} ".format(l.length())) | |
| l.printList(l.head) | |
| l.delBeg() | |
| l.delLast() | |
| l.delPos(7) | |
| print('list after deletion operation...') | |
| l.printList(l.head) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment