def __init__(self): """ Creating object of linked list class and call method data_transfer_file_to_list to read data from file and store in linked list """ self.user_list = LinkedList() GetAndStore.data_transfer_file_to_list(self.user_list)
class Queue: def __init__(self): self.queue = LinkedList() def enqueue(self, obj): self.queue.add(obj) def dequeue(self): return self.queue.poll_first() def size(self): return self.queue.size() def isEmpty(self): return self.queue.is_empty()
def hashing(self): """ Creating 11 linked list object for storing data and maintain 11 object of linked list in the list @read_from_file method call for reading data from number.txt and store data in respective linked list object by calculating hash Perform operation on object of linked list @append method call for storing data into file as number.txt """ for i in range(11): self.list.append(LinkedList()) number = read_from_file("number.txt") for i in range(len(number)): res = int(number[i]) % 11 self.list[res].sort(number[i]) # print(number[i]) self.display_hash() self.get_number() self.display_hash() try: f = open("number.txt", 'r+') except IOError: print("number.txt file not found.") else: f.truncate() for i in range(11): if self.list[i].is_empty(): continue append("number.txt", self.list[i])
class DeQueue: def __init__(self): self.queue = LinkedList() def enqueue_from_rear(self, obj): self.queue.add(obj) def enqueue_from_front(self, obj): self.queue.add_first(obj) def dequeue_from_rear(self): return self.queue.poll_last() def dequeue_from_front(self): return self.queue.poll_first() def size(self): return self.queue.size() def is_empty(self): return self.queue.is_empty()
class Stack: def __init__(self): self.stack = LinkedList() def push(self, obj): self.stack.add(obj) def pop(self): return self.stack.poll_last() def peak(self): return self.stack.get_last() def size(self): return self.stack.size() def isEmpty(self): return self.stack.is_empty()
# when see a new node, start from dummy # cur is the unsorted part while cur is not None: # 还有元素没有探索过 cur就是新元素 ==> cur 非空 执行, 空 跳出while pre = dummy # 每次第一步 先把dummy设置成prev while pre.next is not None and pre.next.value < cur.value: # prev.next 比 cur小 prev后移 pre = pre.next temp = cur.next # 新建一个temp 方便cur后移 cur.next = pre.next # cur 连接到 pre.next的那个元素, 断开cur 与temp的连接 pre.next = cur # pre.next断开与 prev下一个元素的连接, 连接 cur cur = temp # cur 后移一位到temp return dummy.next node1 = Node(-9) node2 = Node(1) node3 = Node(-13) node4 = Node(6) node5 = Node(5) node1.next = node2 node2.next = node3 node3.next = node4 node4.next = node5 lst = LinkedList() lst.head.next = node1 lst.printlist() node = insertionSortList(node1) lst.head.next = node lst.printlist()
def __init__(self): self.stack = LinkedList()
node = node.next return head # 删除所有重复的,一个不留 Given 1->1->1->2->3, return 2->3. def deleteDuplicates2(head): dummy = pre = Node(0) # dummy 留着作为开头连接后面, pre去连接后面的 dummy.next = head # dummy和LL连接起来 while head and head.next: if head.value == head.next.value: while head and head.next and head.value == head.next.value: head = head.next head = head.next pre.next = head else: pre = pre.next head = head.next return dummy.next lst = LinkedList() lst.add_last(1) lst.add_last(3) lst.add_last(3) lst.add_last(3) lst.add_last(5) lst.add_last(7) lst.add_last(7) lst.add_last(9) lst.head.next = deleteDuplicates2(lst.head.next) lst.printlist()
def __init__(self): self.customer_list = LinkedList() self.company_list = LinkedList()
class CompanyShareWithLinkedList: def __init__(self): self.customer_list = LinkedList() self.company_list = LinkedList() def add_new_customer(self): """ read customer data and maintain in customer linked list creating new customer data and then add new json data into linked list Update customer json file """ customer_data = FileLoad.json_file_load("Customer.json") for i in customer_data: self.customer_list.add(i) try: customer_id = input("Enter customer id : ").strip() customer_name = input("Enter customer name : ").strip().upper() balance = input( "Enter balance to add share market account : ").strip() if not customer_id.isnumeric() or not customer_name.isalpha( ) or not balance.isnumeric(): raise ValueError except ValueError: print("You have entered wrong data.") return data = {} new_customer = { "id": customer_id, "name": customer_name, "balance": balance, "data": data } flag = True for i in range(self.customer_list.size()): temp = self.customer_list.get_by_index(i) if temp["id"] == customer_id or str(temp["name"]) == customer_name: print("Your account already available.") flag = False break if flag: self.customer_list.add(new_customer) list = [] size = self.customer_list.size() for i in range(size): list.append(self.customer_list.poll_first()) FileLoad.json_file_write("Customer.json", list) def add_new_company(self): """ reading company data from company json file creating new company json data and then add into company linked list Update company json file """ company_data = FileLoad.json_file_load("Company.json") for i in company_data: self.company_list.add(i) try: company_name = input("Enter company name : ").strip().upper() no_of_share = input("Enter number of share : ").strip() price = input("Enter share per price : ").strip() balance = input("Enter balance amount : ").strip() if not company_name.isalpha() or not no_of_share.isnumeric( ) or not price.isnumeric() or not balance.isnumeric(): raise ValueError except ValueError: print("You have entered wrong data.") return new_company = { company_name: { "no_of_share": no_of_share, "price": price, "balance": balance } } flag = True for i in range(self.company_list.size()): data = self.company_list.get_by_index(i) if data.get(company_name) == None: print("Your account already available.") flag = False break if flag: self.company_list.add(new_company) list = [] size = self.company_list.size() for i in range(size): list.append(self.company_list.poll_first()) # print(self.company_list.display()) def remove_customer(self): """ reading customer data from json file and maintain in customer linked list take customer id and customer name and then find in customer linked list update customer json file after deletion """ customer_data = FileLoad.json_file_load("Customer.json") for i in customer_data: self.customer_list.add(i) try: customer_id = input("Enter customer id : ").strip() customer_name = input("Enter customer name : ").strip().upper() if not customer_id.isnumeric() or not customer_name.isalpha(): raise ValueError except ValueError: print("You have entered wrong data.") return for i in range(self.customer_list.size()): temp = self.customer_list.get_by_index(i) if temp["id"] == customer_id or str(temp["name"]) == customer_name: self.customer_list.delete_by_index(i) break list = [] size = self.customer_list.size() for i in range(size): list.append(self.customer_list.poll_first()) FileLoad.json_file_write("Customer.json", list) def remove_company(self): """ reading company data from json file and maintain in company linked list take company name and then find in company linked list update company json file after deletion :return: """ company_data = FileLoad.json_file_load("Company.json") for i in company_data: self.company_list.add(i) try: company_name = input("Enter company name : ").strip().upper() if not company_name.isalpha(): raise ValueError except ValueError: print("You have entered wrong data.") return flag = True for i in range(self.company_list.size()): data = self.company_list.get_by_index(i) if data.get(company_name) == None: self.company_list.delete_by_index(i) break list = [] size = self.company_list.size() for i in range(size): list.append(self.company_list.poll_first()) # print(self.company_list.display()) def save(self): pass
def __init__(self): """ Creating object of linked list for maintain and store data """ self.list = LinkedList()
class UnOrderedList: def __init__(self): """ Creating object of linked list for maintain and store data """ self.list = LinkedList() def unorderedList(self): """ Read data from file and store in linked list @read_from_file method call for reading data from file Take one input from user and search in linked list If data found in linked list then remove that data from linked list If not found then add that data into linked list. Write data from linked list into abc.txt file @write_from_file method call for writing data into abc.txt file """ word = IO.read_from_file("abc.txt") for j in word: self.list.add(j) self.list.display() w = input('\nEnter word :').strip() if self.list.search(w): self.list.remove(w) print("Removed") else: self.list.add(w) print("Added") self.list.display() IO.write_to_file("abc.txt", self.list)
def __init__(self): self.queue = LinkedList()
class OrderedList: def __init__(self): """ Creating object of linked list for maintaining data """ self.list = LinkedList() def orderList(self): """ Read data from file and store in sorted order in linked list @read_from_file method call for reading data from file Take one input from user and search in linked list If data found in linked list then remove that data from linked list If not found then add that data into linked list. Write data from linked list into abc.txt file @write_from_file method call for writing data into abc.txt file """ try: word = IO.read_from_file("abc.txt") except IOError: print("File not found.") for j in word: self.list.sort(j) self.list.display() try: w = input('\nEnter word :').strip() if int(w) / 100: raise ValueError # else: except ValueError: print("Error : You have given invalid input.") else: if self.list.search(w): self.list.remove(w) print("Removed") else: self.list.sort(w) print("Added") self.list.display() IO.write_to_file("abc.txt", self.list)
class Services: def __init__(self): """ Creating object of linked list class and call method data_transfer_file_to_list to read data from file and store in linked list """ self.user_list = LinkedList() GetAndStore.data_transfer_file_to_list(self.user_list) def account_created(self): """ Take two input from user one is account number and second is name And set amount 00 Customer object created and store in linked list """ account_no = int(input("Enter account number : ")) customer_name = input("Enter customer name : ") obj = Customer() obj.set_amount(00) obj.set_customer_name(customer_name) obj.set_account_no(account_no) self.user_list.add(obj) def get_data(self, account_no): """ :param account_no: Take one parameter as account number to check the customer account available in database or not :return: If found in database then return the object of customer """ head = self.user_list.get_head() while hash(head.object.get_account_no()) != hash(account_no): head = head.next if head == None: return -1 return head.object def withdraw(self, account_no, amount, name): """ :param account_no: Take account number for verification and validation :param amount: Take amount for withdraw and update :param name: Take customer name If balance will less than amount parameter then pring insufficient balance """ obj = self.get_data(account_no) if obj == -1: print( name, "Your account not created. First you should be create your account. " ) print("Enter detail for account creation.") self.account_created() return if int(obj.get_amount()) > amount: obj.set_amount(str(int(obj.get_amount()) - amount)) else: print("Insufficient balance.") def deposit(self, account_no, amount, name): """ :param account_no: Take account number for verification and validation :param amount: Take amount for deposit and update :param name: Take customer name Update balance with amount parameter """ obj = self.get_data(account_no) if obj == -1: print( name, "Your account not created. First you should be create your account. " ) print("Enter detail for account creation.") self.account_created() return obj.set_amount(str(int(obj.get_amount()) + amount)) def write(self): """ Write data in file from linked list """ GetAndStore.data_transfer_list_to_file(self.user_list)