Python Implementation of Data Structure Stack and Queue Operation Method

  • 2021-07-18 08:41:58
  • OfStack

Queue, stack and linked list are 1 kind, which is very basic in data structure. Similarly, they also have various variations and implementations of 5 flowers and 8 gates. But no matter how they change in form, queues and stacks have their most basic characteristics that remain unchanged. Let's look at queues and stacks from the most basic and simple implementation today.

No matter what form of queue, it always has a common feature of "first in, first out". How to understand it? It's like a supermarket queuing to check out. People who line up first line up at the front of the queue and check out first. This is the characteristic of the queue.

The stack, on the other hand, is the opposite of the queue. It is "first in, then out". How do you understand it? Basically, all editors have an undo function, that is, press Ctrl + Z. When you write a paragraph of text, the first time you press Ctrl+Z, the last text you wrote disappears, and the second time you press Ctrl+Z, the last text you wrote in the current editor disappears. This is an application example of stack structure.

Ok, after introducing the concepts, let's look at how the code 1 implements these two data structures. In this article, we use the simplest way-through the native data type list of Python. In the last article, we introduced the linked list. Through the linked list, we can also realize the stack and queue. Interested friends may wish to try 1.

Queue

First, let's define a queue class:


class Queue():
 def __init__(self):
 self.__list = list()

Next, we add one method to the queue class:

Determine whether the queue is empty


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

Enter the team


def push(self, data):
 self.__list.append(data)

Get out of the team


 def pop(self):
 if self.isEmpty():
  return False
 return self.__list.pop(0)

· Defining len () function and print () operation class methods


 def __len__(self):
 return len(self.__list)
 def __str__(self):
 if self.isEmpty():
  return ''
 return ' '.join([str(x) for x in self.__list])

OK, here, 1 of the simplest queue to achieve, their own instantiation of a queue test 1 under it

Let's look at the stack

Stack

The implementation of stack is similar to that of queue, and there are also stack-in and stack-out operations. We directly go to the code:


class Stack():
 def __init__(self):
 self.__list = list()

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

 def push(self, data):
 self.__list.append(data)
 
 def pop(self):
 if self.isEmpty():
  return False
 return self.__list.pop()

 def __len__(self):
 return len(self.__list)

 def __str__(self):
 if self.isEmpty():
  return ''
 return ' '.join([str(x) for x in self.__list])

It can be seen that the class implementation of stack and queue is basically the same, except that when queuing and queuing, the queue pops up the first element, while the stack pops up the last element. This is also the most essential difference between queue and stack.

Summarize


Related articles: