"Data Structure and Algorithm (Python Language Description)" Chapter 3 LCList Table Partial Function Implementation

Implement several functions given in the book, so that you can easily find your own mistakes by comparing the books


The book implements the following functions :

prepend function: create a new node and insert it into the head end.
append function: create a new node and insert it at the end.
pop function: the front end pops up.
printall function: output table elements.

First build ADT模型, write out the functions to be implemented and the data roughly used, and organize the ideas . The following are the considerationsADT模型 before building : Do you need to derive from the linked list class we have implemented ? It can be found that only the tail is used repeatedly . Pointers , counters , and other methods need to be overridden, so there is no need to derive from other linked list classes.
LCList类
self._rearself._len

The following is used XMindto do something like ADT模型:
LCList


Next, we will analyze the functions I wrote by myself in the order implemented in the book (skip the functions that are clear at a glance).

I have achieved prepend功能:

def prepend(self, elem):
    if self._rear is None: #表是空滴
        p = LNode(elem) # 建立新结点
        self._rear = p # 更新尾结点
        p.next = p # 将结点next域指向寄己
        self._len += 1
    else:
        p = self._rear # p 为尾结点
        p.next = LNode(elem, p.next)
        self._len += 1

Implemented in the book prepend功能:

def prepend(self, elem):
    p = LNode(elem) # 将重复建立结点的语句抽出来
    if self._rear is None:
        p.next = p # 建立一个结点的环,结点的next域指向寄己
        self._rear = p
        self._len += 1 # 计数器,__init__中创建
    else:
        p.next = self._rear.next #新建结点的next域,指向原来的首端结点
        self._rear.next = p
        self._len += 1

What I achieved append功能:

def append(self, elem):
    if self._rear is None:
        self.prepend(elem) # 这里这么写,是因为空表添加的代码与prepend方法空表添加时,是一样的
    else:
        p = self._rear
        p.next = LNode(elem, p.next)
        self._rear = p.next # 更新尾结点,哪个是尾结点,self._rear说了算
        self._len += 1

High energy ahead! ! !

Implemented in the book append功能:

def append(self, elem):
    self.prepend(elem)
    self._rear = self._rear.next

Is it very concise? I thought about writing it like this, but I was a little worried that it might not be so intuitive for others to see; now it seems that I just need to simulate the whole process, and I can understand it completely.
Here's a look at 91页the instructions in the book:

Since the nodes in the circular linked list are connected into a circle, which node is the head or tail of the table is mainly a conceptual issue, which cannot be distinguished from the internal form of the table .

The only thing that identifies the tail node in the table is the tail pointer self._rear, and whether the node is inserted into the head or tail, the process is exactly the same, the difference is only in self._rearthe pointing, who it refers to is the tail, so it is completely possible to use Insert the node part to prepend方法do, append方法just modify self._rearthe pointer.

Skip pop功能, it is relatively simple, of course, what I realized is more wordy than the book.
What I achieved printall功能:

def printall(self):
    if self._rear is None:
        return None
    p = self._rear.next # 首端结点赋值给变量P
    end = self._rear # end为尾端结点
    while p is not end: # 当p 是尾端结点时,结束循环
        print(p.elem, end="")
        p = p.next
    print(p.elem)

It's rather wordy, and if someone else reads such code, it may take some time to straighten it out.
Implemented in the book printall功能:

def printall(self):
    if self.is_empty():
        return
    p = self._rear.next # 首结点
    while True:
        print(p.elem)
        if p is self._rear: # 如果 p 是最后的结点
            break # break直接结束循环
        p = p.next

It is also possible to set p as the tail node, just change the judgment condition.

Summarize

Let's take a look at the thinking here:

Object-oriented programming is encapsulated : implementation and invocation are separated, no matter how the internal implementation is implemented, as long as the function is satisfied, the interface for invocation can be provided.


So, that's it, welcome corrections and questions, thank you!
Marxism is well-behaved

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326770161&siteId=291194637