Data structure Python implementation

Source: Internet
Author: User

Reference blog: Talking about algorithms and data structures: a stack and queue Python data structure--stack, queue implementation (a) Python data structure--stack, queue implementation (ii) Python data structure--the implementation of the linked list

Data

Definition: Simply put, a data structure is the way in which a design is organized and stored in a computer. For example: Lists, collections, and dictionaries are data structures.

PS: "program = data structure + algorithm"

Lists: called "arrays" in other programming languages, is a basic type of data structure.
About: List of storage issues!

Stack:
A stack is a collection of data that can be understood as a list of inserts or deletions that can only be done at one end.
Features of the stack: LIFO (last-in, first-out)
The concept of stacks:
Top of Stack
Bottom of Stack
Basic operation of the stack:
Input stack (stack): push

Out stack: Pop
Take the top of the stack: gettop

Simple implementation of stack operations with Python

Class Stack (object):    def __init__ (self):        self.stack=[]    def isEmpty (self):        return self.stack==[]    def push (Self,item):        self.stack.append (item)    def pop (self):        if Self.isempty ():            raise Indexerror, ' Pop from empty stack '        return Self.stack.pop ()    def peek (self):        return self.stack[-1]    def size (self):        return len (self.stack)

Application of stack--bracket matching problem
Parentheses match the question: give a string that contains parentheses, brackets, curly braces, to find out if the parentheses in the string match.
For example:
() () []{} match
([{()}]) match
[] (Does not match
[(]) mismatch

def kuohaopipei (exp= "{[)]}"):    stack = []    for I in Exp:        if I in {' (', ' [', ' {'}:            stack.append (i)        if i = = ') ':            If Len (stack) >0 and stack[-1] = = ' (':                stack.pop ()            else:                return False        if i = = '] ':            if L En (Stack) >0 and stack[-1] = = ' [':                stack.pop ()            else:                return False        if i = = '} ':            If Len (stack) >0 and stack[-1] = = ' {':                stack.pop ()            else:                return False    If Len (stack) ==0:        return True    else:        return Falseprint (Kuohaopipei ("{() () () [(([]{})]}")

Queue
A queue is a collection of data that is only allowed to be inserted at one end of the list and deleted at the other end.
The one end of the insertion is called the tail of the queue (rear), and the Insert action is called the incoming or the queue
The one end of the deletion is called the team header (front), and the delete action is called the queue
Nature of the queue: FIFO (first-in, first-out)

Two-way queue: Both sides of the queue allow incoming and outbound operations.

Queue implementations:
How to use: From collections import deque
Create queue: Queue = Deque (LI)
Incoming team: Append
Out team: Popleft
Two-way queue team first team: Appendleft
Two-way Queue Team Tail Team: Pop

How the queue is implemented
Normal queue:
Initial assumptions: List + two subscript pointers
Create a list and two variables, the front variable points to the head of the team, and the rear variable points to the tail. Initially, both the front and the rear were 0.
Enter the operation: the element writes to Li[rear] position, rear increment 1.
Out of action: Returns the element of Li[front], front 1.

Ring queue:
Improved scenario: Logically connect the list to the end.
Ring queue: When the tail pointer front = = Maxsize + 1 o'clock, move forward one position automatically to 0.
Implementation method: Calculating remainder
Team first pointer forward 1:front = (front + 1)% MaxSize
Team Tail hand forward 1:rear = (rear + 1)% MaxSize
Team NULL Condition: rear = = Front
Team Full Condition: (rear + 1)% MaxSize = = Front

Data structure Python implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.