Created
September 13, 2020 12:40
-
-
Save jamilnoyda/7427d337a36303e7748469ffdb1f3da4 to your computer and use it in GitHub Desktop.
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, next=None): | |
# self.curr = None | |
self.data = data | |
self.next = next | |
def get_data(self): | |
return self.data | |
def get_next(self): | |
return self.next | |
def __str__(self): | |
return str(self.data) | |
def set_next(self, node): | |
self.next = node | |
class LinkedList: | |
def __init__(self, head=None): | |
""" | |
""" | |
self.head = None | |
self.count = 0 | |
self.cur = None | |
def insert(self, data, node=None): | |
node = Node(data) | |
node.set_next(self.head) | |
self.head = node | |
self.count = self.count + 1 | |
self.cur = self.head | |
def __str__(self): | |
s = str() | |
curr = self.head | |
s = s + str(curr.data) | |
while curr.next: | |
curr = curr.next | |
s = s + str(curr.data) | |
return s | |
def __iter__(self): | |
return self | |
def __next__(self): | |
temp = self.cur | |
if self.cur.next: | |
self.cur = self.cur.next | |
return temp | |
def get_head(self): | |
return self.head | |
linked_list = LinkedList() | |
linked_list.insert(1) | |
linked_list.insert(2) | |
linked_list.insert(3) | |
print(linked_list) | |
i = iter(linked_list) | |
print(next(i)) | |
print(next(i)) | |
print(next(i)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
321
3
2
1