Python list Action Instance _python

Source: Internet
Author: User
Tags python list

The examples in this article describe how Python list operations are done. Share to everyone for your reference.

The implementation methods are as follows:

Copy Code code as follows:
Class Node:
"" "" "" "" "" "" "" "Structure

def __init__ (self, data):
"" "Node constructor" ""

Self._data = Data
Self._nextnode = None

def __str__ (self):
"" "" "" Node Data Representation "" "" "

Return str (self._data)

Class List:
"" "Linked List" "

def __init__ (self):
"" "List constructor" ""

Self._firstnode = None
Self._lastnode = None

def __str__ (self):
"" "" "List string Representation" ""

If Self.isempty ():
Return "Empty"

CurrentNode = Self._firstnode
output = []

While CurrentNode are not None:
Output.append (str (currentnode._data))
CurrentNode = Currentnode._nextnode

Return "". Join (output)

def insertatfront (self, value):
"" Insert node at front of list "" "

NewNode = Node (value)

If Self.isempty (): # List is empty
Self._firstnode = Self._lastnode = NewNode
Else: # List is not empty
Newnode._nextnode = Self._firstnode
Self._firstnode = NewNode

def insertatback (self, value):
"" Insert node at back of list "" "

NewNode = Node (value)

If Self.isempty (): # List is empty
Self._firstnode = Self._lastnode = NewNode
Else: # List is not empty
Self._lastnode._nextnode = NewNode
Self._lastnode = NewNode

def removefromfront (self):
"" "Delete node from front of list" "

If Self.isempty (): # Raise exception on empty list
Raise Indexerror, "Remove from empty list"

Tempnode = Self._firstnode

If Self._firstnode is self._lastnode: # one node in list
Self._firstnode = Self._lastnode = None
Else
Self._firstnode = Self._firstnode._nextnode

Return Tempnode

def removefromback (self):
"" "Delete node from back of list" "

If Self.isempty (): # Raise exception on empty list
Raise Indexerror, "Remove from empty list"

Tempnode = Self._lastnode

If Self._firstnode is self._lastnode: # one node in list
Self._firstnode = Self._lastnode = None
Else
CurrentNode = Self._firstnode

# Locate Second-to-last Node
While Currentnode._nextnode are not self._lastnode:
CurrentNode = Currentnode._nextnode

Currentnode._nextnode = None
Self._lastnode = CurrentNode

Return Tempnode

Def isempty (self):
"" "Returns true if List is empty" ""

Return Self._firstnode are None

I hope this article will help you with your Python programming.

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.