-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
237 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import array | ||
arr = array.array('i', [1,2,3]) | ||
|
||
print("The new array created is: ") | ||
for i in range(0,3): | ||
print(arr[i]) | ||
|
||
print("\r") | ||
|
||
arr.append(4) | ||
|
||
print("The appended array is: ") | ||
for i in range(0,4): | ||
print(arr[i]) | ||
|
||
|
||
arr.insert(2,5) | ||
|
||
|
||
print("The inserted array is: ") | ||
for i in range(0,5): | ||
print(arr[i]) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
class Node: | ||
def __init__(self, next=None, prev=None, data= None): | ||
self.next = next | ||
self.prev = prev | ||
self.data = data | ||
|
||
class DoublyLinkedList: | ||
def __init__(self): | ||
self.head = None | ||
|
||
def insert_at_begin(self, data): | ||
node = Node(data) | ||
node.next = self.head | ||
if self.head: | ||
self.head.prev = node | ||
self.head = node | ||
|
||
def insert_after(self, prev_node, data): | ||
if not prev: | ||
print("This node is not present in the list") | ||
return | ||
node = Node(data) | ||
|
||
node.next = prev_node.next | ||
prev_node.next = node | ||
node.prev = prev_node | ||
|
||
if node.next: | ||
node.next.prev = node | ||
|
||
def insert_at_end(self, data): | ||
|
||
node = Node(data) | ||
last = self.head | ||
|
||
|
||
|
||
|
||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
class Node: | ||
def __init__(self, data): | ||
self.data = data | ||
self.next = None | ||
|
||
class LinkedList: | ||
def __init__(self): | ||
self.head = None | ||
|
||
# insert a new node at the beginning | ||
def insert_at_begin(self, data): | ||
new_node = Node(data) | ||
new_node.next = self.head | ||
self.head = new_node | ||
|
||
# Inserts a new node after the given prev_node | ||
|
||
def insertAfter(self, prev, data): | ||
new_node = Node(data) | ||
|
||
if not prev: | ||
print("prev must be in the node") | ||
return | ||
new_node = Node(data) | ||
curr= self.head | ||
while curr: | ||
if curr != prev: | ||
curr = curr.next | ||
new_node.next = prev.next | ||
prev.next = new_node | ||
|
||
def insertAtEnd(self, data): | ||
new_node = Node(data) | ||
if not self.head: | ||
self.head = new_node | ||
curr = self.head | ||
while curr.next: | ||
curr = curr.next | ||
self.head.next = new_node | ||
|
||
# Given a reference to the head of a list | ||
# and a position, delete the node at a given position | ||
def deleteNode(self, position): | ||
if not self.head: | ||
return | ||
curr = self.head | ||
if position == 0: | ||
self.head = curr.next | ||
curr = None | ||
return | ||
for index in range(position -1): | ||
curr = curr.next | ||
if curr is None: | ||
break | ||
if curr is None: | ||
return | ||
next = curr.next.next | ||
curr.next = None | ||
curr.next = next | ||
|
||
|
||
# This function prints contents of linked list | ||
# starting from head | ||
def printList(self): | ||
temp = self.head | ||
res = [] | ||
while temp: | ||
res.append(temp.data) | ||
temp = temp.next | ||
return res | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
# Start with the empty list | ||
|
||
|
||
llist = LinkedList() | ||
llist.insert_at_begin(6) | ||
llist.insert_at_begin(6) | ||
llist.insert_at_begin(1) | ||
llist.insertAtEnd(4) | ||
llist.insertAtEnd(9) | ||
llist.insertAfter(llist.head.next, 8) | ||
llist.printList() | ||
print("Created Linked List: ") | ||
llist.printList() | ||
llist.deleteNode(4) | ||
print ("\nLinked List after Deletion at position 4: ") | ||
llist.printList() |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
class Solution: | ||
def partition(self, arr, low, high): | ||
i = (low -1) | ||
pivot = arr[high] | ||
|
||
for j in range(low, high): | ||
if arr[j] < pivot: | ||
i = i+1 | ||
arr[i],arr[j] = arr[j], arr[i] | ||
arr[i+1], arr[high] = arr[high], arr[i+1] | ||
return (i+1) | ||
|
||
def quick_sort(self, arr, low, high): | ||
if low < high: | ||
pi = self.partition(arr, low, high) | ||
self.quick_sort(arr, low, pi -1) | ||
self.quick_sort(arr, pi + 1, high) | ||
|
||
|
||
|
||
|
||
arr = [10, 7, 8, 9, 1, 5] | ||
n = len(arr) | ||
Solution().quick_sort(arr,0,n-1) | ||
print ("Sorted array is:") | ||
for i in range(n): | ||
print ("%d \t" %arr[i] + " ") |