Created
October 22, 2017 12:42
-
-
Save ahmedash95/83aa57b2b823420000a935f1a683e0de 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(object): | |
def __init__(self, data, prev, next): | |
self.data = data | |
self.prev = prev | |
self.next = next | |
class DoubleList(object): | |
head = None | |
tail = None | |
size = 0 | |
def append(self, data): | |
new_node = Node(data, None, None) | |
if self.head is None: | |
self.head = self.tail = new_node | |
else: | |
new_node.prev = self.tail | |
new_node.next = None | |
self.tail.next = new_node | |
self.tail = new_node | |
self.size += 1 | |
def remove(self, node_value): | |
current_node = self.head | |
while current_node is not None: | |
if current_node.data == node_value: | |
# if it's not the first element | |
if current_node.prev is not None: | |
current_node.prev.next = current_node.next | |
current_node.next.prev = current_node.prev | |
else: | |
# otherwise we have no prev (it's None), head is the next one, and prev becomes None | |
self.head = current_node.next | |
current_node.next.prev = None | |
current_node = current_node.next | |
self.size -= 1 | |
def show(self): | |
print "Show list data:" | |
current_node = self.head | |
while current_node is not None: | |
print current_node.prev.data if hasattr(current_node.prev, "data") else None, | |
print current_node.data, | |
print current_node.next.data if hasattr(current_node.next, "data") else None | |
current_node = current_node.next | |
print "*"*50 | |
word = raw_input("Enter word : ") | |
d = DoubleList() | |
for char in word: | |
d.append(char) | |
# check if word is palindrome or not | |
def isPalindrome(d): | |
first = d.head | |
last = d.tail | |
size = d.size | |
while(size > 0): | |
if first.data != last.data: | |
return False | |
first = first.next | |
last = last.prev | |
size -= 1 | |
return True | |
print isPalindrome(d) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment