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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值