Python implements basic linear data structures

Source: Internet
Author: User
Array

Design of arrays

The design of an array is initially dependent on memory allocations, so the space must be pre-requested before use. This allows the array to have the following characteristics:

1, the request space after the size fixed, can no longer change (data overflow problem);

2, there is space continuity in memory performance, in the middle there will be no other programs need to call the data, for this array of dedicated memory space;

3, in the old-fashioned programming language (if there is a middle-order language called C), the program does not make the lower bounds of the operation of the array, there is a potential risk of cross-border operations (such as the data will be written in the running program to call the core part of the memory).

Because simple arrays are strongly dependent on computer hardware, they are not suitable for modern programming. To use variable-size, hardware-independent data types, Java and other programming languages provide more advanced data structures: ArrayList, vectors, and other dynamic arrays.

An array of Python

Strictly speaking, Python does not have a strict array of meanings.

List can be said to be a Python array, the following code is the implementation of the CPython list structure:

typedef struct {pyobject_var_head/* Vector of pointers to list elements. List[0] is ob_item[0], etc. */Pyobject **ob_it em;  /* Ob_item contains space for ' allocated ' elements. The number  * currently is ob_size.  * Invariants:  *  0 <= ob_size <= allocated  *  len (list) = = Ob_size  *  Ob_item = NULL implies Ob_size = = Allocated = 0  * List.sort () temporarily sets allocated to-1 to detect mutations.  *  Items must normally not being NULL, except during construction when * The list was not  yet visible outside the F Unction that builds it.  */py_ssize_t allocated;} Pylistobject;

Of course, in Python it's an array.
Some of the subsequent structures will also be implemented using list.

Stack

What is a stack

Stacks (English: Stack), also directly called Stacks, is a special kind of data structure in computer science, which is unique in that it can only be allowed at one end of a linked string or array (called the stacked top indicator, English: Top) to add data (English: Push) and output data (English: POP) operations. Alternatively, the stack can be done in the form of a one-dimensional array or a link string. Another relative operation of a stack is called a queue.

Since the stacking data structure only allows operation at one end, the principle of LIFO (LIFO, last in first out) operates.

Characteristics

1, first into and out, after in first out.

2, in addition to the top and tail nodes, each element has a precursor, a successor.

Operation

From the theory, the stack (stack) can be done by:

1. Top (): Gets the top object of the stack

2. Push (): Add an object to the stack

3. Pop (): Launch an object from the stack

Realize

Class My_stack (object): Def __init__ (self, value):  self.value = value  # precursor  self.before = None  # successor  Self.behind = None  def __str__ (self):  return str (self.value)  def top (Stack): If Isinstance (Stack, my_stack ):  If Stack.behind is not None:   return Top (Stack.behind)  else:   return stack  def push (Stack, ele ): Push_ele = My_stack (ele) if isinstance (Stack, my_stack):  stack_top = Top (stack)  Push_ele.before = Stack_top  Push_ele.before.behind = Push_ele else:  raise Exception (' Don't throw things in okay ')  def pop (stack): If Isinstance ( Stack, my_stack):  stack_top = Top (Stack)  if Stack_top.before is not none:   Stack_top.before.behind = None   stack_top.behind = None   return stack_top  else:   print (' Already top of stack ')

Queue

What is a queue

Similar to the stack, the only difference is that the queue can only be performed on the team head, so the queue is a linear table of first in, out (FIFO, first-in-first-out)

Characteristics

1. First-in, first-out, after-entry

2, except the tail node, each node has a successor

3, (optional) In addition to the head node, each node has a precursor

Operation

1. Push (): queue

2, Pop (): Out of the team

Realize

Normal queue

Class Myqueue (): Def __init__ (self, value=none):  self.value = value  # precursor  # self.before = None  # successor  SE Lf.behind = None  def __str__ (self):  if self.value are not None:   return str (self.value)  else:   return ' None '  def create_queue (): "" "Only Team Head" "" Return Myqueue ()  def last (queue): If Isinstance (Queue, myqueue):  If Queue.behind is isn't None:   return Last (Queue.behind)  else:   return queue  def push (queue, ele): if Isinstance (Queue, myqueue):  last_queue = Last (queue)  New_queue = Myqueue (ele)  last_queue.behind = New_ Queue  def pop (queue): If Queue.behind is not None:  get_queue = queue.behind  Queue.behind = Queue.behind.behind  return get_queue else:  print (' There are no elements in the queue ') def print_queue (queue): print (queue) if Queue.behind is not None:  print_queue (Queue.behind)

Linked list

What is a linked list

A linked list (Linked list) is a common basic data structure, a linear table, but does not store data in a linear order, but rather as a pointer to the next node (Pointer) in each node. Since they do not have to be stored sequentially, the list can achieve an O (1) complexity at the time of insertion, much faster than another linear table-order table, but the time to find a node or to access a particular number of nodes requires O (n), while the corresponding time complexity of the sequential table is O (logn) and O (1) respectively.

Characteristics

Using the list structure can overcome the shortcoming that the array list needs to know the data size beforehand, the list structure can make full use of the computer memory space and realize the flexible memory dynamic management. However, the list loses the advantage of random array reading, and the spatial overhead of the linked list is larger due to the increase of the point-of-view pointer domain.

Operation

1. Init (): Initialize

2, insert (): Insert

3, Trave (): Traverse

4. Delete (): delete

5. Find (): Find

Realize

Only two-way lists are implemented here

Class LinkedList (): Def __init__ (self, value=none):  self.value = value  # precursor  self.before = None  # successor  Self.behind = None  def __str__ (self):  if self.value are not None:   return str (self.value)  else:   Return ' None '  def init (): Return LinkedList (' HEAD ')  def Delete (linked_list): If Isinstance (Linked_list, LinkedList):  If Linked_list.behind is not none:   Delete (linked_list.behind)   Linked_list.behind = None   Linked_list.before = none  Linked_list.value = None

Summarize

The above is the use of Python to achieve the basic linear data structure of the entire content, I hope this article for you to learn Python can help. If in doubt you can leave a message to discuss.

Related Article

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.