Array and List algorithm

  1. Static data structure (static data structure)
    array.
    Consecutively allocated memory space (contiguous allocation).
    Fast read modify, delete, added slowly.
    In the early establishment of a static data structure must be declared maximum fixed memory space may be occupied, so easily lead to waste of memory.
  2. Dynamic data structure (dynamic data structure)
    chain (linked list).
    Discontinuous memory space.
    Easy insertion and deletion, to find trouble, trouble design.
    Memory allocation is done only when the program is executed, without prior notice, to fully save memory.
  • Matrix multiplication
    matrix A (M, N), the matrix B (N, P) -> matrix C (M, P)
    to expand into a one-dimensional array operates
    assignment:

      for i in range(M):
          for j in range(N):
              A[ i * N + j ] = num

    Multiplying:

      for i in range(M):
          for j in range(P):
              temp = 0
              for k in range(N):
                  temp = temp + int( A[ i * N + k ] ) * int( B[ k * P + j ] )
              C[ i * P + j ] = temp
  • Transposed matrix of
    matrix A (M, M)

      for i in range(M):
          for j in range(i):
              if i!= j:
                  A[i][j], A[j][ i] = A[j][ i], A[i][ j]
  • The establishment of a one-way linked list
    • Generating dynamic allocation list node - the definition of a class
      • Defines a pointer field: points to the next node
      • Defining at least one data field
        class student:
            def __init__(self):
                self.name = ""
                self.score = 0
                self.next = None

      The establishment of student node singly linked list:

        class Student:
            def __init__(self):
                self.name = ""
                self.score = 0
                self.next = None
      
        head = Student()
        ptr = head  # 存取指针的位置
        for i in range(5):
            new_data = Student()
            new_data.name = "张三"
            new_data.score = 90
            # ptr :指针,不破坏 head
            ptr.next = new_data
            ptr = ptr.next
      
        print('-->', ptr)
        for i in range(6):
            print('==>', head.next)
            head = head.next
    • Connectivity way linked list
      cascade concatenation

        def concatlist(ptr1, ptr2):
            ptr = ptr1
            while ptr.next != None:
                ptr = ptr.next
            ptr.next = ptr2
            return ptr1
    • Way linked list of nodes deleted
      1. Delete the first node
        python top = head head = head.next
      2. Remove the last node
        python ptr.next = tail ptr.next = None
      3. Delete intermediate node
        python Y = ptr.next ptr.next = Y.next
        def del_ptr(head, ptr):
            top = head
            if ptr.num == top.num:  # [ 1 ]
                head = head.next
            else:
                while top.next != ptr:  # 找到删除节点的前一个位置
                    top = top.next
                if ptr.next == None:  # [ 2 ]
                    top.next = None
                else:
                    top.next = ptr.next  # [ 3 ]
            return head
        ptr = head
        find = 0  # 未找到标记
        while ptr != None:
            if ptr.num == findword:  # findword 要查找的 num
                ptr = del_ptr(head, ptr)
                head = ptr
                find += 1
            ptr = ptr.next
    • Reverse way linked list
      not know the location of a node requires three pointers variables.

        class Employee:
            def __init__(self):
                self.num = 0
                self.next = None
      
        def invert(head):
            next = head
            cur = None
            while next != None:
                pre = cur
                cur = next
                next = next.next
                cur.next = pre
            return cur

      Type structures which typically comprise an array of several properties: the start address, the dimension, the lower limit on the index, the number of elements, the array type.

      class Node:
        def __init__(self):
            self.data = ""
            self.next = None
      
        def insert(T, X, Y):
            node = Node()
            node.data = Y
            if T == None:
                T = I
                # I.next = None
            else:
                node.next = X.next
                X.next = node

Guess you like

Origin www.cnblogs.com/catyuang/p/11760485.html