#!/usr/bin/env python # coding: utf-8 # ### Singly-linked list (SLL) # # Create an SLL class that will implement a singly-linked list to be used as a stack (last-in first-out). # # Use the Node class below as nodes carrying data in the SLL. # # Implement all of the methods that are awaiting your excellent code in the SLL note below. Remove the Python "pass" command in each method, replacing with with your own code. # # # In[1]: class Node: def __init__(self, data): self.data = data self.next = None def value(self): return self.data # In[2]: class SLL: # if data_list is provided, it must be a list of values, and they are pushed one-by-one onto SLL def __init__(self,data_list = None): pass # adds a Node with data to the front of SLL, assume data is not None def push(data): pass # If SLL is empty, returns None. Else returns the data of the first Node, and removes the Node. def pop(): pass # returns the data from the first Node, makes the first Node "current", else None if SLL is empty def getFirst(): pass # moves internally to the Node after "current" (if possible), and returns its data, else None # cannot be used after push() or pop() calls, only after getFirst() or getNext() def getNext(): pass # returns number of Nodes in SLL def length(): pass # empties SLL, returns None def clear(self): pass # In[3]: class SLL_Answer: def __init__(self,data_list = None): self.size = 0 self.first = None self.current = None if data_list: for data in data_list: self.push(data) # adds a Node with data to the front of SLL, assume data is not None, returns None def push(self,data): anode = Node(data) if self.size == 0: self.first = anode else: anode.next = self.first self.first = anode self.size += 1 self.current = None return None # If SLL is empty, returns None. Else returns the data of the first Node, and removes the Node. def pop(self): if self.size == 0: return None data = self.first.data self.first = self.first.next self.size -= 1 self.current = None return data # returns the data from the first Node, makes the first Node "current", else None if SLL is empty def getFirst(self): if self.size == 0: self.current = None return None self.current = self.first return self.current.data # moves internally to the Node after "current" (if possible), and returns its data, else None # must be called only after an uninterrupted sequence of getFirst() and getNext() calls. def getNext(self): if self.current == None: return None self.current = self.current.next if self.current == None: return None return self.current.data # returns number of Nodes in SLL def length(self): return self.size # empties SLL, returns None def clear(self): while self.pop(): pass return 5 # In[4]: # Test... input = [4,7,2,2,1,8] a = SLL_Answer(input) def forward(a): # use getFirst() and getNext() to acquire the list fwd = [] d = a.getFirst() if d: fwd.append(d) d = a.getNext() while d: fwd.append(d) d = a.getNext() return fwd def back(a): # use pop() to acquire the list bk = [] d = a.pop() while d: bk.append(d) d = a.pop() return bk # Make sure that both lists are the same, and they are the reverse of the input list fwd = forward(a) bk = back(a) if fwd == bk and fwd == input[::-1]: print('Success!') else: print('Nope. A problem...') # In[ ]: