Data Structure stack & queue

1. Stack

The abstract data structure of the
stack The stack is a linear table restricted to insert and delete at one end of the table. The end that allows insertion and deletion is called the top of the stack, and the fixed end of the other end is called the bottom of the stack. When there is no element in the table, it is called an empty stack.
The stack is ordered LIFO (last in, first out).
The stack operations are:

  • Stack() creates a new empty stack.
  • push(item) adds a new item to the top of the stack.
  • pop() deletes the top item on the stack and returns the value of the top item on the stack. The stack is modified.
  • peek() returns the top item of the stack. Do not modify the stack.
  • isEmpty() Tests whether the stack is empty, and returns the Bool value.
  • size() returns the stack length (the number of items in the stack).

The stack can be easily implemented through an array or a linked list

1.1 List implementation stack

class Stack:
    def __init__(self):
        self.items = []
    
    def isEmpty(self):
        return self.items == []
    
    def push(self, item):
        self.items.append(item)
    
    def pop(self):
        return self.items.pop()
    
    def peek(self):
        return self.items[-1]
    
    def size(self):
        return len(self.items)

if __name__ == '__main__':
    s = Stack()
    print(s.isEmpty())
    s.push(4)
    s.push('Rocket')
    print(s.peek())
    s.push(True)
    print(s.size())
    print(s.isEmpty())
    s.push(88
    print(s.pop())
    print(s.pop())
    print(s.size())    

1.2 Simple bracket matching

Python3 implementation:

# coding=utf-8
class Stack:
    def __init__(self):
        self.items = []

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

    def push(self, item):
        self.items.append(item)

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

    def peek(self):
        return self.items[-1]

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


def parChecker(symbol_string):
    s = Stack()
    balanced = True
    index = 0
    while index < len(symbol_string) and balanced:
        symbol = symbol_string[index]
        if symbol == '(':
            s.push(symbol)
        else:
            if s.isEmpty():
                balanced = False
            else:
                s.pop()
        index += 1

    if balanced and s.isEmpty():
        return True
    else:
        return False

print(parChecker('((()))'))
print(parChecker('((()'))
from stack import Stack

def check(strings):
    s = Stack()
    for string in strings:
        if string == '(':
            s.push(string)
        elif string == ')':
            try:
                s.pop()
            except IndexError:
                return False
        else:
            return False
    return s.isEmpty()


print(check('((()))'))
print(check('()))'))

2. Queue

The queue is a linear table that restricts the insertion operation of the node at one end, and the deletion operation of the node at the other end.

  • Basic idea
    • First in first out FIFO
    • operating:
      • Enqueue: add an item to the end of the queue
      • Dequeue: Remove an item from the front of the queue
      • size: returns the current number of items in the queue
      • peek: return the current top element of the queue instead of deleting it
    • Queue can be realized by (cyclic) array or linked list

2.1 Stack implementation queue:

# coding=utf-8
# 使用栈实现队列
class QueueWithTwoStack:
    def __init__(self):
        self.insertStack = []
        self.popStack = []

    def enqueue(self,e):
        self.insertStack.append(e)
        return e

    def dequeue(self):
        if len(self.insertStack)==0 and len(self.popStack)==0:
            return None
        if len(self.popStack) == 0:
            while len(self.insertStack)!=0:
                self.popStack.append(self.insertStack.pop())
        return self.popStack.pop()

mystack = QueueWithTwoStack()
e = mystack.enqueue(8)
print(e)
e = mystack.enqueue(9)
print(e)
e = mystack.enqueue(10)
print(e)
print(mystack.dequeue())
print(mystack.dequeue())
print(mystack.dequeue())

Guess you like

Origin blog.csdn.net/weixin_44127327/article/details/108248809