├── Binary Search Tree.py ├── Graph Manipulation.py ├── Link lists.py ├── Pairing Heaps.py ├── README.md ├── Selection Sort.py └── Stack & Queue.py /Binary Search Tree.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # In[8]: 5 | 6 | 7 | class Node: 8 | 9 | def __init__(self,value): 10 | self.value = value 11 | self.left = None 12 | self.right = None 13 | 14 | class BST: 15 | 16 | def __init__(self): 17 | self.root = None 18 | 19 | def Insert(self, value): 20 | self.root = self.__InsertWrap(self.root, value) 21 | 22 | def __InsertWrap(self,x, value): 23 | if x == None: 24 | x = Node(value) 25 | 26 | else: 27 | if value < x.value: 28 | #print("left") 29 | x.left = self.__InsertWrap(x.left, value) 30 | else: 31 | #print("right") 32 | x.right = self.__InsertWrap(x.right, value) 33 | return x 34 | 35 | def InOrder(self): 36 | return self.__InOrder(self.root) 37 | 38 | def __InOrder(self, x): 39 | if x: 40 | self.__InOrder(x.left) 41 | print(x.value) 42 | self.__InOrder(x.right) 43 | 44 | 45 | def PreOrder(self): 46 | return self.__PreOrder(self.root) 47 | 48 | def __PreOrder(self,x): 49 | if x: 50 | print(x.value) 51 | self.__PreOrder(x.left) 52 | self.__PreOrder(x.right) 53 | 54 | def PostOrder(self): 55 | return self.__PostOrder(self.root) 56 | 57 | def __PostOrder(self,x): 58 | if x: 59 | self.__PostOrder(x.left) 60 | self.__PostOrder(x.right) 61 | print(x.value) 62 | 63 | def FindMin(self, x ): 64 | while(x.left != None): 65 | x = x.left 66 | return (x.value) 67 | 68 | def FindMax(self, x ): 69 | while(x.right != None): 70 | x = x.right 71 | return (x.value) 72 | 73 | def successor(self, x): 74 | if x.right != None: 75 | xx = x.right 76 | while (xx.left != None): 77 | xx = xx.left 78 | return xx.value 79 | 80 | def predecessor(self, x ): 81 | if x.left != None: 82 | xx = x.left 83 | while(xx.right != None): 84 | xx = x.right 85 | return xx.value 86 | 87 | def Height(self, x ): 88 | y = self.__Height(x) 89 | return y 90 | 91 | def __Height(self,x): 92 | if x==None: 93 | return 0 94 | else: 95 | return 1 + max(self.__Height(x.left),self.__Height(x.right)) 96 | 97 | def delete(self, node): 98 | x = self.root #rootNode, {Parent Node of desired Node} 99 | 100 | if node > x.value: 101 | y = x.right #desiredNode {Node to be delted} [iff on right] 102 | 103 | else: 104 | y = x.left #desiredNode [iff left] 105 | 106 | while(y.value != node): # Searching the Node to delete 107 | if node > y.value: 108 | x = x.right 109 | y = y.right 110 | 111 | else: 112 | x = x.left 113 | y = y.left 114 | 115 | 116 | #print("xr", x.right.value) 117 | #print("y", y.value) 118 | 119 | left = x.left #left of ParentNode 120 | right = x.right #right of ParentNode 121 | 122 | # Case 01 123 | 124 | if left.value == y.value and left.left is None and left.right is None: 125 | #print("left", x.left.value) 126 | x.left = None 127 | 128 | elif right.value == y.value and right.left is None and right.right is None: 129 | #print("right", x.right.value) 130 | x.right = None 131 | 132 | # Case 02 133 | 134 | elif left.value == y.value and (left.left is not None and left.right is None) or (left.left is None and left.right is not None): 135 | if left.left is not None: 136 | child = left.left 137 | 138 | elif left.right is not None: 139 | child = left.right 140 | x.left = None 141 | x.left = child 142 | #print("x",x.left.value) 143 | 144 | elif right.value == y.value and (right.left is not None and right.right is None) or (right.left is None and right.right is not None): 145 | if right.left is not None: 146 | child = right.left 147 | 148 | elif right.right is not None: 149 | child = right.right 150 | x.right = None 151 | x.right = child 152 | 153 | # Case 03 154 | 155 | elif left.value == y.value and left.left is not None and left.right is not None: 156 | lChild = left.left 157 | rChild = left.right 158 | min = self.successor(left) 159 | self.delete(min) 160 | minNode = Node(min) 161 | minNode.left = lChild 162 | minNode.right = rChild 163 | x.left = None 164 | x.left = minNode 165 | 166 | elif right.value == y.value and right.left is not None and right.right is not None: 167 | lChild = right.left 168 | rChild = right.right 169 | min = self.successor(right) 170 | self.delete(min) 171 | minNode = Node(min) 172 | minNode.left = lChild 173 | minNode.right = rChild 174 | x.right = None 175 | x.right = minNode 176 | 177 | # Driver Code 178 | 179 | a = BST() 180 | a.Insert(20) 181 | a.Insert(40) 182 | a.Insert(12) 183 | a.Insert(1) 184 | 185 | root = a.root 186 | 187 | print("Getting Inorder:") 188 | a.InOrder() 189 | 190 | print("\nGetting Preorder:") 191 | a.PreOrder() 192 | 193 | print("\nGetting PostOrder:") 194 | a.PostOrder() 195 | 196 | print("\nGetting Height:") 197 | print(a.Height(root)) 198 | 199 | print("\nGetting Minimum Node:") 200 | print(a.FindMin(root)) 201 | 202 | print("\nGetting Maximum Node:") 203 | print(a.FindMax(root)) 204 | 205 | print("\nGetting Successor:") 206 | print(a.successor(root)) 207 | 208 | print("\nGetting Predecessor:") 209 | print(a.predecessor(root)) 210 | 211 | print("\nDeleting a specific Node:") 212 | a.delete(12) 213 | 214 | print("\nTo cross-check deletion, printing preorder:") 215 | a.PreOrder() 216 | 217 | 218 | # In[ ]: 219 | 220 | 221 | 222 | 223 | -------------------------------------------------------------------------------- /Graph Manipulation.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # In[4]: 5 | 6 | 7 | class Vertex: 8 | 9 | def __init__(self,value): 10 | 11 | self.value=value 12 | self.dist=float("inf") 13 | self.predecessor=value #jesy graph mai 1 sy 2 gaya aur phir 2 sy 3 edge hai tou 3 ka pred 2 hai 14 | self.visited=False 15 | 16 | #MINIMUM PRIORITY QUEUE GIVES MINIMUM VALUE 17 | #THEN WE PUT OBJECTS IN A LIST 18 | #MINIMUM FUNCTION NEVER APPLYS ON OBJECTS THAT'S WHY WE WILL FIND MINIMUM VALUE FROM LINEAR SEARCH 19 | #PRIORITY QUEUE APPLIES WRT MINIMUM VALUE 20 | 21 | 22 | class PriorityQueue: 23 | 24 | def __init__(self): 25 | self.list=[] 26 | def is_empty(self): 27 | if self.list==[]: 28 | return True 29 | return False 30 | 31 | def Enqueue(self,v): 32 | self.list.append(v) 33 | 34 | def Extract_min(self): 35 | minloc=self.__Findmin() 36 | y=self.list[minloc]#pop kara re hain taky list emty hojai 37 | self.list.pop(minloc) 38 | return y 39 | 40 | def __Findmin(self): 41 | minloc= self.list[0] 42 | loc=0 43 | 44 | for i in range(len(self.list)): 45 | if self.list[i].dist0: 63 | neg.append(i) 64 | return neg 65 | 66 | def Dijkstra_shortest_path(self,source): 67 | #INTIALIZING A EMPTY LIST FOR SORTING VERTEX OBJECTS 68 | cost=[] 69 | q=PriorityQueue() 70 | 71 | #CREATE A VERTEX OBJECTS 72 | for i in range(self.vertex): 73 | cost.append(Vertex(i)) 74 | 75 | #INTIALIZING THE OBJECTS FROM SOURCE TO ZERO AND OTHER VERTICES TO INFINITY 76 | for i in range(self.vertex): 77 | cost[i].dist=float("inf") 78 | cost[source].dist=0 79 | 80 | #ENQUEUE OBJECTS 81 | for i in range(self.vertex): 82 | q.Enqueue(cost[i]) 83 | 84 | #CHECK UNTIL QUEUE IS EMPTY 85 | while not q.is_empty(): 86 | z=q.Extract_min() 87 | self.visited=True 88 | print("visited {}".format(z.value)) 89 | 90 | neighbours=self.Get_Directed_neighbour(z.value) 91 | 92 | for i in neighbours: 93 | if cost[i].visited ==False and cost[i].dist>z.dist+self.adjmat[z.value][i]: 94 | cost[i].dist=z.dist+self.adjmat[z.value][i] 95 | cost[i].pred=z.value 96 | for j in cost: 97 | print(j.value,j.dist,j.predecessor) 98 | 99 | # Array Stack 100 | 101 | a=DGraph(4) 102 | 103 | class ArrayStack(): 104 | 105 | def __init__(self, size): 106 | self.size = size 107 | self.data = [0 for i in range(size)] 108 | self.top = 0 109 | 110 | def Push(self, value): 111 | if self.top != ((self.size)): 112 | self.data[self.top] = value 113 | self.top += 1 114 | return(self.top , value) 115 | else: 116 | return("STACK OVERFLOW") 117 | 118 | def Pop(self): 119 | 120 | if self.top != 0: 121 | x = self.data[self.top-1] 122 | self.top -= 1 123 | return(x) 124 | 125 | else: 126 | return("STACK UNDERFLOW") 127 | 128 | def Peek(self): 129 | return(self.data[self.top-1]) 130 | 131 | def isEmpty(self): 132 | if self.top == 0: 133 | return True 134 | else: 135 | return False 136 | 137 | def Count(self): 138 | return len(self.data) 139 | 140 | def Print(self): 141 | print(self.data) 142 | 143 | # Queue 144 | 145 | class ArrayQueue: 146 | 147 | def __init__(self,size): 148 | self.size = size 149 | self.data = [0 for i in range(size)] 150 | self.rear = 0 151 | self.front = -1 152 | 153 | def IsEmpty(self): 154 | if self.rear == 0: 155 | return True 156 | else: 157 | return False 158 | 159 | def Enqueue(self, value): 160 | self.data[self.rear] = value 161 | self.rear = (self.rear+1)%self.size 162 | return self.data 163 | 164 | def Dequeue(self): 165 | x = self.data[self.front] 166 | self.front = (self.front+1)%self.size 167 | return x 168 | 169 | def Count(self): 170 | return len(self.data) 171 | 172 | def Print(self): 173 | print(self.data) 174 | 175 | # Graph 176 | 177 | class Graph: 178 | 179 | def __init__(self, vertex): 180 | self.vertex = vertex 181 | self.adj = [[0 for i in range(vertex)] for j in range(vertex)] 182 | 183 | def addEdge(self, src,dest): 184 | if src == dest: 185 | print("Source and dest r same") 186 | else: 187 | self.adj[src][dest] = 1 188 | self.adj[dest][src] = 1 189 | 190 | def printMatrix(self): 191 | for i in self.adj: 192 | for j in i: 193 | print(j, end=" ") 194 | print("\r") 195 | 196 | def getNeighbours(self,src): 197 | neighbours = [] 198 | for i in range(len(self.adj)): 199 | k = self.adj[i] 200 | #print(i[src+1]) 201 | if k[src] == 1: 202 | neighbours.append(i) 203 | return neighbours 204 | 205 | def DFS(self,source): 206 | visited = [] 207 | s = ArrayStack(self.vertex) 208 | s.Push(source) 209 | visited.append(source) 210 | while not (s.isEmpty()): 211 | x = s.Pop() 212 | print("visited", x) 213 | n = self.getNeighbours(x) 214 | for i in range(len(n)+1): 215 | if i not in visited: 216 | s.Push(i) 217 | visited.append(i) 218 | return visited 219 | 220 | def BFS(self, src): 221 | visited = [False] * self.vertex 222 | q = ArrayQueue(self.vertex) 223 | q.Enqueue(src) 224 | visited[src] = True 225 | 226 | while not (q.IsEmpty()): 227 | x = q.Dequeue() 228 | print("visited", x) 229 | n = self.getNeighbours(x) 230 | for i in range(len(n)+1): 231 | if visited[i] == False: 232 | print("HI", i) 233 | q.Enqueue(i) 234 | visited[i] = True 235 | 236 | def newBFS(self, src): 237 | visited = [] 238 | q = ArrayQueue(self.vertex) 239 | q.Enqueue(src) 240 | visited.append(src) 241 | while not q.IsEmpty(): 242 | x = q.Dequeue() 243 | print("visited", x) 244 | neighbours = self.getNeighbours(x) 245 | for i in neighbours: 246 | if i not in visited: 247 | q.Enqueue(i) 248 | visited.append(i) 249 | return visited 250 | 251 | # Driver Code 252 | 253 | a = Graph(3) 254 | a.addEdge(0,1) 255 | a.addEdge(1,2) 256 | 257 | print("Printing Matrix:") 258 | a.printMatrix() 259 | 260 | print("\nGetting Neighbours:") 261 | print(a.getNeighbours(1)) 262 | 263 | print("\nPerforming DFS:") 264 | print(a.DFS(2)) 265 | 266 | print("\nPerforming BFS:") 267 | print(a.newBFS(2)) 268 | 269 | 270 | -------------------------------------------------------------------------------- /Link lists.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # In[11]: 5 | 6 | 7 | # singly linklist 8 | 9 | class node: 10 | def __init__(self,value): 11 | self.value=value 12 | self.next=None 13 | 14 | class linkedList: 15 | def __init__(self): 16 | self.head=None 17 | self.tail=None 18 | 19 | def InsertAtFirst(self,value): 20 | newnode=node(value) 21 | if self.head is None: 22 | self.head=newnode 23 | self.tail=newnode 24 | else: 25 | newnode.next=self.head 26 | self.head=newnode 27 | 28 | def InsertAtEnd(self,value): 29 | newnode=node(value) 30 | if self.head is None: 31 | self.head=newnode 32 | self.tail=newnode 33 | else: 34 | self.tail.next=newnode 35 | self.tail=newnode 36 | 37 | def InsertAfter(self,item,value): 38 | x=self.head 39 | while (x != None): 40 | if x.value == item: 41 | break 42 | x=x.next 43 | if x is None: 44 | print("Item not found") 45 | else: 46 | newnode=node(value) 47 | newnode.next = x.next 48 | x.next=newnode 49 | 50 | def DeleteAtFirst(self): 51 | if self.head is None: 52 | print("Link List is empty") 53 | else: 54 | self.head=self.head.next 55 | 56 | def DeleteAtEnd(self): 57 | if self.head is None: 58 | raise Exception ("Link list is emoty") 59 | else: 60 | p=self.head 61 | q=p.next 62 | while (q.next is not None): 63 | p=p.next 64 | q=q.next 65 | p.next= None 66 | 67 | def DeleteByValue(self,value): 68 | x=self.head 69 | if x!=None: 70 | while x!=None: 71 | if x.value==value: 72 | break 73 | y=x 74 | x=x.next 75 | if x==None: 76 | return 77 | y.next=x.next 78 | x=None 79 | 80 | def Print(self): 81 | a=self.head 82 | while(a): 83 | print(a.value, end=" ") 84 | a=a.next 85 | 86 | # driver code 87 | 88 | print('Single Link List') 89 | ob=linkedList() 90 | 91 | ob.InsertAtFirst(1) 92 | ob.InsertAtFirst(2) 93 | ob.InsertAtFirst(3) 94 | ob.InsertAtFirst(4) 95 | ob.InsertAtFirst(5) 96 | 97 | print("\nWhen inserted first:") 98 | ob.Print() 99 | ob.InsertAtEnd(7) 100 | 101 | print("\nWhen inserted last:") 102 | ob.Print() 103 | 104 | print("\nWhen inserted after:") 105 | ob.InsertAfter(4,6) 106 | ob.Print() 107 | 108 | print("\nWhen deleted from first:") 109 | ob.DeleteAtFirst() 110 | ob.Print() 111 | 112 | print("\nWhen deleted from end:") 113 | ob.DeleteAtEnd() 114 | ob.Print() 115 | 116 | print("\nWhen deleted by value:") 117 | ob.DeleteByValue(2) 118 | ob.Print() 119 | 120 | 121 | 122 | 123 | # In[12]: 124 | 125 | 126 | # Doubly linklist 127 | 128 | class Node: 129 | def __init__(self,value): 130 | self.value=value 131 | self.next=None 132 | self.previous=None 133 | 134 | class DLL: 135 | def __init__(self): 136 | self.head=None 137 | self.tail=None 138 | 139 | def insertatfirst(self,value): 140 | newNode=Node(value) 141 | if self.head==None: 142 | self.head=newNode 143 | self.tail=newNode 144 | else: 145 | newNode.next=self.head 146 | self.head.previous=newNode 147 | self.head=newNode 148 | 149 | def insertatlast(self,value): 150 | newNode=Node(value) 151 | if self.head==None: 152 | self.head=newNode 153 | self.tail=newNode 154 | else: 155 | self.tail.next=newNode 156 | newNode.previous=self.tail 157 | self.tail=newNode 158 | 159 | def insertafter(self,after, val): 160 | newNode=Node(val) 161 | x=self.head 162 | while x.value!=after and x!=None: 163 | x=x.next 164 | newNode.next=x.next 165 | x.next.previous=newNode 166 | x.next=newNode 167 | newNode.previous=x 168 | 169 | def deleteatfirst(self): 170 | self.head=self.head.next 171 | self.head.previous=None 172 | 173 | def deleteatlast(self): 174 | self.tail=self.tail.previous 175 | self.tail.next=None 176 | 177 | def deletebyvalue(self,value): 178 | if self.head is None: 179 | raise Exception("LIST IS EMPTY") 180 | else: 181 | x=self.head 182 | while x.value!=value and x!=None: 183 | x=x.next 184 | n = x.next 185 | x =x.previous 186 | n.prev = x 187 | x.next = n 188 | del(x) 189 | 190 | 191 | def Print(self): 192 | x = self.head 193 | if x is None: 194 | raise Exception("LIST IS EMPTY") 195 | else: 196 | while (x): 197 | print(x.value, end=" ") 198 | x = x.next 199 | print("\n") 200 | 201 | ob=DLL() 202 | print("Inserting at Start:") 203 | ob.insertatfirst(1) 204 | ob.Print() 205 | 206 | print("Inserting at End") 207 | 208 | ob.insertatlast(3) 209 | ob.insertatlast(2) 210 | ob.insertatlast(3) 211 | ob.insertatlast(4) 212 | ob.insertatlast(5) 213 | ob.insertatlast(6) 214 | ob.Print() 215 | 216 | print("Inserting after a specific Value: ") 217 | ob.insertafter(2,0) 218 | ob.Print() 219 | 220 | print("Deleting from Start:") 221 | ob.deleteatfirst() 222 | ob.Print() 223 | 224 | print("Deleting from end: ") 225 | ob.deleteatlast() 226 | ob.Print() 227 | 228 | print("Deleting a specific Value: ") 229 | ob.deletebyvalue(4) 230 | ob.Print() 231 | 232 | -------------------------------------------------------------------------------- /Pairing Heaps.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # In[1]: 5 | 6 | 7 | class PairingHeap: 8 | 9 | def __init__(self): 10 | self.heap = [0] 11 | self.size = 0 12 | self.root=min(self.heap) 13 | 14 | def insert(self, value): 15 | self.heap.append(value) 16 | self.size += 1 17 | self.BubbleUp(self.size) 18 | 19 | def merge(self, root1, root2): 20 | if root1 is None: 21 | return root2 22 | elif root2 is None: 23 | return root1 24 | elif root1< root2: 25 | self.heap.append(root2) 26 | return root1 27 | 28 | else: 29 | self.heap.append(root1) 30 | return root2 31 | 32 | 33 | def delete(self): 34 | self.root=None 35 | self.heap[1], self.heap[-1] = self.heap[-1], self.heap[1] 36 | deletedValue = self.heap.pop(-1) 37 | self.size -= 1 38 | return deletedValue 39 | 40 | 41 | def findMin(self): 42 | if self.root==None: 43 | print("Pairing Heap is empty") 44 | else: 45 | return self.root 46 | 47 | 48 | def getSize(self): 49 | print (len(self.heap)) 50 | 51 | # Two Pass Method 52 | 53 | def BubbleUp(self, key): 54 | if self.heap[key] > self.heap[key // 2] or key <= 1: # parent and child 55 | return () 56 | else: 57 | self.heap[key], self.heap[key // 2] = self.heap[key // 2], self.heap[key] 58 | return self.BubbleUp(key // 2) 59 | 60 | 61 | # Driver Code 62 | 63 | heap = PairingHeap() 64 | 65 | print("Pairing Heap upon Insertion:") 66 | heap.insert(2) 67 | heap.insert(18) 68 | heap.insert(20) 69 | heap.insert(1) 70 | heap.insert(3) 71 | heap.insert(8) 72 | print(heap.heap) 73 | print() 74 | 75 | print("Getting Mininmum in pairing heap:") 76 | print(heap.findMin()) 77 | print() 78 | 79 | print("Merging roots in pairing heaps:") 80 | print("Smaller root =",heap.merge(5,1)) 81 | print("Heap after merge:") 82 | print(heap.heap) 83 | print() 84 | 85 | print("Pairing Heap after deleting:") 86 | heap.delete() 87 | heap.delete() 88 | print(heap.heap) 89 | print() 90 | 91 | print("Size of Pairing Heap:") 92 | heap.getSize() 93 | 94 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python-Data-Structures-Implementation: 2 | 3 | All the data structures including: 4 | 5 | * Singly linklist 6 | * Doubly linklist 7 | * Stacks 8 | * Queues 9 | * Binary search tree 10 | * Graph 11 | * Pairing heaps etc 12 | 13 | are implemented in this repository along with their relevant functions. 14 | 15 | ##### Post Script: 16 | 17 | * It was an empty, forked repository earlier and it is intended to make a PR daily with a new data stucture so presence of any specific data structure is subjected to its pull request. If its not found, it will be uploaded later. 18 | * View Branches for unavailable data structure as I'm waiting to get a merge with the original repo. 19 | -------------------------------------------------------------------------------- /Selection Sort.py: -------------------------------------------------------------------------------- 1 | def selectionSort(array, n): 2 | 3 | for i in range(n): 4 | minimum = i 5 | 6 | for j in range(i + 1, n): 7 | 8 | # to sort in descending order, change > to < in this line 9 | # select the minimum element in each loop 10 | if array[j] < array[minimum]: 11 | minimum = j 12 | 13 | # put min at the correct position 14 | (array[i], array[minimum]) = (array[minimum], array[i]) 15 | 16 | 17 | data = [ ] 18 | size = int(input("Enter size of array : ")) 19 | print("Enter array elements: ") 20 | for i in range(size): 21 | e=int(input()) 22 | data.append(e) 23 | selectionSort(data, size) 24 | print('Sorted Array in Ascending Order:') 25 | print(data) 26 | -------------------------------------------------------------------------------- /Stack & Queue.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # In[7]: 5 | 6 | 7 | class ArrayStack: 8 | def __init__(self,size): 9 | self.size=size 10 | self.List=[0 for i in range(size)] 11 | self.top=0 12 | def Push(self,value): 13 | if self.top!=self.size: 14 | self.List[self.top]=value 15 | self.top=self.top+1 16 | print(self.List) 17 | else: 18 | print("Stack Overflow") 19 | def Pop(self): 20 | if self.IsEmpty(): 21 | print("Stack Underflow") 22 | self.top=self.top-1 23 | x=self.List[self.top] 24 | return x 25 | 26 | def IsEmpty(self): 27 | if self.top==0: 28 | print("Stack Underflow") 29 | def Peek(self): 30 | return (self.List[self.top - 1]) 31 | def Count(self): 32 | print ("The number of elements in stack is",self.size,"!!!") 33 | def strExp(self,String): 34 | s=ArrayStack(len(String)) 35 | for i in String: 36 | if i == "(" or i== "{" or i == "[": 37 | s.Push(i) 38 | 39 | elif i == ")" or i== "}" or i== "]": 40 | s.Pop() 41 | 42 | if s.IsEmpty() == True: 43 | print("True") 44 | def Print(self): 45 | print(self.List) 46 | 47 | 48 | # Driver Code 49 | 50 | size=4 51 | ob=ArrayStack(size) 52 | 53 | print("Pushing elements into stack: ") 54 | 55 | ob.Push(7) 56 | ob.Push(2) 57 | ob.Push(8) 58 | ob.Push(9) 59 | print("\n") 60 | ob.Push(2) 61 | ob.Print() 62 | 63 | print("\nPopping element from stack: ") 64 | 65 | ob.Pop() 66 | ob.Print() 67 | 68 | print("\nPeek in stack: ") 69 | 70 | 71 | ob.Peek() 72 | ob.Print() 73 | 74 | print("\nCounting elements in stack: ") 75 | ob.Count() 76 | 77 | print("\nString expression in stack: ") 78 | String = "{}()[" 79 | ob.strExp(String) 80 | 81 | 82 | # In[6]: 83 | 84 | 85 | class Node: 86 | def __init__(self, value): 87 | self.value = value 88 | self.next = None 89 | 90 | class Queue: 91 | def __init__(self): 92 | self.rear = None # head 93 | self.front = None # tail 94 | 95 | def enqueue(self,value): 96 | newnode=Node(value) 97 | if self.rear==None: 98 | self.front=newnode 99 | self.rear=newnode 100 | else: 101 | self.rear.next=newnode 102 | self.rear=newnode 103 | 104 | def dequeue(self): 105 | if self.front==None: 106 | print("Stack underflow") 107 | else: 108 | self.front=self.front.next 109 | 110 | def Print(self): 111 | a=self.front 112 | while a!=None: 113 | print(a.value,end=" ") 114 | a=a.next 115 | print() 116 | 117 | 118 | ob=Queue() 119 | print("when inserted in queue") 120 | ob.enqueue(4) 121 | ob.enqueue(3) 122 | ob.enqueue(2) 123 | ob.Print() 124 | print("when deleted from queue") 125 | ob.dequeue() 126 | ob.Print() 127 | 128 | 129 | # In[ ]: 130 | 131 | 132 | 133 | 134 | --------------------------------------------------------------------------------