02. --- memory data structure, sequence tables, single chain

A. Memory

  1. Basic Concepts

The role of computers: 
  is used to store data and binary arithmetic 


concept variables:
  is a memory of a computer
 

 

Measure the scope of computer memory size: 
  bit 
  byte 
  kB 
  mb computer memory space will have two basic properties of 
  the size of the 
  address

 

Different size data memory space occupied by 
  an integer:. 4 byte 
  a float :. 4 byte 
  Double :. 8 byte 
  characters:. 1 byte

 

Appreciated that a = the memory 10 (reference point) 
  pointing to: if a variable stores the address of a piece of memory space, it indicates that the variable pointing to the block of memory   references: if a variable stores the address of a piece of memory space, the variable can be a reference to the memory

 

II. Order table


Stored in the container element is in order, the structure of sequence table can be divided into two forms: a single data type, and multiple data types.
python lists and tuples belong to multiple data types order table

 

Single data type sequence table memory map (memory continuously open) 
memory map multiple data types order table (memory discontinuous open)
disadvantages sequence table: the structure of sequence table needs to know in advance the size of the data to apply for a contiguous memory space, and in Shiyou need to be expanded to move the data.

 

  1. Single Data Type

 

# A single type of sequence tables array 
Import numpy AS NP 
ARR = np.array ([ . 1 , 2 , . 3 ]) 
Print (ARR [ . 1 ]) 

# 2

 

 

 

  2. Multi-type data structure

 

 III. Singly linked list

No data with respect to the order of the relocation table, list structure may take advantage of computer memory, dynamic memory management for flexible and augmented: list. 
list (Linked list) is a common basic data structures, a linear table, as sequence table but not continuously stored data,
but each node (data storing means) stored in a node of the next information (ie, address)

 

   1. Basic Operation

. Is_empty (): the list is empty 

length ():. Chain length of 

travel ():. Traverse the entire list 

add (item):. Additive element list head 

append (item):. Additive element list tail 

insert (pos,. item): Specifies the location to add an element 

remove (item):. delete node 

search (item):. nodes to find whether there is

 

   2. The code structure

class Node():
    def __init__(self,item):
        self.item = item
        self.next = None #存储的链表中下一个节点的地址
class Link():
    def __init__(self):
        #_head永远指向None或者第一个节点的地址
        self._head = None      
    def add(self,item):
        node = Node(item)
        node.next = self._head
        self._head = node
    def travel(self):
        #cur存储的就是第一个节点的地址
        cur = self._head
        while cur:
            print(cur.item)
            cur = cur.next
    def is_Empty(self):
        return self._head == None
    def size(self):
        length = 0
        if self._head == None:
            return length
        #cur就是指向了第一个节点
        cur = self._head
        while cur:
            length += 1
            cur = cur.next
        return length
    def append(self,item):
        node = Node(item)
        #如果链表为空则执行如下操作
        if self._head == None:
            self._head = node
            return
        
        cur = self._head
        pre = None
        while cur:
            pre = cur
            cur = cur.next
        pre.next = node
        
    def search(self,item):
        ex = False
        
        cur = self._head
        while cur:
            if cur.item == item:
                ex = True
                break
            cur = cur.next
        return ex
    def insert(self,pos,item):
        node = Node(item)
        
        if pos <= 0:
            self.add(item)
            return
        if pos >= self.size():
            self.append(item)
            return
        
        cur = self._head
        pre = None
        
        for i in range(0,pos):
            pre = cur
            cur = cur.next
        pre.next = node
        node.next = cur
    def remove(self,item):
        cur = self._head
        pre = None
        if cur.item == item:
            self._head = cur.next
            return
        while cur:
            pre = cur
            cur = cur.next
            if cur.item == item:
                pre.next = cur.next
                break
        

 

link = Link()
link.add(1)
link.add(2)
link.add(3)
link.append(4)
# link.insert(-2,'hello')
link.remove(3)
link.travel()
结果:

2
1 4

 

Guess you like

Origin www.cnblogs.com/sc-1067178406/p/10960542.html