Data structure notes: stack, queue

(1) Stack 1. Stack is a LIFO, FIFO data structure. 2. A s...

(1) Stack

1. Stack is a LIFO, FIFO data structure.

2. A stack is a linear table with limited operations that allows insertion and deletion of data only at one end.

3. Stack mainly contains two operations, stacking and stacking

4. Stacks can be implemented using arrays or chained lists.A stack implemented with an array is called a sequential stack, and a stack implemented with a chain table is called a chain stack.

For example:

There is now an empty bottle.

1. We put several apples in turn

2. When you take an apple from a bottle, the last apple put in will be taken out first, the first apple put in will be taken out last.

3. Apples can only be put or removed from the opening of the bottle.(Allow insertion and deletion of data only at one end)

Implement a stack with an array: (instead of a list here)

1 class ArrayStack(): 2 3 ITEMS = [] # List instead 4 COUNT = 0 # Number of elements in the stack 5 N = 10 # Stack size 6 7 def push(self ,item): 8 """ 9 Push 10 :param item: 11 :return: 12 """ 13 if len(self.ITEMS) >= self.N: return False # The stack is full 14 self.ITEMS.append(item) 15 self.COUNT += 1 16 return True 17 18 def pop(self): 19 """ 20 Stack Out 21 :return: 22 """ 23 if len(self.ITEMS) == 0: return False # Stack is empty 24 item = self.ITEMS.pop() 25 self.COUNT -= 1 26 return item

(2) Queues

1. Queues are a FIFO data structure.For example: the supermarket queues for payment, the top up-front payment finishes, and then goes out first.Later, only queue up, no queue jumping allowed.

2. The stack only supports two basic operations, push and pop.Queues also support only two basic operations, enqueue(), one data to the end of the queue, and dequeue(), one element from the head of the queue.

3. Queues are like stacks.It is also a linear table structure with limited operations.

4. Like stacks, they can also be implemented using arrays or chained lists.A queue implemented with an array is called a sequential queue, and a queue implemented with a chain table is called a chain queue.

Implement a queue with an array (instead of a list):

1 class ArrayQueue(): 2 3 ITEMS = [] # array 4 HEAD = 0 # Queue Head Index 5 TAIL = 0 # End of Queue Index 6 7 def __init__(self,n): 8 self.n = n # Array size 9 10 def enqueue(self,item): 11 """ 12 Entry 13 :param item: 14 :return: 15 """ 16 if self.TAIL == self.n: return False # End-of-queue index equals the size of the array, indicating that the queue is full 17 self.ITEMS.append(item) 18 self.TAIL += 1 19 return True 20 21 def dequeue(self): 22 """ 23 Queue 24 :return: 25 """ 26 if self.HEAD == self.TAIL: return False # Queue Head Index==End-of-queue index, indicating that the queue is empty 27 item = self.ITEMS[self.HEAD] 28 self.ITEMS[self.HEAD] = "X" # Identity has been deleted 29 self.HEAD += 1 30 return item

(3) Exercises

1,leetcode 20

1 class Solution: 2 def isValid(self, s: str) -> bool: 3 the_dict = {"(":")","{":"}","[":"]","k":"k"} 4 stack = ["k"] 5 for i in s: 6 if i in the_dict: stack.append(i) # Push left parenthesis on stack 7 elif the_dict[stack.pop()] != i: return False # Returns if the right parenthesis in a string is not equal to the expected right parenthesis false 8 return len(stack) == 1

16 May 2020, 12:27 | Views: 8732

Add new comment

For adding a comment, please log in
or create account

0 comments