Skip to content

Commit 0fe429f

Browse files
committed
Updated files!
1 parent ad5f8e6 commit 0fe429f

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed

LinkedList/QueueLinkedList.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
2+
3+
class Node:
4+
def __init__(self, value=None) -> None:
5+
self.value = value
6+
self.next = None
7+
8+
def __str__(self) -> str:
9+
return str(self.value)
10+
11+
class LinkedList:
12+
def __init__(self) -> None:
13+
self.head = None
14+
self.tail = None
15+
16+
def __iter__(self):
17+
node = self.head
18+
while node:
19+
yield node
20+
node = node.next
21+
22+
23+
class Queue:
24+
def __init__(self) -> None:
25+
self.linkedList = LinkedList()
26+
27+
def __str__(self) -> str:
28+
values = [str(x) for x in self.linkedList]
29+
return ' '.join(values)
30+
31+
def enqueue(self, value):
32+
newNode = Node(value)
33+
if self.linkedList.head == None:
34+
self.linkedList.head = newNode
35+
self.linkedList.tail = newNode
36+
else:
37+
self.linkedList.tail.next = newNode
38+
self.linkedList.tail = newNode
39+
40+
def isEmpty(self):
41+
if self.linkedList.head == None:
42+
return True
43+
else:
44+
return False
45+
46+
def dequeue(self):
47+
if self.isEmpty():
48+
return "Queue Underflow! No any element."
49+
else:
50+
tempNode = self.linkedList.head
51+
if self.linkedList.head == self.linkedList.tail:
52+
self.linkedList.head = None
53+
self.linkedList.tail = None
54+
else:
55+
self.linkedList.head = self.linkedList.head.next
56+
return tempNode
57+
58+
def peek(self):
59+
if self.isEmpty():
60+
return "Queue Underflow! No any element."
61+
else:
62+
return self.linkedList.head
63+
64+
65+
def deleteQueue(self):
66+
self.linkedList.head = None
67+
self.linkedList.tail = None
68+
69+
70+
custQueue = Queue()
71+
custQueue.enqueue(10)
72+
custQueue.enqueue(11)
73+
custQueue.enqueue(12)
74+
print(custQueue)
75+
print(custQueue.dequeue())
76+
print(custQueue)
77+
print(custQueue.peek())
78+
print(custQueue)

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ In this repo I upload everything I do related to data structures and algorithm t
33

44
Hope it helps you. Don't forgot to :star:.
55

6-
# Data Structure
6+
# Data Structure Implementation
7+
### Linked List
78
| Name | Completed | Tested |
89
| :----: | :----: | :----: |
9-
| LinkedList | <ul><li>- [x] </li></ul> | <ul><li>- [x] </li></ul> |
10+
| [LinkedList](https://github.com/sushant097/Data-Structure-Algorithms-Collections-Python/blob/master/LinkedList/LinkedList.py) | <ul><li>- [x] </li></ul> | <ul><li>- [x] </li></ul> |
11+
| [Circular LinkedList](https://github.com/sushant097/Data-Structure-Algorithms-Collections-Python/blob/master/LinkedList/CircularSinglyLinkedList.py) | <ul><li>- [x] </li></ul> | <ul><li>- [x] </li></ul> |
12+
| [Doubly LinkedList](https://github.com/sushant097/Data-Structure-Algorithms-Collections-Python/blob/master/LinkedList/DoublyLinkedList.py) | <ul><li>- [x] </li></ul> | <ul><li>- [x] </li></ul> |
13+
| [Queue with LinkedList](https://github.com/sushant097/Data-Structure-Algorithms-Collections-Python/blob/master/LinkedList/QueueLinkedList.py) | <ul><li>- [x] </li></ul> | <ul><li>- [x] </li></ul> |

0 commit comments

Comments
 (0)