学习算法 - 表指针实现~ C++
2024-10-01 23:45:44
表指针实现。第二种方法是使用访问列表,模拟指针。
在我的理解中学习,它是创建一个节点数组,模拟存储装置,然后从中分配内存和释放内存。
但实际的内存没有被释放~
下面的代码直接附着:
//
// 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;
}
算法是没有问题。之后,我每一个功能考完试~
有任何疑问,请留言~
最新文章
- Yii2 vendor出现bower-asset这么解决
- xml转义字符
- android 多线程下载 断点续传
- jQuery_pager.js分页
- linux运维安全工具集合[持续更新中..]
- web项目学习之spring-security
- poj 3728 The merchant(LCA)
- 为已有表快速创建自动分区和Long类型like 的方法-Oracle 11G
- matlab等高线绘制
- windows远程桌面连接时,显示发生身份验证错误,给函数提供的身份无效
- 4.3Python数据类型(3)之字符串类型
- 洛谷P1908 逆序对【递归】
- Win10系列:C#应用控件基础3
- 20155326 2016-2017-2《Java程序设计》课程总结
- Jena 操作 RDF 文件
- JavaScript语法对{}的奇葩处理
- python 删除文件/夹
- CRF++模板使用(转)
- Scurm 术语
- LINQ查询中的IEnumerable<;T>;和IQueryable<;T>;
热门文章
- 9.Spring Boot实战之配置使用Logback进行日志记录
- 使用solr的DIHandler 构建mysql大表全量索引,内存溢出问题的解决方法
- 洛谷 P1727 计算π
- Qt Creator 源码学习 03:qtcreator.pro
- 软件——python,主函数
- OC学习篇之—写类别(类的扩展)
- 【例题 6-2 UVA - 514】Rails
- 【习题 3-4 UVA - 455】Periodic Strings
- Maven基础教程 分类: C_OHTERS 2015-04-10 22:53 232人阅读 评论(0) 收藏
- VC和MATLAB混合开发经验总结