Python data structure
The structure in which the data is organized is called data structures that contain a list of tuples (immutable collections) (mutable sets) dictionary (key-value pairs), etc.
Tuples ("Apples", "oranges", "Bananas")
list ["Apple", "orange", "banana")
Dictionary {"1": "Apple", "2": "Orange", "3": "Banana"}
stack of data structures
The stack is a data structure, the first stack equivalent to one end of the closed container, data A can be stored in the stack, the data a moved to the stack
Process is called into the stack, also known as the stack, into the stack, the data A into the stack, then to the top of the stack, while occupying a position in the stack, when entering a data b
, that is, when a data is put into the stack, the new data will occupy the top of the stack, the original data is pushed to the top of the stack by the new data
's next position. The stack can only manipulate the data at the top of the stack, so the original data cannot be manipulated at this time, and only the new data can be manipulated.
Can be out of the stack or delete, etc., and so on data b after the stack, you can operate on a
Python's stack case
Stack.py's Program
classStack ():def __init__(self,size):#equivalent to the Java constructor, self is a Python class must pass valueself.stack=[]; Self.size=size; Self.top=-1; defpush (self,content):ifSelf . Full ():Print("Stack is full") Else: Self.stack.append (content) Self.top=st.top+1defpop (self):ifSelf . Empty ():Print("Empty") Else: Self.stack.pop (); Self.top=self.top-1defFull (self):ifSelf.top==self.size-1: returnTrueElse: returnFalsedefEmpty (self):ifSelf.top==-1: returnTrueElse: returnFalse
Using stacks
S=stack (a); for in range (3): s.push (i); S.pop () print s.empty ();
Queue
A queue is also a data structure, where the queue is equivalent to a container that is open at both ends, but only one end can delete operations
The insertion operation cannot be performed, and the other end can be inserted only and cannot be deleted, and this section of the insert operation is called the tail of the queue.
The end of the delete operation is called the team head. The data is from the end of the team into the first
Queue.py's Program
classQueue ():def __init__(self,size): Self.size=size; Self.front=-1; Self.rear=-1; Self.queue=[]; defEnqueue (Self,ele):#Queued Operation ifself.isfull ():Raiseexception"Queue is full"); Else: Self.queue.append (ele); Self.rear=self.rear+1; defDequeue (self):#out of team Operation ifself.isempty ():Raiseexception"queue is empty"); Else: Self.front=self.front+1; returnSelf.queue[self.front]; defIsfull (self):returnself.rear-self.front+1==self.size; defIsEmpty (self):returnSelf.front==self.rear;
Using queues
Q=queue (ten); for in range (3): q.enqueue (i); Print q.dequeue (); print q.isempty ();
Python data structure