表指针实现。第二种方法是使用访问列表,模拟指针。

在我的理解中学习,它是创建一个节点数组,模拟存储装置,然后从中分配内存和释放内存。

但实际的内存没有被释放~

下面的代码直接附着:

//
// main.cpp
// CursorList
//
// Created by Alps on 14-7-27.
// Copyright (c) 2014年 chen. All rights reserved.
// #include <iostream> #define CursorSpace 100
#define ElementType int using namespace std; typedef int PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position; void InitializeCursorList(void);
List MakeEmpty(List L);
int isEmpty(List L);
int isLast(List L, Position P);
void Insert(List L, Position P, ElementType X);
void Delete(List L, ElementType X);
Position Find(List L, ElementType X);
Position FindPrevious(List L, ElementType X);
void DeleteList(List L); struct Node{
ElementType X;
Position Next;
}; struct Node CursorList[CursorSpace]; int isEmpty(List L){
return CursorList[L].Next == 0;
} int isLast(List L, Position P){
return CursorList[P].Next == 0;
} void InitializeCursorList(void){
int i = 0;
for (i = 0; i < CursorSpace; i++) {
CursorList[i].Next = i + 1;
}
CursorList[CursorSpace - 1].Next = 0;
} Position CursorAlloc(){
Position P;
P = CursorList[0].Next;
CursorList[0].Next = CursorList[P].Next;
CursorList[P].Next = 0;
return P;
} void CursorFree(Position P){
CursorList[P].Next = CursorList[0].Next;
CursorList[0].Next = P;
} Position Find(List L, ElementType X){
Position P = CursorList[L].Next;
while (CursorList[P].X != X && P) {
P = CursorList[P].Next;
}
if (P == 0) {
return false;
}
return P;
} Position FindPrevious(List L, ElementType X){
Position P = L;
Position tmp = CursorList[P].Next;
while (CursorList[tmp].X != X && tmp) {
tmp = CursorList[tmp].Next;
P = CursorList[P].Next;
}
return P;
} void Delete(List L, ElementType X){
Position P = FindPrevious(L, X);
Position tmp = CursorList[P].Next;
CursorList[P].Next = CursorList[tmp].Next;
} void Insert(List L, Position P, ElementType X){
Position tmp;
tmp = CursorAlloc();
CursorList[tmp].X = X;
CursorList[tmp].Next = CursorList[P].Next;
CursorList[P].Next = tmp;
} void DeleteList(List L){
Position P = CursorList[L].Next;
Position tmp = P;
while (tmp != 0) {
P = CursorList[P].Next;
CursorFree(tmp);
if (P == 0) {
break;
}
tmp = P;
}
CursorList[L].Next = 0;
} void Print(List L){
Position P = CursorList[L].Next;
while (P != 0) {
printf("%d ",CursorList[P].X);
P = CursorList[P].Next;
} printf("\n");
} int main(int argc, const char * argv[])
{ printf("start ...\n");
InitializeCursorList();
List L = CursorAlloc();
Insert(L, L, 1);
Insert(L, L, 3);
Insert(L, L, 5);
Insert(L, L, 4);
Print(L);
Position P = FindPrevious(L, 3);
printf("%d\n",P);
Delete(L, 3);
Print(L);
DeleteList(L);
Print(L);
return 0;
}

算法是没有问题。之后,我每一个功能考完试~

有任何疑问,请留言~

学习算法 - 表指针实现~ C++的更多相关文章

  1. 1、学习算法和刷题的框架思维——Go版

    前情提示:Go语言学习者.本文参考https://labuladong.gitee.io/algo,代码自己参考抒写,若有不妥之处,感谢指正 关于golang算法文章,为了便于下载和整理,都已开源放在 ...

  2. C++学习之this指针

    C++学习之this指针 一个对象的this指针并不是对象本身的一部分,不会影响sizeof(对象)的结果.this作用域是在类内部,当在类的非静态成员函数中访问类的非静态成员的时候,编译器会自动将对 ...

  3. 斯坦福大学公开课机器学习:advice for applying machine learning | learning curves (改进学习算法:高偏差和高方差与学习曲线的关系)

    绘制学习曲线非常有用,比如你想检查你的学习算法,运行是否正常.或者你希望改进算法的表现或效果.那么学习曲线就是一种很好的工具.学习曲线可以判断某一个学习算法,是偏差.方差问题,或是二者皆有. 为了绘制 ...

  4. CS229 Lesson 5 生成学习算法

    课程视频地址:http://open.163.com/special/opencourse/machinelearning.html 课程主页:http://cs229.stanford.edu/ 更 ...

  5. 强化学习实战 | 表格型Q-Learning玩井字棋(二)

    在 强化学习实战 | 表格型Q-Learning玩井字棋(一)中,我们构建了以Game() 和 Agent() 类为基础的框架,本篇我们要让agent不断对弈,维护Q表格,提升棋力.那么我们先来盘算一 ...

  6. 强化学习实战 | 表格型Q-Learning玩井子棋(三)优化,优化

    在 强化学习实战 | 表格型Q-Learning玩井字棋(二)开始训练!中,我们让agent"简陋地"训练了起来,经过了耗费时间的10万局游戏过后,却效果平平,尤其是初始状态的数值 ...

  7. [C#][算法] 用菜鸟的思维学习算法 -- 马桶排序、冒泡排序和快速排序

    用菜鸟的思维学习算法 -- 马桶排序.冒泡排序和快速排序 [博主]反骨仔 [来源]http://www.cnblogs.com/liqingwen/p/4994261.html  目录 马桶排序(令人 ...

  8. Stanford大学机器学习公开课(五):生成学习算法、高斯判别、朴素贝叶斯

    (一)生成学习算法 在线性回归和Logistic回归这种类型的学习算法中我们探讨的模型都是p(y|x;θ),即给定x的情况探讨y的条件概率分布.如二分类问题,不管是感知器算法还是逻辑回归算法,都是在解 ...

  9. 深度信任网络的快速学习算法(Hinton的论文)

    也没啥原创,就是在学习深度学习的过程中丰富一下我的博客,嘿嘿. 不喜勿喷! Hinton是深度学习方面的大牛,跟着大牛走一般不会错吧-- 来源:A fast learning algorithm fo ...

随机推荐

  1. [Angular] Using the Argon 2 Hashing Function In Our Sign Up Backend Service

    Which hash algorithom to choose for new application: https://www.owasp.org/index.php/Password_Storag ...

  2. UVA - 590Always on the run(递推)

    题目:UVA - 590Always on the run(递推) 题目大意:有一个小偷如今在计划着逃跑的路线,可是又想省机票费. 他刚開始在城市1,必须K天都在这N个城市里跑来跑去.最后一天达到城市 ...

  3. 【JAVASE】Java同一时候抛出多个异常

    Java有异常抛出后.跳出程序.一般无法运行接下来的代码. 大家做登陆功能.常常会实username和password的登陆校验,username或者password错误.假设通常是提示usernam ...

  4. Android Material风格的应用(三)--DrawerLayout

    添加抽屉导航 Android Material风格的应用(一)--AppBar TabLayoutAndroid Material风格的应用(二)--RecyclerViewAndroid Mater ...

  5. Project Euler 363 Bézier Curves(几何+二分)

    题目链接: https://projecteuler.net/problem=363 题目: A cubic Bézier curve is defined by four points: \(P_0 ...

  6. 读文件头数据判断 PE 文件格式和类型

    namespace X.Reflection { using System; using System.IO; public static partial class ReflectionX { pu ...

  7. HZK16应用实例

    在C51中,HZK16汉字库的使用(mydows's Blog转载) 定义如下: unsigned char str[]="我" 个字节长度,内容为"我"的GB ...

  8. LA 3135 - Argus

    看题:传送门 大意就是让你编写一个称为argus的系统,这个系统支持一个register的命令: Register  Q_num Period 该命令注册了一个触发器,它每Period秒就会残生一个编 ...

  9. vivado中basic memory生成

    vivado中basic memory生成

  10. python的报错

    1;; //////////////////////////////////////////////////////////////////////////////////////////////// ...