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

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

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

下面的代码直接附着:

//
// 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. 1.1 Introduction中 Topics and Logs官网剖析(博主推荐)

    不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Topics and Logs 话题和日志 (Topic和Log) Let's fi ...

  2. Codeforces Round #426 (Div. 1) A.The Meaningless Game (二分+数学)

    题目链接: http://codeforces.com/problemset/problem/833/A 题意: 给你 \(a\) 和 \(b\),两个人初始化为 \(1\).两个人其中一方乘以 \( ...

  3. What is corresponding Cron expression to fire in every X seconds, where X > 60? --转载

    原文地址:http://stackoverflow.com/questions/2996280/what-is-corresponding-cron-expression-to-fire-in-eve ...

  4. 添加asp.net mvc到现有的asp.net web form 应用程序

    前言 asp.net mvc的前一版本为asp.net web Form(Asp.net mvc之前称为asp.net),其第一个版本与2002年年初发布.asp.net web form 属于.ne ...

  5. DIV+CSS学习笔记(CSS)

    css基础知识: css样式表的定义 css:(Cascading Style Sheets)层叠样式表: 分类及位置:内部样式-head区域style标签里面 外部样式-link调用 内联样式-标签 ...

  6. AMBA标准

    AMBA标准定义了三种不同的总线 高级高性能总线(AHB): 高级系统总线(ASB):-----用的比较少 高级外设总线(APB). 基于AMBA的典型微控制器: 典型的AMBA AHB系统设计包含以 ...

  7. P2P借款的几种情况

    借款,至少出现2种人,借款人和出借人.根据人的性质,企业和个人,分成4种情况. 企业-个人,企业-企业,个人-企业,个人-个人. P2P平台可能出现几种情况: 个人-个人 2种情况:   a. 借款人 ...

  8. 打开utf-8文件乱码的解决方法

    gvim一直用的好好的,但是今天看一网友贴出来的代码时,却发现中文显示乱码了.... 使用notepad++打开,右下角显示是utf-8 w/0 BOM. 马上放狗, 发现解决方法如下:   在_vi ...

  9. vagrant 的安装与使用

    1. 安装 ubuntu 安装vagrant过程 ubuntu 安装 vagrant 时需要首先安装 virtualbox: (1)下载安装与当前 ubuntu 版本相适应的 virtualbox 安 ...

  10. vue中的select框的值动态绑定

    <--这两种写法效果一样--> 1: <select v-model="wxStatus"> <option label="已添加" ...