学习算法 - 表指针实现~ C++
表指针实现。第二种方法是使用访问列表,模拟指针。
在我的理解中学习,它是创建一个节点数组,模拟存储装置,然后从中分配内存和释放内存。
但实际的内存没有被释放~
下面的代码直接附着:
//
// 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、学习算法和刷题的框架思维——Go版
前情提示:Go语言学习者.本文参考https://labuladong.gitee.io/algo,代码自己参考抒写,若有不妥之处,感谢指正 关于golang算法文章,为了便于下载和整理,都已开源放在 ...
- C++学习之this指针
C++学习之this指针 一个对象的this指针并不是对象本身的一部分,不会影响sizeof(对象)的结果.this作用域是在类内部,当在类的非静态成员函数中访问类的非静态成员的时候,编译器会自动将对 ...
- 斯坦福大学公开课机器学习:advice for applying machine learning | learning curves (改进学习算法:高偏差和高方差与学习曲线的关系)
绘制学习曲线非常有用,比如你想检查你的学习算法,运行是否正常.或者你希望改进算法的表现或效果.那么学习曲线就是一种很好的工具.学习曲线可以判断某一个学习算法,是偏差.方差问题,或是二者皆有. 为了绘制 ...
- CS229 Lesson 5 生成学习算法
课程视频地址:http://open.163.com/special/opencourse/machinelearning.html 课程主页:http://cs229.stanford.edu/ 更 ...
- 强化学习实战 | 表格型Q-Learning玩井字棋(二)
在 强化学习实战 | 表格型Q-Learning玩井字棋(一)中,我们构建了以Game() 和 Agent() 类为基础的框架,本篇我们要让agent不断对弈,维护Q表格,提升棋力.那么我们先来盘算一 ...
- 强化学习实战 | 表格型Q-Learning玩井子棋(三)优化,优化
在 强化学习实战 | 表格型Q-Learning玩井字棋(二)开始训练!中,我们让agent"简陋地"训练了起来,经过了耗费时间的10万局游戏过后,却效果平平,尤其是初始状态的数值 ...
- [C#][算法] 用菜鸟的思维学习算法 -- 马桶排序、冒泡排序和快速排序
用菜鸟的思维学习算法 -- 马桶排序.冒泡排序和快速排序 [博主]反骨仔 [来源]http://www.cnblogs.com/liqingwen/p/4994261.html 目录 马桶排序(令人 ...
- Stanford大学机器学习公开课(五):生成学习算法、高斯判别、朴素贝叶斯
(一)生成学习算法 在线性回归和Logistic回归这种类型的学习算法中我们探讨的模型都是p(y|x;θ),即给定x的情况探讨y的条件概率分布.如二分类问题,不管是感知器算法还是逻辑回归算法,都是在解 ...
- 深度信任网络的快速学习算法(Hinton的论文)
也没啥原创,就是在学习深度学习的过程中丰富一下我的博客,嘿嘿. 不喜勿喷! Hinton是深度学习方面的大牛,跟着大牛走一般不会错吧-- 来源:A fast learning algorithm fo ...
随机推荐
- Oracle 12C R2 on Linux 7.X Data Guard 搭建文档
1.查看主机和数据库信息 [oracle@oracle1 ~]$ sqlplus / as sysdba SQL*Plus: Release 12.2.0.1.0 Production on ...
- 配置PL/SQL Developer连接server数据库
配置PL/SQL Developer连接server数据库 远程应用server上安装client客户端软件,可在oracle官网上下载. 举例: 环境 应用server操作系统 WIN 7 本地地址 ...
- OVS中对于用户层和datapath层的多个通道利用epoll进行控制
这里先临时记录下代码流程,有待完好. static int construct(struct ofproto *ofproto_) { struct ofproto_dpif *ofproto = o ...
- poj 3071 Football(线段树+概率)
Football Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2801 Accepted: 1428 Descript ...
- netty检测系统工具PlatformDependent
1. 检测jdk版本 @SuppressWarnings("LoopStatementThatDoesntLoop") private static int javaVersion ...
- Altium Designer敷铜的规则设定
InPolygon 这个词是铺铜对其他网络的设置,铺铜要离其他网络远点,因为腐蚀不干净会对 电路板有影响... 问题一:: 如下图所示,现在想让敷铜与板子边界也就是keepoutlayer的间距小一点 ...
- 关于Android中设置闹钟的相对比较完善的解决方案
我当时说承诺为大家写一个,一直没空,直到最近又有人跟我要,我决定抽时间写一个吧.确实设置闹钟是一个比较麻烦的东西.我在这里写的这个demo抽出来了封装了一个类库,大家直接调用其中的设置闹钟和取消闹钟的 ...
- css 单行图片文字水平垂直居中汇总
(1) 水平居中 a. 行内元素水平居中 因为img是行内元素(行内块级元素也一样)父级元素设置text-align:center即可,例如: <div style="width: 6 ...
- 【rlz03】十六进制转十进制
Time Limit: 3 second Memory Limit: 2 MB 问题描述 输入一个十六进制数,编程转换为十进制数. 整数部分不会超过65535,十六进制的小数部分不会超过2位. Sam ...
- 【24.17%】【codeforces 721D】Maxim and Array
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...