當前位置: 首頁>>代碼示例>>Python>>正文


Python LinkedList.add方法代碼示例

本文整理匯總了Python中LinkedList.LinkedList.add方法的典型用法代碼示例。如果您正苦於以下問題:Python LinkedList.add方法的具體用法?Python LinkedList.add怎麽用?Python LinkedList.add使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在LinkedList.LinkedList的用法示例。


在下文中一共展示了LinkedList.add方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: LLQueue

# 需要導入模塊: from LinkedList import LinkedList [as 別名]
# 或者: from LinkedList.LinkedList import add [as 別名]
class LLQueue(object):
    def __init__(self, size=0):
        self._list = LinkedList()
        self._size = size

    def enqueue(self, data):
        if (self._size > 0 and self._list.count < self._size):
            self._list.add(data)
        else:
            raise Exception("Too many items in the queue")

    def dequeue(self):
        if (self._list.count == 0):
            raise Exception("No items in the queue")
        node = self._list[0]
        del self._list[0]
        return node

    def isEmpty(self):
        return (self._list.count == 0)

    def clear(self):
        self._list.clear()

    @property
    def size(self):
        return self._size

    def __len__(self):
        return len(self._list)
開發者ID:bertothunder,項目名稱:coursera-algorithms,代碼行數:32,代碼來源:LLQueue.py

示例2: __init__

# 需要導入模塊: from LinkedList import LinkedList [as 別名]
# 或者: from LinkedList.LinkedList import add [as 別名]
class Queue:
    def __init__(self):
        self.__elements = LinkedList()

    # Adds an element to this queue
    def enqueue(self, e):
        self.__elements.add(e)
    
    # Removes an element from this queue
    def dequeue(self):
        if self.getSize() == 0:
            return None
        else:
            return self.__elements.removeAt(0)
    
    # Return the size of the queue
    def getSize(self):
        return self.__elements.getSize()
    
    # Returns a string representation of the queue
    def __str__(self):
        return self.__elements.__str__()

    # Return true if queue is empty 
    def isEmpty(self):
        return self.getSize() == 0
開發者ID:EthanSeaver,項目名稱:Python-Projects,代碼行數:28,代碼來源:Queue.py

示例3: read_file

# 需要導入模塊: from LinkedList import LinkedList [as 別名]
# 或者: from LinkedList.LinkedList import add [as 別名]
def read_file(file):
    '''
    Read CSV File, Important Only Read column 1 and column 2
    
    Parameters
    ----------
    file : str, path of file 
        path for file to read and fill dataset
    
    Returns
    -------
    dataset: LinkedLink dataset
    '''
    #Open File
    with open(file, 'rb') as csvfile:
        reader = csv.reader(csvfile)
        dataset=LinkedList()
        #for row in file:
        for row in reader:
                #if length of row >= 2:
                if len(row) >= 2:
                    #add to LinkedList Float Row[0] and Row[1]
                    try:
                        dataset.add(( float(row[0]) , float(row[1]) ))
                    except ValueError:
                        log.warning("%s Problem whit row" % row[0])
                else:
                    #mesnsaje: row whit insuficient data.
                    log.warning("row whit insuficient data or Empty Row")
        return dataset
開發者ID:arpagon,項目名稱:assignaments,代碼行數:32,代碼來源:fit.py

示例4: sum_list

# 需要導入模塊: from LinkedList import LinkedList [as 別名]
# 或者: from LinkedList.LinkedList import add [as 別名]
def sum_list(l1, l2):
  """
  returns a list which contains the addition of input two lists.
  """
  lsum = LinkedList()
  carry = 0
  l1_node = l1.head
  l2_node = l2.head

  while l1_node or l2_node:
    val = carry
    if l1_node:
      val += l1_node.value
      l1_node = l1_node.next

    if l2_node:
      val += l2_node.value
      l2_node = l2_node.next

    lsum.add(val % 10)
    carry = val / 10
  if carry:
    lsum.add(carry)

  return lsum
開發者ID:vivekkeshore,項目名稱:ctci,代碼行數:27,代碼來源:list_sum_2_5.py

示例5: test_1

# 需要導入模塊: from LinkedList import LinkedList [as 別名]
# 或者: from LinkedList.LinkedList import add [as 別名]
 def test_1(self):
     A = LinkedList()
     A.add(1)
     A.add(2)
     A.add(6)
     A.add(3)
     res = s.recursive_search(A, 2)
     self.assertIsNot(res,None)
開發者ID:smwade,項目名稱:ACME-1,代碼行數:10,代碼來源:testcases.py

示例6: test_deleteNone

# 需要導入模塊: from LinkedList import LinkedList [as 別名]
# 或者: from LinkedList.LinkedList import add [as 別名]
 def test_deleteNone(self):
     l = LinkedList(1)
     l.add(2)
     l.add(3)
     l.delete(5)
     self.assertEquals(l.val, 1)
     self.assertEquals(l.next.val, 2)
     self.assertEquals(l.next.next.val, 3)
開發者ID:jackchi,項目名稱:interview-prep,代碼行數:10,代碼來源:test_linkedList.py

示例7: test_add

# 需要導入模塊: from LinkedList import LinkedList [as 別名]
# 或者: from LinkedList.LinkedList import add [as 別名]
 def test_add(self):
     l = LinkedList(0)
     l.add(1)
     l.add(2)
     l.add(3)
     self.assertEquals(0, l.val)
     self.assertEquals(1, l.next.val)
     self.assertEquals(2, l.next.next.val)
     self.assertEquals(3, l.next.next.next.val)
開發者ID:jackchi,項目名稱:interview-prep,代碼行數:11,代碼來源:test_linkedList.py

示例8: test_add

# 需要導入模塊: from LinkedList import LinkedList [as 別名]
# 或者: from LinkedList.LinkedList import add [as 別名]
    def test_add(self):
        myLinkedList = self.getLinkedList()
        myLinkedList.add(-1)
        self.assertEqual(myLinkedList.toArray()[0], Node(-1).getData())

        # test empty list
        myLinkedList = LinkedList()
        myLinkedList.add(-1)
        self.assertEqual(myLinkedList.toArray()[0], Node(-1).getData())
開發者ID:avihoo,項目名稱:singly-linked-list,代碼行數:11,代碼來源:TestLinkedList.py

示例9: linked_list_test

# 需要導入模塊: from LinkedList import LinkedList [as 別名]
# 或者: from LinkedList.LinkedList import add [as 別名]
    def linked_list_test(self):
        """Test that the singly linked list is able to correctly append items. Asserts that the list contains all items
        AND that the items are int he correct order.
        """
        items = range(-10, 10)                      # randomly ordered list
        shuffle(items)

        linked_list = LinkedList()                  # linked list containing same items in same order
        for item in items:
            linked_list.add(item)

        ll_items = [x for x in linked_list.next()]  # contents of linked list as a python list
        self.assertEqual(ll_items, items)           # test that the contents and order are the same as the original list
開發者ID:Clarksj4,項目名稱:Algorithms_and_Data_Structures,代碼行數:15,代碼來源:test.py

示例10: reverse_list_test

# 需要導入模塊: from LinkedList import LinkedList [as 別名]
# 或者: from LinkedList.LinkedList import add [as 別名]
    def reverse_list_test(self):
        """Test the the linked list is correctly able to reverse the order of items within itself. Asserts that the list
        has the correct ordering of items after being reversed AND after being reversed a second time.
        """
        items = range(1, 10)                        # test list of items ( numbers 1 - 10)

        linked_list = LinkedList()
        for item in items:                          # add each test item to linked list
            linked_list.add(item)

        linked_list.reverse()                       # reverse order of both lists
        items = reversed(items)
        for ll_item, item in zip(linked_list.next(), items):    # check that each index holds same item
            self.assertEqual(ll_item, item)

        linked_list.reverse()                       # reverse linked list again
        for ll_item, item in zip(linked_list.next(), range(1, 10)):    # check that each index holds same item
            self.assertEqual(ll_item, item)
開發者ID:Clarksj4,項目名稱:Algorithms_and_Data_Structures,代碼行數:20,代碼來源:test.py

示例11: LLStack

# 需要導入模塊: from LinkedList import LinkedList [as 別名]
# 或者: from LinkedList.LinkedList import add [as 別名]
class LLStack(object):
	def __init__(self):
		self._list = LinkedList()

	def push(self, data=None):
		self._list.add(data)

	def pop(self):
		try:
			node = self._list[-1]
			del self._list[-1]
			return node.data
		except IndexError:
			# Mimicing the array error message
			raise IndexError("pop from empty stack")

	def __len__(self):
		return len(self._list)
開發者ID:bertothunder,項目名稱:coursera-algorithms,代碼行數:20,代碼來源:LLStack.py

示例12: __init__

# 需要導入模塊: from LinkedList import LinkedList [as 別名]
# 或者: from LinkedList.LinkedList import add [as 別名]
class EdgeDB:
    def __init__(self):
        self.list = LinkedList()
        self.totalEdge = 0

    def addEdge(self, n):
        self.list.add(None, n)                  # Need to check for duplicates
        self.totalEdge += 1

    def deleteUser(self, id):
        l = []
        user = []
        count = 0
        pre = None
        n = self.list.start
        while n:
            if (n.v.A == id) or (n.v.B == id):  # If either A or B is the UserID
                if n.v.B == id:                 # If Followed user(B) matches, stash the follower
                    user.append(n.v.A)
                else:                           # If Following user(A) matches, count
                    count += 1
                if pre:                         # Actual delete process
                    pre.next = n.next
                    n = n.next
                else:
                    self.list.start = n.next
                    n = self.list.start
                self.totalEdge -= 1             # Update
            else:
                pre = n
                n = n.next
        l.append(user)
        l.append(count)
        return l    # [List of UserID following the input, # of Users which the input follows]

    def getFollowID(self, id):
        l = []
        n = self.list.start
        while n:
            if n.v.A == id:
                l.append(n.v.B)
            n = n.next
        return l
開發者ID:Rikuforever,項目名稱:2011171061_LHJ,代碼行數:45,代碼來源:DB.py

示例13: sum_lists

# 需要導入模塊: from LinkedList import LinkedList [as 別名]
# 或者: from LinkedList.LinkedList import add [as 別名]
def sum_lists(ll_a, ll_b):
    n1, n2 = ll_a.head, ll_b.head
    ll = LinkedList()
    carry = 0
    while n1 or n2:
        result = carry
        if n1:
            result += n1.value
            n1 = n1.next
        if n2:
            result += n2.value
            n2 = n2.next

        ll.add(result % 10)
        carry = result // 10

    if carry:
        ll.add(carry)

    return ll
開發者ID:5imp1e,項目名稱:CtCI-6th-Edition-Python,代碼行數:22,代碼來源:5_Sum_Lists.py

示例14: read_file

# 需要導入模塊: from LinkedList import LinkedList [as 別名]
# 或者: from LinkedList.LinkedList import add [as 別名]
def read_file(file):
    '''
    Read CSV File, Important Only Read column 1
    
    :param file: path for file to read 
    :returns dataset: LinkedLink dataset
    '''
    with open(file, 'rb') as csvfile:
        #dialect = csv.Sniffer().sniff(csvfile.read(1024))
        #csvfile.seek(0)
        reader = csv.reader(csvfile)
        dataset=LinkedList()
        for row in reader:
                if len(row) >= 1:
                    try:
                        dataset.add(float(row[0]))
                    except ValueError:
                        log.warning("%s Is not a float" % row[0])
                else:
                    log.warning("Empty Row")
        return dataset
開發者ID:arpagon,項目名稱:assignaments,代碼行數:23,代碼來源:mean_std.py

示例15: getLinkedList

# 需要導入模塊: from LinkedList import LinkedList [as 別名]
# 或者: from LinkedList.LinkedList import add [as 別名]
    def getLinkedList(self):
        myLinkedList = LinkedList()
        for i in range(0, 10):
            myLinkedList.add(i)

        return myLinkedList
開發者ID:avihoo,項目名稱:singly-linked-list,代碼行數:8,代碼來源:TestLinkedList.py


注:本文中的LinkedList.LinkedList.add方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。