Python中的数据结构

目录

  1. ·队列

  2. ·堆栈

  3. ·单链表


分别使用数据结构来完成学生信息管理


1.队列

class Info:
    def __init__(self, name, age):
        self.name = name
        self.age = age


class QNode:
    def __init__(self, info):
        self.data = info

    def __str__(self):
        return  str((self.data.name, self.data.age))


class Queue:
    def __init__(self, Maxsize=100):
        self.top = 0
        self.base = self.top
        self.queue = []
        self.Maxsize = Maxsize


    def is_empty(self):
        return self.top == self.base

    def pop(self):
        if self.is_empty():
            print('队列为空')
        else:
            newNode = self.queue.pop(0)
            self.top = self.top + 1
            return newNode

    def push(self, qnode):
        if self.top - self.base == self.Maxsize:
            print('队列满')
        else:
            self.queue.append(qnode)
            self.base = self.base + 1
            print(f'{qnode}入队列成功')

    def Print_queue(self):
        if self.is_empty():
            print('队列为空')
            return False
        while not self.is_empty():
            Node = self.pop()
            if Node != None:
                print(f'{self.top}姓名为:{Node.data.name},年龄为:{Node.data.age}')
        return True
        # print(self.queue)

q = Queue()
print(q.is_empty())
Q1 = QNode(Info('张三',15))
Q2 = QNode(Info('李四',16))
Q3 = QNode(Info('王五',17))
Q4 = QNode(Info('赵六',18))
q.push(Q1)
q.push(Q2)
q.push(Q3)
q.push(Q4)
print(q.is_empty())
print(q.Print_queue())

2.堆栈

可以使用列表来模拟堆栈

class Info:
    def __init__(self, name, age):
        self.name = name
        self.age = age


class SNode:
    def __init__(self, info):
        self.data = info

    def __str__(self):
        return  str((self.data.name, self.data.age))

class Stack:
    def __init__(self,Maxsize = 100):
        self.Maxsize = Maxsize
        self.top = 0
        self.base = 0
        self.stack = []

    def is_empty(self):
        return self.top == self.base

    def push(self,node):
        self.stack.append(node)
        print(f'{node}入栈成功!')
        self.top += 1

    def pop(self):
        self.top -=1
        return self.stack[self.top]

    def is_funll(self):
        return self.top == self.Maxsize

    def Print_Stack(self):
        while not self.is_empty():
            Node = self.pop()
            print(f'{self.top+1}的姓名为:{Node.data.name},年龄为:{Node.data.age}')


s = Stack()
print(s.is_empty())
S1 = SNode(Info('张三',15))
S2 = SNode(Info('李四',16))
s.push(S1)
s.push(S2)
print(s.is_funll())
s.Print_Stack()

3.单链表

简单实现一下学生信息的单链表

class Lnode:  # 每个节点都有Item型的elem和一个默认为None的指针,该指针的类型是节点Lnode类型
    def __init__(self, item):
        self.Item = item
        self.next = None


class LinkList:  # 链表类
    def __init__(self, node=None):
        self._head = node  # 只需要定义一个头节点,默认为None即可

    def if_empty(self):  # 判断链表是否为空
        return self._head is None

    def length(self):  # 计算链表的长度
        cur = self._head
        count = 0
        while cur is not None:
            cur = cur.next
            count += 1
        return count

    def trave_List(self):  # 遍历链表,打印各节点的数据
        cur = self._head
        while cur is not None:
            print("学生姓名为:{}".format(cur.Item.Name))
            print("学生学号为:{}".format(cur.Item.Num))
            print("学生成绩为:{}".format(cur.Item.Score))
            cur = cur.next
        print()

    def Head_insert(self, Item):  # 头插法插入数据
        newLnode = Lnode(Item)
        if self._head is not None:  # 如果链表的头指针不为空,则要把新节点的next指针指向链表的头指针
            # 如果链表的头指针为空,则直接将头指针的next指向新的节点
            newLnode.next = self._head
        self._head = newLnode  # 将链表的头指针指向新的节点

    def Tail_Insert(self, item):  # 尾插法插入数据
        if self._head is None:  # 如果头指针为空,说明链表没数据,则插入的数据可直接放在首元节点上
            self.Head_insert(item)
        else:  # 头指针不为空,则要遍历到尾部,在插入数据
            cur = self._head
            while cur.next is not None:
                cur = cur.next
            newLnode = Lnode(item)
            cur.next = newLnode

    def insert(self, pos, item):
        if pos < 0:  # 如果输入的位置小于0则在头部插入数据
            self.Head_insert(item)
        elif pos > self.length() - 1:
            self.Tail_Insert(item)
        else:
            count = 0
            cur = self._head
            while cur.next is not None and count < pos - 1:
                cur = cur.next
                count += 1
            newLnode = Lnode(item)
            newLnode.next = cur.next
            cur.next = newLnode

    def DeleteLnode(self, pos):
        if pos < 0 or pos > self.length():
            print("删除的位置上没有数据")
            return
        else:
            cur = self._head
            count = 1
            while count < pos - 1:
                cur = cur.next
                count += 1
            newLnode = cur.next
            cur = newLnode.next
            del newLnode

    def search(self, Name):
        cur = self._head
        flage = False
        while cur.next is not None:
            if cur.item.Name == Name:
                flage = True
                newLnode = cur
                print("查询成功!\n学生信息为:{},{},{}".format(newLnode.item.Num, newLnode.item.Name, newLnode.item.Score))
            else:
                cur = cur.next
        if not flage:
            print("查询失败!")

    def reverse(self):
        cur, prev = self._head, None
        while cur:
            cur.next, prev, cur = prev, cur, cur.next
        self._head = prev


def main():
    List1 = LinkList()
    if List1.if_empty():
        print("链表为空")
    else:
        print("链表非空")
    item1 = Item("张三", 1, 150)
    item2 = Item("李四", 2, 150)
    item3 = Item("王五", 3, 150)
    item4 = Item("赵六", 4, 150)
    List1.Head_insert(item2)
    List1.Head_insert(item1)
    List1.Tail_Insert(item3)
    List1.Tail_Insert(item4)

    List1.trave_List()


if __name__ == "__main__":
    main()

 更新时间 2021/5/30

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python ,常见的内建数据结构包括: 1. 列表(List):一种有序、可变的数据类型,可以包含不同类型的数据,使用方括号 [] 表示。 2. 元组(Tuple):一种有序、不可变的数据类型,可以包含不同类型的数据,使用圆括号 () 表示。 3. 集合(Set):一种无序、不重复的数据类型,可以进行交集、并集、差集等操作,使用大括号 {} 或 set() 函数表示。 4. 字典(Dictionary):一种无序的键值对(key-value)数据类型,使用大括号 {} 表示,其每个键对应一个值。 5. 堆(Heap):一种可以快速找到最大或最小值的数据结构,由于 Python 没有内建的堆数据类型,可以使用 heapq 模块实现。 6. 队列(Queue):一种先进先出(FIFO)的数据结构Python 内建的队列类型有 queue.Queue、multiprocessing.Queue、asyncio.Queue 等。 7. 栈(Stack):一种后进先出(LIFO)的数据结构Python 可以使用列表实现栈。 8. 布隆过滤器(Bloom Filter):一种空间效率极高的数据结构,用于检测一个元素是否在集合Python 可以使用第三方库如 pybloomfiltermmap 实现。 9. 数组(Array):一种有序、可变的数据类型,与列表类似,但只能存储相同类型的数据,Python 可以使用第三方库如 NumPy 实现。 以上是 Python 常见的内建数据结构,使用这些数据结构可以方便地实现各种算法和数据处理任务。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值