You are on page 1of 10

BSCS-402 DATA STRUCTURE

LAB 6

1|Page
OBJECT 1: Write a python program to check of the given string is palindrome or not
using stack.
S.no STACK(CLASS).PY
1 class Stack :
2 # Creates an empty stack.
3 def __init__( self ):
4 self._theItems = list()
5
6 # Returns True if the stack is empty or False otherwise.
7 def isEmpty( self ):
8 return len( self ) == 0
9
10 # Returns the number of items in the stack.
11 def __len__ ( self ):
12 return len( self._theItems )
13
14 # Returns the top item on the stack without removing it.
15 def peek( self ):
16 assert not self.isEmpty(), "Cannot peek at an empty stack"
17 return self._theItems[-1]
18
19 # Removes and returns the top item on the stack.
20 def pop( self ):
21 assert not self.isEmpty(), "Cannot pop from an empty stack"
22 return self._theItems.pop()
23
24 # Push an item onto the top of the stack.
25 def push( self, item ):
26 self._theItems.append( item )
27
28 def show(self):
29 return(self._theItems)
30
31 def isPalindrome(self):
32 Reverse = self._theItems.copy()
33 # Reverse.reverse()
34 # for i in range(len(self)):
35 # if self._theItems[i] != Reverse[i]:
36 # return False
37 # break
38 # else:
39 # return True
40 s1 = Stack()
41

2|Page
OBJECT 1:
S.no PYTHON
1 def isPalindromeStack(string1):
2 isPalindrome = True
3 stack1 = Stack()
4 stack2 = Stack()
5
6 # Pushing string into stack
7 # a = input("Enter a Word for Palindrome Check : ")
8 for i in string1:
9 stack1.push(i)
10
11 # Copy of the Original Stack for comparison
12 stack1copy = stack1.show().copy()
13
14 # Transfer of Stack
15 for i in range(stack1.__len__()):
16 stack2.push(stack1.pop())
17
18 for i in range(len(stack1copy)):
19 if stack1copy[i] != stack2.show()[i]:
20 isPalindrome = False
21 break
22
23 else:
24 isPalindrome = True
25
26 return (isPalindrome)

RESULT:

3|Page
OBJECT 2: Write a python program to check of the given string is palindrome or not using
deque.

S.no DEQUE(CLASS).PY
1 class Deque:
2 def __init__(self):
3 self.que = list()
4
5 def isEmpty(self):
6 return len(self) == 0
7
8 def __len__(self):
9 return len(self.que)
10
11 def peekRight(self):
12 assert not self.isEmpty(), "Cannot peek at an empty stack"
13 return self.que[-1]
14
15 def peekLeft(self):
16 assert not self.isEmpty(), "Cannot peek at an empty stack"
17 return self.que[0]
18
19 def popRight(self):
20 assert not self.isEmpty(), "Cannot pop from an empty stack"
21 return self.que.pop()
22
23 def popLeft(self):
24 assert not self.isEmpty(), "Cannot pop from an empty stack"
25 return self.que.pop(0)
26
27 def pushLeft(self, item):
28 self.que.insert(0,item)
29
30 def pushRight(self, item):
31 self.que.append(item)
32
33 def show(self):
34 return(self.que)

4|Page
OBJECT 2:
S.no PYTHON
1 def isPalindromeDeque(string1):
2 isPalindrome = True
3 d1 = Deque()
4 d2 = Deque()
5 # a = input("Enter a Word for Palindrome Check : ")
6 for i in string1:
7 d1.pushRight(i)
8 # print(d1.show())
9
10 queCopy = d1.show().copy()
11
12
13 for i in range(d1.__len__()):
14 d2.pushLeft(d1.popLeft())
15 # print(d2.show())
16
17 for i in range(len(queCopy)):
18 if d2.show()[i] != queCopy[i]:
19 isPalindrome = False
20 break
21
22 else:
23 isPalindrome = True
24
25 return(isPalindrome)

RESULT:

5|Page
Compare the running time of both of your programs and interpret the
results.

CONCLUSION:
Since both are using python list as basic data type no significant change can be seen.

OBJECT 3:
Write a program that takes two positive integers (say M, N) as inputs.
It should first enqueue N random integers one by one into a queue, and then dequeue
the N elements.
This process should be repeated M times.
Use the following in turn to store the queue:

(a) a usual Python list, and


(b) the alternative implementation suggested above, and compare the times taken by the two
implementations.

6|Page
S.no DOUBLELL(CLASS).PY
1 class Node:
2 def __init__(self,data):
3 self.data = data
4 self.next_address = None
5 self.previous_address = None
6
7 class Doubly_Linked_List:
8 def __init__(self):
9 self.head = None
10
11
12 def printll(self):
13 if self.head is None:
14 print("DOUBLY LINKED LIST IS EMPTY")
15 else:
16 n = self.head
17 while n is not None:
18 print(n.data,'--->',end=" ")
19 n = n.next_address
20
21 def printll_reverse(self):
22 print()
23 if self.head is None:
24 print("DOUBLY LINKED LIST IS EMPTY")
25 else:
26 n = self.head
27 while n.next_address is not None:
28 n = n.next_address
29 while n is not None:
30 print(n.data, '--->', end=" ")
31 n = n.previous_address
32
33 def insert_empty(self,data):
34 if self.head is None:
35 new_node = Node(data)
36 self.head = new_node
37 else:
38 print("DOUBLY LINK LIST IS NOT EMPTY")

7|Page
39
40 def add_begin(self,data):
41 new_node = Node(data)
42 if self.head is None:
43 self.head = new_node
44 else:
45 new_node.next_address = self.head
46 self.head.previous_address = new_node
47 self.head = new_node
48
49 def add_end(self,data):
50 new_node = Node(data)
51 if self.head is None:
52 self.head = new_node
53 else:
54 n = self.head
55 while n.next_address is not None:
56 n = n.next_address
57 n.next_address = new_node
58 new_node.previous_address = n
59 def delete_begin(self):
60 if self.head is None:
61 print("DOUBLY LINKED LIST IS EMPTY HENCE CAN'T
62 DELETE")
63 return
64 if self.head.next_address is None:
65 self.head = None
66 # print("LIST IS EMPTY")
67 else:
68 self.head = self.head.next_address
69 self.head.previous_address= None
70
71 def delete_end(self):
72 if self.head is None:
73 print("DOUBLY LINKED LIST IS EMPTY HENCE CAN'T
74 DELETE")
75 return
76 if self.head.next_address is None:
77 self.head = None
78 # print("LIST IS EMPTY")
79 else:
80 n = self.head
81 while n.next_address is not None:
82 n = n.next_address
83 n.previous_address.next_address = None

8|Page
S.n CODE.PY
o
1 def Enque_Deque_Using_List(N):
2 import random
3 d1 = Deque()
4
5 for i in range(N):
6 d1.pushLeft(random.randint(0,1000))
7 for i in range(N):
8 d1.popLeft()
9
10 def Enque_Deque_Using_DoubleLinkedList(N):
11 import random
12 dll = Doubly_Linked_List()
13 for i in range(N):
14 dll.add_end(random.randint(0,1000))
15 for i in range(N):
16 dll.delete_end()
17
18 def integers(M,N):
19 x = []
20 y1 = []
21 y2 = []
22 for i in range(N):
23 x.append(i)
24
25 y1.append(timeit.timeit(lambda:Enque_Deque_Using_DoubleLinkedList(i),
26 number = M))
27 y2.append(timeit.timeit(lambda:Enque_Deque_Using_List(i),number
28 = M))
29
30
31 plt.plot(x, y1)
32 plt.plot(x, y2)
33 plt.title("Enque and Deque ")
34 plt.xlabel("Integer Enque and Deque ")
35 plt.ylabel("Time/sec")
36 plt.legend(["DOUBLE LINKED LIST","`123 LIST"])
37 plt.show()

9|Page
RESULT:

Explanation of Awry behavior:


Python list is actually implemented in C. A pure Python program is unlikely to beat a C
counterpart, specially when that is written and optimized by experts for many years.

10 | P a g e

You might also like