数据结构与算法——编程实践

数据结构与算法课程团队,全力打造


1.2 顺序表python(25段代码)

<h1>顺序表的构建</h1> <pre><code class="language-python">class Seq: def __init__(self, size=20): &amp;quot;&amp;quot;&amp;quot; 初始化Seq类的实例 :param size: 序列的初始大小,默认为20 &amp;quot;&amp;quot;&amp;quot; self.size = size # 序列的大小 self.curNum = 0 # 当前已存储的元素数量 self.element = [None] * size # 初始化一个固定大小的列表,用于存储元素</code></pre> <h1>顺序表的操作</h1> <h1>辅助操作</h1> <h2>【格式化输出】</h2> <p>其中append方法,在后文会具体实现。</p> <p>简单的:</p> <pre><code class="language-python">class Seq:     def show(self):         for i in range(self.curNum):             print(self.element[i], end=&amp;#039; &amp;#039;)</code></pre> <p>高级的:</p> <pre><code class="language-python">class Seq:     def __str__(self):         s = &amp;#039;&amp;#039;         for i in range(self.curNum):             s += str(self.element[i]) + &amp;#039; &amp;#039;         return s</code></pre> <p>其中<strong>str</strong>是python对象的特殊方法,当调用print的时候,自动调用<strong>str</strong>的返回值。</p> <h2>【扩容】</h2> <p>当超出容量时,可以报错,也可以选择扩容(其中length方法在后文会实现):</p> <pre><code class="language-python">class Seq:     def grow(self, k=2):         self.size *= k         self.element += [None]*(self.size*(k-1))</code></pre> <p> </p> <h2>【判断是不是空】</h2> <pre><code class="language-python">class Seq:     def isempty(self):         return self.curNum == 0</code></pre> <p> </p> <h2>【判断是不是满】</h2> <pre><code class="language-python">class Seq:     def isfull(self):         return self.curNum == self.size</code></pre> <p> </p> <h1>增加元素</h1> <h2>【添加】</h2> <p>方式1:当容量满了,扩容</p> <pre><code class="language-python">class Seq:     def append(self, m):         if self.isfull():             self.grow()         self.element[self.curNum] = m         self.curNum += 1</code></pre> <p>方式2:当容量满了,不执行,方法返回1,表示操作失败,否则返回0</p> <pre><code class="language-python">class Seq:     def append(self, m):         if self.isfull():             return 1         self.element[self.curNum] = m         self.curNum += 1         return 0</code></pre> <p> </p> <h2>【插入】</h2> <p>方式1:当容量满了,扩容</p> <pre><code class="language-python">class Seq:     def insert(self, i, m):         if i &amp;lt; 0 or i &amp;gt; self.curNum:             return 1         if self.isfull():             print(…)             self.grow()         for j in range(self.curNum, i, -1):             self.element[j] = self.element[j-1]         self.element[i] = m         self.curNum += 1</code></pre> <p>方式2:当容量满了,不执行,方法返回1,表示操作失败,否则返回0</p> <pre><code class="language-python">class Seq:     def insert(self, i, m):         if i &amp;lt; 0 or i &amp;gt; self.curNum:             return 1         if self.isfull():             return 1         for j in range(self.curNum, i, -1):             self.element[j] = self.element[j-1]         self.element[i] = m         self.curNum += 1         return 0 </code></pre> <p> </p> <h2>【拼接】</h2> <p>其中append在前文已经实现:</p> <p>方式1:拼接时,通过grow方式增加容量</p> <pre><code class="language-python">class Seq:     def extend(self, other):         while (self.curNum+other.curNum) &amp;gt; self.size:             self.grow()         for i in range(other.curNum):             self.append(other.element[i]) </code></pre> <p>方式2:拼接时,直接合并两者容量:</p> <pre><code class="language-python">class Seq:     def extend(self, other):         self.size += other.size         self.element += [None]*other.size         for i in range(other.curNum):             self.append(other.element[i]) </code></pre> <p> </p> <h1>删除元素</h1> <h2>【弹出一个元素】</h2> <pre><code class="language-python">class Seq:     def pop(self):         if self.isempty():             return 1         res = self.element[self.curNum-1]         self.element[self.curNum - 1] = None         self.curNum -= 1         return res</code></pre> <p> </p> <h2>【删除第i个】</h2> <pre><code class="language-python">class Seq:     def popi(self, i):         if i &amp;lt; 0 or i &amp;gt;= self.curNum:             return 1         for j in range(i, self.curNum-1):             self.element[j] = self.element[j+1]         self.element[self.curNum-1] = None         self.curNum -= 1</code></pre> <p> </p> <h2>【删除n这个值】</h2> <p>其中,popi在前文已经实现</p> <pre><code class="language-python">class Seq:     def remove(self, n):         for i in range(self.curNum-1, -1, -1):             if self.element[i] == n:                 self.popi(i) </code></pre> <p> </p> <h2>【清空】</h2> <pre><code class="language-python">class Seq:     def clear(self):         self.curNum = 0         self.element = [None] * self.size</code></pre> <p> </p> <h2>【删除一段】</h2> <pre><code class="language-python">class Seq:     def cut(self, start=0, end=None):         # 不用判断错误,因为在popi中已经处理         if end is None:             end = self.curNum         for k in range(end-1, start-1, -1):             self.popi(k)</code></pre> <p> </p> <h1>修改</h1> <h2>【修改第i个值】</h2> <pre><code class="language-python">class Seq:     def set(self, i, x):         if i &amp;lt; 0 or i &amp;gt;= self.curNum:             return 1         self.element[i] = x</code></pre> <p> </p> <h2>【替换x为y】</h2> <pre><code class="language-python">class Seq:     def replace(self, x, y):         for i in range(self.curNum):             if self.element[i] == x:                 self.element[i] = y </code></pre> <p> </p> <h2>【翻转】</h2> <pre><code class="language-python">class Seq:     def reverse(self):         for i in range(self.curNum//2):             j = self.curNum - 1 - i             self.element[i], self.element[j] = self.element[j], self.element[i]  </code></pre> <h1>查询</h1> <h2>【获取长度】</h2> <pre><code class="language-python">class Seq:     def length(self):         return self.curNum</code></pre> <h2>【获取最大值/最小值】</h2> <pre><code class="language-python">class Seq:     def max(self):         if self.isempty():             return None         res = self.element[0]         for i in range(1, self.curNum):             if self.element[i] &amp;gt; res:                 res = self.element[i]         return res</code></pre> <p>最小值略。</p> <p> </p> <h2>【判断某个值在不在里面】</h2> <pre><code class="language-python">class Seq:     def has(self, k):         for i in range(self.curNum):             if self.element[i] == k:                 return True         return False</code></pre> <p> </p> <h2>【获取某个位置的值】</h2> <pre><code class="language-python">class Seq:     def get(self, i):         if i &amp;lt; 0 or i &amp;gt;= self.curNum:             return 1         return self.element[i]</code></pre> <p> </p> <h2>【获取某个值的位置】</h2> <pre><code class="language-python">class Seq:     def index(self, x):         for i in range(self.curNum):             if self.element[i] == x:                 return i         return -1 </code></pre>

页面列表

ITEM_HTML