Skip to content

Instantly share code, notes, and snippets.

@jamilnoyda
Created September 13, 2020 12:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamilnoyda/7427d337a36303e7748469ffdb1f3da4 to your computer and use it in GitHub Desktop.
Save jamilnoyda/7427d337a36303e7748469ffdb1f3da4 to your computer and use it in GitHub Desktop.
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))
@jamilnoyda
Copy link
Author

321
3
2
1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment