Created
August 17, 2015 05:59
-
-
Save jweinst1/d1f152c457da39f5aa5b to your computer and use it in GitHub Desktop.
Circular Linked List.py
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 Link (object): | |
| def __init__(self, first=None, rest=None): | |
| self.first = first | |
| self.rest = rest | |
| def get_data(self): | |
| return self.first | |
| def get_next(self): | |
| return self.rest | |
| def __getitem__(self, i): | |
| if i == 0: | |
| return self.first | |
| get = self | |
| while i > 0: | |
| get = get.rest | |
| i -= 1 | |
| if get == None: | |
| raise IndexError('The Sentence Index is Out of Range.') | |
| return get.first | |
| class Circular_Link (object): | |
| def __init__(self, things): | |
| assert len(things) > 2 | |
| last = Link(things[len(things)-1]) | |
| first = Link(things[len(things)-2], last) | |
| index = len(things)-2 | |
| while index > 0: | |
| index -= 1 | |
| first = Link(things[index], first) | |
| last.rest = first | |
| self.circle = first | |
| def __getitem__(self, i): | |
| return self.circle[i] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment