Data Structures and Algorithms (2) and the order list table

List of table do not need to do the following

  1. add (item): to add a data item to the list, assuming the original item is not present in the list
  2. remove (item): remove the item from the list, the list is modified, the original item should be stored in a table
  3. search (item): Find item in the list, return type bool value
  4. Whether isEmpy () returns the list is empty
  5. size (): Returns the list contains the number of data items
  6. append (item): to add a data item to the end of the table, assuming the original item is not present in the list
  7. index (item): returns the location data item in the table
  8. insert (pos, item): the data into position pos, assuming item not originally present in the list, while the list has sufficient original multiple data items, item allows occupy the position pos
  9. pop (): the number of items removed from the end of the list, assuming the original list has at least one data item
  10. pop (pos): removal position pos items, assuming the presence of the original list position pos

Using a linked list to achieve unordered list

  1. Storage location of data items and no rules, but if a connection point between the data items, the relative position may be maintained before and after the
  2. The first and last items need to be explicitly demonstrated, a team is the first team ,, is a tail, no longer back up data

List implementation

  1. Node Node:
    each node comprising at least two information: the data item itself, and a reference information pointing to the next node.
    Note next to no sense None of the next node, and this is very important
class Node:
    def __init__(self,initdata):
        self.data = initdata
        self.next = None
    def getData(self):
        return self.data
    def getNext(self):
        return self.next
    def setData(self,newdata):
        self.data = newdata
    def setNext(self,newnext):
        self.next = newnext
temp = Node(93)
temp.getData()
# 可以采用连接节点的方式构建数据集来实现无序表
# 链表的第一个和字后一个节点最重要
# 链表实现: add方法实现
def add(self,item):
    temp = Node(item)
    temp.setNext(self.head)
    self.head = temp
# 链表实现size
def size(self):
    current = self.head
    count = 0
    while current !=None:
        count+= 1
        current = current.getNext()
    return count

Ordered list

  1. Is a sorted list of data items according to their properties to determine a position in Kobe list

    Operation as orderly as defined in the table

  2. OrderList (): creates an empty sorted list
  3. add (item): adding a data item in the table, and keep the overall sequence, the absence of this original
  4. search(item)
  5. isEmpty()
  6. size()
  7. index(item)
  8. pop()
  9. pop(pos)

# 习题一 合并两个有序链表
class Solution:
    def mergeTwolist(self, l1, l2):
        if l1 is None:
            return l2
        elif l2 is None:
            return l1
        elif ll1.val > l2.val:
            l1=l2
            l2 = l1
        p = l1
        while p is not None:
            while l2 is not None (p.next is None or l2.val < p.next.val):
                tmp = l2
                l2 = l2.next
                tmp.next = p.next
                p.next = tmp
                p = p.next
            p = p.next
        return l1
        
# 习题2 删除链表中倒数第n个节点
class Solution:
    def removeN(self,head,n):
        h = listNode(-1)
        h.next = head
        p = h
        q = h
        for _ in range(n+1):
            q = q.next
        while q != None:
            p = p.next
            q = q.next
        p.next = p.next.next
        return h.next

Guess you like

Origin www.cnblogs.com/yangjiez/p/12169340.html