What & How & Why

差别

这里会显示出您选择的修订版和当前版本之间的差别。

到此差别页面的链接

两侧同时换到之前的修订记录前一修订版
上一修订版两侧同时换到之后的修订记录
dsa:notes:dahua_ds:3_linearlist [2018/09/24 15:30] – 外部编辑 127.0.0.1dsa:notes:dahua_ds:3_linearlist [2020/04/19 10:21] codinghare
行 52: 行 52:
   - 验证下标是否在有效范围以内。   - 验证下标是否在有效范围以内。
   - 如果在范围内,返回结果。   - 如果在范围内,返回结果。
-<code:cpp linenums:1>+<code cpp linenums:1>
 if (L.length == 0 || i < 1 || i > L.length) { if (L.length == 0 || i < 1 || i > L.length) {
     return -1;     return -1;
行 64: 行 64:
   - 从最后一个元素开始,往前遍历到第 ''i'' 个元素,把这些元素分别往后移动一位。   - 从最后一个元素开始,往前遍历到第 ''i'' 个元素,把这些元素分别往后移动一位。
   - 在 ''i'' 的位置插入新元素。   - 在 ''i'' 的位置插入新元素。
-<code:cpp linenums:1>+<code cpp linenums:1>
 if(L.length == MAXSIZE) //check if list is full if(L.length == MAXSIZE) //check if list is full
     return -1;     return -1;
行 84: 行 84:
   * 从被删除的元素开始遍历到表尾,分别把这区域的元素都往前移动一位。   * 从被删除的元素开始遍历到表尾,分别把这区域的元素都往前移动一位。
   * 表长减去1。   * 表长减去1。
-<code:cpp linenums:1>+<code cpp linenums:1>
 if (i < 1 || i > L.length) //check if element is in range if (i < 1 || i > L.length) //check if element is in range
     return -1;     return -1;
行 122: 行 122:
 ===链表的数据=== ===链表的数据===
 根据上面的描述,我们需要先定义链表的 Data 模型:具有数据部分和指针部分。我们可以用结构体 / 类来定义这个存储结构: 根据上面的描述,我们需要先定义链表的 Data 模型:具有数据部分和指针部分。我们可以用结构体 / 类来定义这个存储结构:
-<code:cpp linenums:1>+<code cpp linenums:1>
 struct Node { struct Node {
     int data;      int data; 
行 138: 行 138:
   * 如果链表为空,建立初始节点(头节点)   * 如果链表为空,建立初始节点(头节点)
   * 如果链表非空,建立新节点,并用前一个节点指向新节点。   * 如果链表非空,建立新节点,并用前一个节点指向新节点。
-<code:cpp linenums:1>+<code cpp linenums:1>
 Linklist p, r; Linklist p, r;
 *L = (LinkList)malloc( sizeof(Node)); //apply memory for LinkList *L = (LinkList)malloc( sizeof(Node)); //apply memory for LinkList
行 157: 行 157:
   - 如果到了链表的尾部, ''p''的数据部分为空,那证明没有找到第 ''i'' 个元素。   - 如果到了链表的尾部, ''p''的数据部分为空,那证明没有找到第 ''i'' 个元素。
   - 否则的话,''i'' 的元素应该存到了 ''p'' 里面,这时我们返回 ''p''的值就可以。   - 否则的话,''i'' 的元素应该存到了 ''p'' 里面,这时我们返回 ''p''的值就可以。
-<code:cpp linenums>+<code cpp linenums>
 LinkList p; LinkList p;
 p = (*L).next(); p = (*L).next();
行 181: 行 181:
   - 再将前驱结点指向新节点。   - 再将前驱结点指向新节点。
 用代码可以表示为: 用代码可以表示为:
-<code:cpp linenums:1>+<code cpp linenums:1>
 new -> next = front -> next; // copy the pointer info from front node to new node, use the info point to the rear node new -> next = front -> next; // copy the pointer info from front node to new node, use the info point to the rear node
 front -> next = new; // then let front point to the new node front -> next = new; // then let front point to the new node
行 204: 行 204:
 \\ \\
 用代码可以表示为: 用代码可以表示为:
-<code:cpp linenums:1>+<code cpp linenums:1>
 front -> next = new -> next; front -> next = new -> next;
 </code> </code>