Python single-chain table simple implementation method, python Single-Chain implementation

Source: Internet
Author: User

Python single-chain table simple implementation method, python Single-Chain implementation

The example in this article describes the simple implementation of a Python single-chain table and shares it with you for your reference. The specific method is as follows:

Generally, to define a single-chain table, first define the linked list Element: Element. It contains three fields:

List: indicates which list you belong.
Datum: change the value of an element.
Next: Location of the next node

The specific implementation code is as follows:

class LinkedList(object):    class Element(object):        def __init__(self,list,datum,next):       self._list = list      self._datum = datum       self._next = next    def getDatum(self):       return self._datum    datum = property(      fget = lambda self: self.getDatum())    def getNext(self):      return self._next    next = property(      fget = lambda self: self.getNext())  def __init__(self):    self._head = None    self._tail = None  def getHead(self):    return self._head   head = property(    fget = lambda self: self.getHead())   def prepend(self,item):    tmp = self.Element (self,item,self._head)    if self._head is None:      self._tail = tmp     self._head = tmp   def insert(self, pos, item):    i = 0    p = self._head    while p != None and i < pos -1:      p = p._next      i += 1    if p == None or i > pos-1:      return -1    tmp = self.Element(self, item, p._next)    p._next = tmp    return 1  def getItem(self, pos):    i = 0    p = self._head    while p != None and i < pos -1:      p = p._next      i += 1    if p == None or i > post-1:      return -1    return p._datum  def delete(self, pos):    i = 0    p = self._head    while p != None and i < pos -1:      p = p._next      i += 1    if p == None or i > post-1:      return -1    q = p._next    p._nex = q._next    datum = p._datum    return datum  def setItem(self, pos, item):    i = 0    p = self._head    while p != None and i < pos -1:      p = p._next      i += 1    if p == None or i > post-1:      return -1    p._datum = item    return 1  def find(self, pos, item):    i = 0    p = self._head    while p != None and i < pos -1:      if p._datum == item:        return 1      p = p._next      i += 1    return -1  def empty(self):    if self._head == None:      return 1    return 0  def size(self):    i = 0    p = self._head    while p != None and i < pos -1:      p = p._next      i += 1    return i  def clear(self):    self._head = None    self._tail = Nonetest = LinkedList()test.prepend('test0')print test.insert(1, 'test')print test.head.datumprint test.head.next.datum

I hope this article will help you with Python programming.


How does python implement a stack using a one-way cyclic linked list?

Node is okay, that is, the variable is defined with an underscore instead of two.
Stack has some problems,
(I don't know why you need to create a circular linked list here, But no matter)
First, you must define a head and a tail so that the tail and head can be connected to form a loop.
Set head and tail to None to indicate that the Stack is empty.
If push is used, you like to write it like this. You prefer to use the Node pushed in as the new head, and then modify self. _ head is the new Node, and the next of the new Node is the old head. Connect the Tail and Head to save some loops.
If pop is used, add some judgments. First, check whether the Head is None. If yes, the Stack is empty. If we find that both the Tail and Head are the same, it indicates that there is only one item in the Stack. After extracting the Head, set the Stack to null. Then extract the Head, read the Node next to the Head, and set it to the new Head. Then, connect the Tail and Head nodes.
The code is attached for reference.
Class Node: def _ init _ (self, newData): self. _ data = newData self. _ next = None def getData (self): return self. _ data def getNext (self): return self. _ next def setData (self, newData): self. _ data = newData def setNext (self, newNode): self. _ next = newNodeclass Stack: def _ init _ (self): self. _ head = None self. _ tail = None def push (self, data): print 'push', data, 'into stack' new = Node (data) cur = self. _ head end = self. _ tail if cur is None: self. _ head = new. setNext (new) self. _ tail = new else: new. setNext (self. _ head) self. _ head = new self. _ tail. setNext (new) def pop (self): if self. _ head is not None: cur = self. _ head print 'pop ', cur. getData (), 'Out of stack' if cur. getNext () is not cur: self. _ head = cur. getNext () self. _ tail. setNext (self. _ head) else :...... remaining full text>

How to use classes to implement a simple one-way linked list in c ++?

The problem is so messy!
1. getnext and setnext
The two functions cannot be understood anymore. The designer defines a package * pnext in the package class to point to the next element of the linked list. getnext is to obtain the next element and naturally returns this pointer, setnext is to specify the address of the next element for the current element. Naturally, it is to assign a value to pnext.

2. When the first package object is created and pnext is initialized to 0, when the second package object is created, how does the pnext In the first package object point to it (isn't it initialized to 0 ?)?
Read this Code:
Package * ppackage = new package (pBox );

If (pHead)
PTail-> setnext (ppackage );

First create the second package object, and then pTail-> setnext (ppackage );
Note that pTail always points to the last object of the linked list, so pTail-> setnext (ppackage); this means that the second object is placed in the pnext of the first object, the first object is linked to the second object.

3. If you assign the address ppackage of the package object you created to pnext, isn't pnext pointing to the object you just created? Doesn't it seem to point to the next package object?
As mentioned above, setnext puts the newly created object into the pnext Member of the last element of the current linked list. Before that, the newly created object is not in the linked list, the last element of the linked list is the one created last time.

4. Finally, how can we set pnext to 0?
You have also mentioned that pnext is assigned 0 value when creating a package, so you do not need to assign 0 value to pnext of the last package.

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.