python算法--链表、队列、栈

一、链表

  1. 算法原理:
    通过一个个节点组成的,每个节点都包含了value的基本元素,能保存数据之间逻辑顺序,但存储空间不必按照顺序存储。
    链表的基本元素:
    node节点:每个节点存储两个部分,左边部分为值域用来存放用户数据,右边部分为指针域用来存放下一个元素指针
    head节点:永远指向第一个节点
    tail节点:永远指向最后一个节点
    None:链表中最后一个节点的指针域未None值
  2. 代码:
class Node:
    def __init__(self, value=None, next=None):
        self.value = value
        self.next = next

    def __repr__(self):
        return str(self.value)
    

class LinkChain(object):
    def __init__(self,head):
        self.head = head
        self.tail = self.head.next

    # 链表长度
    def __len__(self):
        count = 0
        if self.head is None:
            return count
        curr = self.head
        while curr:
            count += 1
            curr = curr.next
        return count

    # 正序打印链表所有内容
    def printList(self):
        all = []
        if self.head is None:
            return all
        curr = self.head
        while curr:
            all.append(curr.value)
            curr = curr.next
        return all

    # 在链表开头插入
    def insertToFront(self, number):
        if self.head is None:
            return None
        node = Node(number, self.head)
        self.head = node
        return number

    # 在链表末尾插入
    def append(self, number):
        if self.head is None:
            return None
        curr =self.head
        while curr.next:
            curr = curr.next
        curr.next = Node(number)
        return number

    def find(self, number=None):
        if number is None or self.head is None:
            return None
        curr = self.head
        while curr:
            if curr.value == number:
                return curr.value
            curr = curr.next
        return False

    # 在链表中删除数字
    def deleteData(self, number=None):
        if number is None:
            return None
        if self.head.value == number:
            self.head = self.head.next
            return self.head
        curr = self.head
        while curr.next:
            if curr.next.value == number:
                curr.next = curr.next.next
                return curr
            curr = curr.next
        return False


if __name__ == '__main__':
    node1 = Node(1)
    node2 = Node(2)
    node3 = Node(3)
    node1.next = node2
    node2.next = node3
    l = LinkChain(node1)
    print('在链表开头插入4:', l.insertToFront(4))
    print('在链表末尾插入5:', l.append(5))
    print('在链表中查找数字:', l.find(5))
    print('在链表中删除数字:', l.deleteData(4))
    print('正序打印链表:', l.printList())

执行结果:
在链表开头插入4: 4
在链表末尾插入5: 5
在链表中查找数字: 5
在链表中删除数字: 1
正序打印链表: [1, 2, 3, 5]

二、队列

  1. 算法原理:
    先进先出。
    队列中基本元素:
    head:头结点
    tail:尾结点
    队列实例方法:
    enquenen:入队列
    dequeue:出队列
  2. 代码:
class Node(object):
    def __init__(self, value=None, next=None):
        self.value = value
        self.next = next

class Queue(object):
    def __init__(self):
        self.head = None
        self.tail = None

# 入队列
    def enQueue(self, n):
        node = Node(n)
        if self.head is None:
            self.head = node
            self.tail = node
        else:
            self.tail.next = node
            self.tail = node

# 出队列
    def deQueue(self):
        if self.head is None:
            return None
        else:
            temp = self.head.value
            self.head = self.head.next
            if self.head is None:
                self.tail = None
        return temp


if __name__ == '__main__':
    q = Queue()
    q.enQueue(1)
    q.enQueue(2)
    print(q.deQueue())
	print(q.deQueue())


执行结果:
1
2

三、栈

  1. 算法原理:
    栈是一种只能在一端进入插入和删除操作数据结构。先进后出原则。
    栈中基本属性:
    top:栈顶元素
    栈实例方法:
    push(n):入栈
    pop():出栈
    peek():获取栈顶元素
  2. 代码:
class Node(object):
    def __init__(self, value=None, next=None):
        self.value = value
        self.next =next


class stack(object):
    def __init__(self):
        self.top = None

    # 入栈
    def push(self, n):
        node = Node(n)
        node.next = self.top
        self.top = node
        return node.value

    # 出栈
    def pop(self):
        if self.top is None:
            return None
        temp = self.top.value
        self.top = self.top.next
        return temp

    # 获取栈顶元素
    def peek(self):
        if self.top is None:
            return None
        else:
            return self.top.value


if __name__ == '__main__':
    s = stack()
    s.push(1)
    s.push(2)
    s.push(3)
    print(s.pop())
	print(s.peek())

执行结果:
3
2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值