用python实现栈/队列/双端队列/链表

栈是元素的有序集合,添加操作与移除操作都发生在其顶端,先进后出
栈操作:创建空栈,增删(顶端),查(顶端元素,元素个数,是否为空)
应用:将十进制数转换成任意进制数
 1 class Stack:
 2     # 用列表创建一个空栈
 3     def __init__(self):
 4         self.items = []
 5 
 6     # 将一个元素添加到栈的顶端
 7     def push(self, item):
 8         self.item.append(item)
 9 
10     # 将栈顶端的元素移除
11     def pop(self):
12         return self.items.pop()
13 
14     # 返回栈顶端的元素
15     def peek(self):
16         return self.items[len(self.items) - 1]
17 
18     # 返回栈中元素的个数
19     def size(self):
20         return len(self.items)
21 
22     # 判断栈是否为空,是返回True,否则返回False
23     def is_empty(self):
24         return self.items == []

队列是元素的有序集合,添加操作发生在其尾部,移除操作发生在头部,先进先出
队列操作:创建空队列,增(底端),删(顶端),查(元素个数, 是否为空,)
应用:模拟打印任务,知晓打印机的打印速度,打印任务个数随机产生-> 学生平均需要等待多久才能拿到打印好的文章
class Queue:
    def __init__(self):
        self.items = []
    # 在队列底端插入元素
    def enqueue(self, item):
        self.items.insert(0, item)
    
    # 在队列顶端删除元素
    def dequeue(self):
        return self.items.pop()

    def size(self):
        return len(self.items)

    def isEmpty(self):
        return self.items == []

 双端队列是元素的有序集合,其任何一端都允许添加和移除元素

 双端队列操作:创建一个空双端队列,增(前,后),删(前,后),查(元素个数,是否为空)

 应用:回文检测器

class Deque:
    def __init__(self):
        self.items = []

    # 前端加入元素
    def add_front(self, item):
        self.items.append(item)

    # 后端加入元素
    def add_rear(self, item):
        self.items.insert(0, item)

    def remove_front(self):
        return self.items.pop()

    def remove_rear(self):
        return self.items.pop(0)

    def size(self):
        return len(self.items)

    def is_empty(self):
        return self.items == []

无序列表是元素的集合,其中每一个元素都有一个相对于其他元素的位置,无序列表需要维持元素之间的相对位置,但是并不需要在连续的内存空间中维护这些位置信息
无序列表操作:创建一个空列表,增(头部添加,末尾添加,在确定位置添加),删(移除某元素,移除末尾元素,移除指定位置元素),
查(元素个数,搜索某元素,查询是否为空,查询某个元素的下标)
单链表:必须指明第一个元素的位置,最后一个元素需要知道自己没有下一个元素
节点是构建链表的基本数据结构
节点组成:元素(数据变量) + 指向下一个节点的引用(也是一个对象), 尾节点只有元素,引用为None
节点操作:访问(元素,指向下一个节点的引用),修改(元素,指向下一个节点的引用)
class Node:
    def __init__(self, init_data):
        self.data = init_data
        self.next = None


class UnorderedList():
    def __init__(self):
        self.head = None  # 头节点,用于存储节点对象

    def add(self, item):
        """链表头部添加元素"""
        node = Node(item)  # 创建一个新节点
        node.next = self.head  # 将新节点引用从None改为指向待添加列表中的第一个节点
        self.head = node  # 新创建的节点自动成为新列表的第一个节点,因此将头节点,指向新创建的节点

    def append(self, item):
        """链表尾部添加元素"""
        node = Node(item)
        if self.head is None:
            self.head = node
        else:
            current = self.head  # 获得第一个节点对象
            while current.next is not None:  # 判断节点对象的引用属性是否为None
                current = current.next  # 获得下一个节点对象,用列表中的列表理解
            current.next = node

    def insert(self, pos, item):
        """指定位置添加元素"""
        if pos <= 0:
            self.add(item)
        elif pos > self.length() - 1:
            self.append(item)
        else:
            current = self.head
            count = 0
            while count < pos - 1:
                count += 1
                current = current.next
            # 循环退出后,pre指向pos-1的位置
            node = Node(item)
            node.next = current.next
            current.next = node

    def remove(self, item):
        """删除指定元素"""
        current = self.head
        previous = None
        while True:
            if current.data == item:
                break
            else:
                previous = current
                current = current.next
        if previous is None:
            self.head = current.next
        else:
            previous.next = current.next

    def pop(self, pos=None):
        """删除指定位置的元素,不传参数则默认删除末尾元素"""
        current = self.head
        previous = None
        if pos is None:
            while True:
                if current.next is None:
                    break
                else:
                    previous = current
                    current = current.next
            if previous is None:
                self.head = None
            else:
                previous.next = current.next
        elif pos == 0:
            self.head = current.next
        else:
            for i in range(pos):
                previous = current
                current = current.next
            previous.next = current.next
        return current.data

    def length(self):
        """获取链表元素个数,即节点个数"""
        current = self.head
        count = 0
        while current is not None:
            count += 1
            current = current.next
        return count

    def search(self, item):
        """查询某位元素是否存在"""
        current = self.head
        while current is not None:
            if current.data == item:
                return True
            else:
                current = current.next
        return False

    def is_empty(self):
        """查询链表是否为空"""
        return self.head is None

    def index(self, item):
        """查询某元素的下标"""
        pos = 0
        current = self.head
        while True:
            if current.data is item:
                break
            else:
                current = current.next
                pos += 1
        return pos

有序列表:通常以升序或降序排列
有序列表操作:创建一个空有序列表,增(添加某元素,并保持整个列表顺序),删(移除某元素,移除末尾元素,移除指定位置元素)
查(某元素是否存在,列表是否为空,元素的个数,元素的下标)
class Node:
    def __init__(self, init_data):
        self.data = init_data
        self.next = None


class OrderedList():
    def __init__(self):
        self.head = None  # 头节点,用于存储节点对象

    def add(self, item):
        """添加某元素,并保持整个列表顺序"""
        current = self.head
        previous = None
        stop = False
        while current is not None and not stop:
            if current.data > item:
                stop = True
            else:
                previous = current
                current = current.next
        node = Node(item)  # 创建一个新节点
        if previous is None:
            node.next = self.head
            self.head = node
        else:
            node.next = current
            previous.next = node

    def remove(self, item):
        """删除指定元素"""
        current = self.head
        previous = None
        while True:
            if current.data == item:
                break
            else:
                previous = current
                current = current.next
        if previous is None:
            self.head = current.next
        else:
            previous.next = current.next

    def pop(self, pos=None):
        """ 删除指定位置的元素,不传参数则默认删除末尾元素 """
        current = self.head
        previous = None
        if pos is None:
            while True:
                if current.next is None:
                    break
                else:
                    previous = current
                    current = current.next
            if previous is None:
                self.head = None
            else:
                previous.next = current.next
        elif pos == 0:
            self.head = current.next
        else:
            for i in range(pos):
                previous = current
                current = current.next
            previous.next = current.next
        return current.data

    def length(self):
        """获取链表元素个数,即节点个数"""
        current = self.head
        count = 0
        while current is not None:
            count += 1
            current = current.next
        return count

    def search(self, item):
        """查询某位元素是否存在"""
        current = self.head
        found = False
        stop =False
        while current is not None and not found and not stop:
            if current.data == item:
                found = True
            else:
                if current.data > item:
                    stop = True
                else:
                    current = current.next
        return found

    def is_empty(self):
        """查询链表是否为空"""
        return self.head is None

    def index(self, item):
        """查询某元素的下标"""
        pos = 0
        current = self.head
        while True:
            if current.data is item:
                break
            else:
                current = current.next
                pos += 1
        return pos

 

 
 

posted on 2020-05-22 14:02  东何  阅读(430)  评论(0编辑  收藏  举报

导航