python 简单讲解基本的数据结构

##顺序类型
"""
内存 -- 存储单元,(一个字节:8位),32位-4个字节,64位8个字节

int(32 - 4个字节;64 - 8个字节)
char (一个字符,占一个字节,ascii/utf8)(一个字符,占两个字节,Unicode/gkb)以上为英文为例
左侧为最高位,顺序存储有大端优先,和小端优先:大端优先及高位在前

一、顺序表
1、顺序表 元素本身连续存储,每个元素所占大小相同,元素的物理地址可以通过起始地址加上存储单元的大小乘以元素的下标之和
2、表结构:表头信息(最大容量,当前元素个数) + 数据区(如python中的Array)
    2.1 顺序表的两种基本实现方式:
        1、遗体式结构,及表头信息和数据区房子一个存储区中
        2、分离式机构,及将表头信息和数据区分别放入两个不同的存储区


二、链表
1、链表结构:数据区(elem) --> 连接区(next) [尾部链接区指向空]
2、单项列表
总结:顺序表和链表都是线性表
"""


##节点实现(链表)
"""
class Node:
    '''
    节点
    '''
    def __init__(self,elem):
        self.elem = elem
        self.next = None


#单链表

class SingleLink:
    '''
    单链表
    '''


    def __init__(self,node=None):
        self._head = node

    def is_empty(self):
        if self._head:
            return False
        return True

    def length(self):
        cur = self._head
        count = 0
        while cur:
            count += 1
            cur = cur.next
        return count

    def travel(self):
        cur = self._head
        count = 0
        while cur:
            count += 1
            print(cur.elem)
            cur = cur.next

    #添加元素到第一个位置
    def add(self,item):
        cur = self._head
        node = Node(item)
        node.next = cur
        self._head = node

    def append(self,item):
        cur = self._head
        while cur.next:
            cur = cur.next
        node = Node(item)
        cur.next = node

    def insert(self,pos,item):
        if pos ==0:
            self.add(item)
        else:
            cur = self._head
            count = 0
            while cur.next and count != pos-1:
                cur = cur.next
                count += 1
            node = Node(item)
            node.next = cur.next
            cur.next = node

    def remove(self,item):
        cur = self._head
        count = 0
        try:
            while count != self.length():
                if count == 0 and cur.elem == item:
                    self._head = cur.next
                elif count != 0 and count != self.length()-2 and cur.next.elem == item:
                    cur.next = cur.next.next
                elif count == self.length()-2 and cur.next.elem == item:
                    cur.next = None

                cur = cur.next
                count += 1
        except AttributeError as e:
            print(e)



    def search(self,item):
        pass


node = Node(100)
obj_single = SingleLink(node)
obj_single.add(202)
obj_single.add(205)
obj_single.insert(3,400)
obj_single.append(203)
obj_single.append(204)
obj_single.insert(3,400)
obj_single.travel()
obj_single.remove(400)
print("============================")
obj_single.travel()
"""
##双向列表

class Node:
    def __init__(self,item):
        self.item = item
        self.next = None
        self.pro = None


class DoubleLink:

    def __init__(self,node=None):
        self._head = node

    def is_empty(self):
        if self._head:
            return False
        return True

    def length(self):
        cur = self._head
        count = 0
        while cur:
            count += 1
            cur = cur.next
        return count

    def travel(self):
        cur = self._head
        count = 0
        while cur:
            count += 1
            print(cur.item)
            cur = cur.next
            pass
    def add(self,item):
        node = Node(item)
        self._head.pro = node
        node.next = self._head
        self._head = node
        pass
    def append(self,item):
        cur = self._head
        while cur.next:
            cur = cur.next
        node = Node(item)
        cur.next = node
        node.pro = cur

    def search(self,item):
        cur = self._head
        count = 0
        while cur.item != item:
            cur = cur.next
            count += 1
        print(count)
        return count
    def remove(self,item):
        cur = self._head
        count = 0
        while cur.item != item:
            cur = cur.next
            count += 1
            if count == self.length()-1:
                print("Not Find Want To Remove Value")
                break
        if count == 0:
            cur.next.pro = None
            self._head =cur.next
            cur = None
        elif count == self.length()-1:
            cur.pro.next = None
        else:
            cur.pro.next = cur.next
            cur.next.pro = cur.pro




node = Node(10)
double_link = DoubleLink(node)
print(double_link.is_empty())
double_link.travel()
print("==============")

double_link.add(60)
double_link.append(70)
double_link.append(80)
double_link.add(90)
double_link.travel()
double_link.remove(100)
print("环境中的变量:"+str(dir()))
double_link.travel()
#栈(stack),也称为堆栈,是一种容器 ,先进后出(First in Last Out)
#队列 ,一段添加另一端取数,先进先出(First In First Out)

你可能感兴趣的:(python 简单讲解基本的数据结构)